Katherina
Katherina

Reputation: 2223

Opening another form with a method/function that is on another thread?

is it possible to open my second form, when the method/or function opening the 2nd form is on another thread?

i have read other threads related to this.. but it seems i cant figure out how to use the invoke

here's how i open the 2nd form when im calling this.. nothing just happens..(because its on the 2nd thread)

TimerMode f2 = new TimerMode();
f2.ShowDialog();

please help me. i newbie to multi -threading..

Upvotes: 0

Views: 472

Answers (2)

Brian Gideon
Brian Gideon

Reputation: 48949

It should be doing something. That is because ShowDialog will run its own message loop. The TimerMode form should at least be visible and functioning. But, you are right, this really is not the best practice especially if this form will be interacting with the other forms which are already running on the main UI thread.

Here is how you might do it.

anotherForm.Invoke(
  (MethodInvoker)(() =>
  {
    new TimerMode().ShowDialog();
  }));

Note that anotherForm is a reference to one of your other forms which is already hosted on the main UI thread.

Upvotes: 0

Reubene
Reubene

Reputation: 146

You need to execute on the main window thread Try the following:

this.Invoke((MethodInvoker)delegate{
    TimerMode f2 = new TimerMode();
    f2.ShowDialog();
}

This will create it on the right thread.

Upvotes: 2

Related Questions