Question

Good day. I have a form and one backgroundworker . In the bw_Dowork event, there are instances when I need to print a message using MessageBox.Show()(i.e YES?NO box). However, whenever I call the messageBox.Show() method, the execution freezes and the form does not allow me to click my selection (i.e either Yes/No). Sometimes, if I want to work, I have to click fast as the message shows. Otherwise it freezes when I give a seconds of gap. Example of an instance where I use MessageBox.Show() is as shown below:

private void bw_DoWork(object sender, DoWorkEventArgs e)
{
    if (fileFTP.Exists == false)
    {
        _busy.WaitOne();

        if ((worker.CancellationPending == true))
        {
            e.Cancel = true;
            return;
        }

        SetText("File Ftp.exe are missing. This file are required to preform update, please contact yout system administrator to retrive Ftp.exe", true);
        MessageBox.Show("File Ftp.exe are missing. This file are required to preform update, please contact yout system administrator to retrive Ftp.exe");
        goto ExitProgram;
    }  
}

After I did some research about this online, some suggested the MessageBox is interfering with the interface thread. This makes me trigger the Messages using delegates but all to no avail. I had to remove all the MessageBoxes. Leaving one still freezes my execution when fired. Please any help would be appreciated.

Était-ce utile?

La solution

Try to use the main UI thread to show the messagebox:

this.BeginInvoke((Action)(() => MessageBox.Show("Hello")));

(assuming this is a Form)

Btw: "goto"? seriously?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top