I have defined a class like this:
class visualDisplay : IDisplay
{
//Fields
Form1 _form;
Thread _displayThread;
//Methods
public visualDisplay()
{
_displayThread = new Thread(new ThreadStart(initialize));
_displayThread.Start();
//initialize();
}
[STAThread]
void initialize()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run( _form = new Form1());
}
}
I want to make an instance of a form and use it. But if call the initialize()
method in the main thread (As it is commented above), I won't be able to run the rest of my code until I close the form.
On the other hand if I call it in the new thread which I defined (displayThread
), It will be like there is no Form at all and when I want to use _form
, I'll get the Null reference exception as below:
Object reference not set to an instance of an object.
So is there any way to fix this?