C#BackgroundWorker在完成之前完成了被叫方式

时间:2015-12-22 16:39:57

标签: c# forms session outlook backgroundworker

我一直在努力弄清楚为什么我的背景工作者正在完成'它的工作还有很多工作要做。我实际上正在重构这个应用程序的代码,所以它确实在过去工作,但现在我无法弄清楚出了什么问题。

具体来说,应用程序应该打开Outlook然后执行一些检查。但是,后台工作人员在Outlook打开后没有明显原因直接退出(因为您将在下面继续进行大量处理)。

这似乎是在newSubtitleE方法的早期,在Outlook.exe上调用Start()之后直接发生的。

代码按此顺序运行:

Process.Start()

calling the background worker - this was the user's choice from a radio set

.... else if (radioButton5.Checked == true) { textBox1.Text = "Please wait while your session restarts"; pageControl1.SelectedIndex = 10; backgroundReset.RunWorkerAsync(); }

The do-work method

public void backgroundReset_DoWork(object sender, DoWorkEventArgs e) { backgroundReset.WorkerSupportsCancellation = true; Session.Reset(); }

the reset session method starts by killing the current session ...

从注释中可以看出,backgroundworder在Reset()方法执行完毕之前调用RunWorkerCompleted方式。

以下是其他方法(kill,logoff,start):

public static void Reset() { KillSession(); System.Threading.Thread.Sleep(5000); Start(); // THE BACKGROUNDWORKER EXITS BEFORE HERE! if (IsLoggedIn() == false) { return; } else { // Make sure Lync is open before finishing the process ... var j = 0; GetSession(Init.servers); j = 0; var checker = false; checker = ProcessHandler.CheckRunning("lync.exe"); while (checker == false) { if (j == 100) { break; } Thread.Sleep(500); checker = ProcessHandler.CheckRunning("lync.exe"); j++; } } }

KillSession logs the session of and then makes sure it is logged off

private static void KillSession() { if (sessionId != null) { LogOff(); for (int i = 0; i < 150; i++) { if (IsLoggedIn() == true) { Thread.Sleep(500); } else { break; } } } }

LogOff sends a Cmd command to log off the current session

public static void LogOff() { string strCmdIn = "/C LOGOFF " + sessionId + " /SERVER:" + serverName; Cmd.Exec(strCmdIn); }

Start() Simply opens Outlook, causing a Citrix session to also start. The app is definitely launching Outlook, but after that it doesn't reach either of the for statements - the BackgroundWorker just exits.

有谁知道这里发生了什么?这让我发疯了!

非常感谢

更新

public static void Start() { Process.Start(appDataCitrix + "Outlook.exe"); for (int i = 0; i < 15; i++) { if (IsLoggedIn2() == false) { Thread.Sleep(1000); } else { break; } } if (IsLoggedIn2() == false) { Process.Start(appDataCitrix + "Outlook.exe"); for (int i = 0; i < 10; i++) { if (IsLoggedIn2() == false) { Thread.Sleep(1000); } else { break; } } } } The RunWorkerCompleted Method:

As far as my understanding goes, this has no baring on when the process will finish.

1 个答案:

答案 0 :(得分:0)

这可能是因为start方法中抛出异常。

您可以在此方法周围添加try / catch块并在catch中处理错误,或者在发生异常时检入RunWorkerCompleted方法:

    private void RunWorkerCompleted (object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            // handle your exception here
        }
    }