Threaded modal progress window

B

bob

Hello,

In my appliction I try to pop up a progress dialog box while an
analysis is being run:

//method progress window
public void Run(IWin32Window parent)
{
Thread analysisThread = new Thread(new ThreadStart(runAnalysis));
analysisThread.Start();
ShowDialog(parent);
}

so you see that I've sent the analysis off on another thread. Now
that's fine except in some situaitions I get an OpenGL error. This
seems to be because the analysis code is firing off the odd event,
some of which cause the application behind the modal progress box to
update itself. This doesn't seem very safe - the main loop updating
the main window + events from the new thread.

How can I fix this problem?

Please help, I'm stuck.

Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

Bob,

In order to get around this, you have to check in your event handlers
whether or not you are on the UI thread or not. The easiest way to do this
is to call the InvokeRequired property on a control on the form, or the form
itself. If it returns true, then you have to call the Invoke method to
perform any kind of action that might affect the UI. You would pass a
delegate to the method you want to call, as well as the parameters to pass
to that method.

Hope this helps.
 
B

bob

Thanks Nicholas,

that worked a treat


Nicholas Paldino said:
Bob,

In order to get around this, you have to check in your event handlers
whether or not you are on the UI thread or not. The easiest way to do this
is to call the InvokeRequired property on a control on the form, or the form
itself. If it returns true, then you have to call the Invoke method to
perform any kind of action that might affect the UI. You would pass a
delegate to the method you want to call, as well as the parameters to pass
to that method.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

bob said:
Hello,

In my appliction I try to pop up a progress dialog box while an
analysis is being run:

//method progress window
public void Run(IWin32Window parent)
{
Thread analysisThread = new Thread(new ThreadStart(runAnalysis));
analysisThread.Start();
ShowDialog(parent);
}

so you see that I've sent the analysis off on another thread. Now
that's fine except in some situaitions I get an OpenGL error. This
seems to be because the analysis code is firing off the odd event,
some of which cause the application behind the modal progress box to
update itself. This doesn't seem very safe - the main loop updating
the main window + events from the new thread.

How can I fix this problem?

Please help, I'm stuck.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top