Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show "please wait" message box

Tags:

c#

winforms

I want to show a 'please wait' message box while my main form is doing a lengthy task. As for my case, the lengthy task is transmitting serial protocol. Below is my code:

public void transmitprotocol()
{    
    try
    {
         MessageBox.Show("Please wait. Uploading logo.", "Status");

         // Transmitting protocol coding here. Takes around 2 minutes to finish.

    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.ToString());
    }
}

I've tried the above method using MessageBox like the above coding, but I always have to close the MessageBox only it will start transmitting protocol. Is there any method I can do to still show the 'please wait' MessageBox while it transmit protocol?

like image 380
Coolguy Avatar asked Feb 16 '26 13:02

Coolguy


2 Answers

You will need to do the expensive operation on a background thread. For that, use either a BackgroundWorker or the new Parallelization Library (.NET 4 and so on).

Actually you need to close the dialog because it blocks the execution until you, well, close it. What you do is that you start the operation, then show the dialog and then, once the operation is done, you close the dialog.

Now, if you're using WPF I will strongly suggest you to don't use a Dialog Box and instead use a Busy Indicator, it's free, pretty easy to use and not so ugly as the Message Box.

EDIT: Now that you specify you're using WinForms, then go ahead, implement the background worked and, why not, a transparent window without chrome whose purpose is to show a Busy label. Once the background worker ends you close that window.

enter image description here

like image 163
Erre Efe Avatar answered Feb 19 '26 03:02

Erre Efe


You have to prepare a backgroundworker and use a windows form instead of MessageBox. Something like this as simple as copy/paste:

    Form1 msgForm;
    public void transmitprotocol()
    {
        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
        //you can use progresschange in order change waiting message while background working
        msgForm = new Form1();//set lable and your waiting text in this form
        try
        {
            bw.RunWorkerAsync();//this will run all Transmitting protocol coding at background thread

            //MessageBox.Show("Please wait. Uploading logo.", "Status");
            msgForm.ShowDialog();//use controlable form instead of poor MessageBox
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.ToString());
        }
    }

    void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        // Transmitting protocol coding here. Takes around 2 minutes to finish. 
        //you have to write down your Transmitting codes here
        ...

        //The following code is just for loading time simulation and you can remove it later. 
        System.Threading.Thread.Sleep(5*1000); //this code take 5 seconds to be passed
    }
    void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        //all background work has complete and we are going to close the waiting message
        msgForm.Close();
    }
like image 26
Hamid Avatar answered Feb 19 '26 03:02

Hamid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!