Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MessageBox.Show() freezes execution : windows form c#

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.

like image 870
Nuru Salihu Avatar asked Jan 12 '23 23:01

Nuru Salihu


1 Answers

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?

like image 179
qwertoyo Avatar answered Jan 18 '23 10:01

qwertoyo