G
Guest
Environment:
Visual Studio 2005, Beta 2
..Net 2.0
Windows XP, SP2
C#
Generics
-------------------------------
Hi,
I have a Windows Form whose contents I would like to dynamically change. I
also plan to put this form in a class library, along with a facade class.
This facade class will start the form, along with a Windows Message Loop
using Application.Run(new MyForm(ref MyQueue))
Moreover, the facade class has a method by which the user can add new
controls (of the same type) at runtime. The strategy used is this:
What I observe is that the event handler gets called on the Worker thread
and not on the main form thread. Hence, the exception "Caanot assign controls
created on one thread to another" is thrown.
How do I get around this problem? Does it help if the event generation is
done in a seperate class?
Visual Studio 2005, Beta 2
..Net 2.0
Windows XP, SP2
C#
Generics
-------------------------------
Hi,
I have a Windows Form whose contents I would like to dynamically change. I
also plan to put this form in a class library, along with a facade class.
This facade class will start the form, along with a Windows Message Loop
using Application.Run(new MyForm(ref MyQueue))
Moreover, the facade class has a method by which the user can add new
controls (of the same type) at runtime. The strategy used is this:
The Client class creates a Facade member object
The client schedules a function on the CLR threadpool. This function randomly calls Facade.AddNewControl(string name)
Next the Client Calls Facade.Start(string name). This order is chosen, since Application.Run is a blocking call
In Facade.AddNewControl(string name), we add the string 'name' to a Queue<string>. Note that this queue is passed as reference to the form class
The Form class spawns a Worker thread that scans the queue and raises custom events, whenever there is an entry available.
The Form also defines an event handler to handle this event, wherein it creates a new control and adds it.
What I observe is that the event handler gets called on the Worker thread
and not on the main form thread. Hence, the exception "Caanot assign controls
created on one thread to another" is thrown.
How do I get around this problem? Does it help if the event generation is
done in a seperate class?