Threading problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a form which acts as an mdi parent. It receives an event which has a name and message
It creates a new form for every name and displays messages within that form
The event handler calls RouteMessage which dtermines if the form already exists (using a hashtable) and tries to create the form if necessary
Unfortunately, this fails with the exceptio

'Controls created on one thread cannot be parented to a control on a different thread.

So the original parent form has been created on one thread and all the events are being handled on another thread
Any suggestions as to how I might solve this

Stev


private void MonitorServer_OnMonitorEvent(object file, MonitorEventArgs args

RouteMessage(args.serviceName,args.message)

public void RouteMessage(string serviceName,string message

if (serviceForms.Contains(serviceName)

((DisplayForm)serviceForms[serviceName]).AddMessage(message)

els

CreateNewForm(serviceName,message)


public DisplayForm CreateNewForm(string name,string message

DisplayForm displayForm = new DisplayForm(name)
displayForm.MdiParent = this
displayForm.Show()
if (message != null
displayForm.AddMessage(message)
serviceForms.Add(name,displayForm)
return displayForm
 
Steve said:
I have a form which acts as an mdi parent. It receives an event which
has a name and message. It creates a new form for every name and
displays messages within that form. The event handler calls
RouteMessage which dtermines if the form already exists (using a
hashtable) and tries to create the form if necessary. Unfortunately,
this fails with the exception

'Controls created on one thread cannot be parented to a control on a
different thread.'

So the original parent form has been created on one thread and all
the events are being handled on another thread. Any suggestions as to
how I might solve this?

As with everything UI-based, only do anything with the UI on the UI
thread which created the main window. Use Control.Invoke to invoke a
delegate on that UI thread.
 
Back
Top