Mark Guo
Mark Guo

Reputation: 213

How to ShowDialog in background thread on top of UI thread modeless?

Is there a way to show a form in a background thread but you can always see it in front of UI thread as modeless?

For example the UI thread run with parentForm, and there is a backgroundworker thread run with childForm on top. May I work with parentForm with childForm on top modeless, which means I can always see my childForm but not block my parentForm.

It seems childForm.ShowDialog(parentForm) will block UI thread and I don't want to Invoke childForm in UI thread.

Upvotes: 0

Views: 1946

Answers (1)

Picrofo Software
Picrofo Software

Reputation: 5571

I'm not sure what do you mean but you can always try to run Show() within a particular form if you would like to show the form without blocking the main UI

Example

Form2 _Form2 = new Form2();
_Form2.Show();

Alternatively, if you would like to run a new form as the main form of the application asynchronously, you may try to create a new Thread and run the form within it

Example

public void RunThread()
{
    Thread thread = new Thread(new ThreadStart(RunForm)); //Create a new thread to execute RunForm()
    thread.Name = "NewForm"; //Name the new thread (Not required)
    thread.Start(); //Start executing RunForm() in the new thread
}

public void RunForm()
{
    try
    {
        Application.Run(new Form2()); //Run Form2() as the main form of the application
    }
    catch (Exception ex)
    {
        //DoSomething
        //MessageBox.Show(ex.Message);       
    }
}

Thanks,
I hope you find this helpful :)

Upvotes: 1

Related Questions