Two threads within one application

  • Thread starter Thread starter Mystery Man
  • Start date Start date
M

Mystery Man

We have developed a large C#, MDI application. One of the components
of the application is a document assembly/script execution engine (ie
a small language that we have developed). This brings up a series of
modal dialogs when running scripts.

What we want, is to run this execution engine on a separate thread(?)
so that the users can use the rest of the application without having
to wait until the script is finished. A typical scenario is when they
start executing a script and then they get a phone call that they need
to respond to immediately.

We would prefer not to have to run two separate instances of the
application.

Any thoughts on how best to achieve this.
 
Your problem can be solved! In fact if you look on the QuickStart Tutorials
on gotdotnet.com, there is a complete example:
http://samples.gotdotnet.com/quickstart/howto/doc/WinForms/WinFormsThreadMarshalling.aspx

Windows Forms controls can only execute on the thread on which they were
created, that is, they are not thread-safe. If you want to get or set
properties, or call methods, on a control from a background thread, the call
must be marshaled to the thread that created the control.

There are five functions on a control that are safe to call from any thread:
InvokeRequired, Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all
other method calls, you should use one of the invoke methods.

By default, Windows marshals the calls for you. However, if you are making
multiple calls to a control, it is much more efficient to create a method
that executes those calls and make the one cross-thread call yourself. You
make the cross-thread call by calling one of the Control.Invoke methods. The
Invoke methods take a reference to a delegate. Typically, this delegate is
an instance of the MethodInvoker delegate.
 
Back
Top