Messagebox oddity with multiple threads

  • Thread starter Thread starter Andrew Ducker
  • Start date Start date
A

Andrew Ducker

When returning from a multithread callback, I've wrapped my code up
with:
if (MainForm.InvokeRequired)
MainForm.Invoke(CallStart,new object[]{BusinessCaller});
else
CallStart(BusinessCaller);

But even so, a Messagebox.show inside the delegate will sometimes be
modal to MainForm and sometimes happily allow me to switch back and
forth between the two.

If I _always_ do the MainForm.Invoke then it's always modal - but I'm
sure I've read somewhere that this is a bad idea.

Can anyone suggest why InvokeRequired might be coming back with the
wrong value?

Andy D
 
Got somewhere further!

It doesn't matter I'm in the right thread or not!

With the following code, if my app is active when the messagebox pops
up, then it's modal. If I have another app active, then it's not
modal. This makes no sense to me at all!

private void button1_Click(object sender, System.EventArgs e)
{
BlankDelegate waitABit = new BlankDelegate(WaitABit);
ThreadStart t = new ThreadStart(waitABit);
Thread n = new Thread(t);
n.Start();
}

public void WaitABit()
{
Thread.Sleep(5000);
TestMessage();
}

public void TestMessage()
{
if (this.InvokeRequired)
{
BlankDelegate testMessage = new BlankDelegate(TestMessage);
this.Invoke(testMessage,null);
}
else
MessageBox.Show("Here we are");
}

delegate void BlankDelegate();
 
Back
Top