J
jm
This works, but I don't know why.
static void Main()
{
// Queue the task.
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc),new
Form1());
Application.Run();
}
static void ThreadProc(Object stateInfo)
{
// When no state object was passed to QueueUserWorkItem, so
// stateInfo is null.
Form1 f1 = (Form1) stateInfo; //the passed form
f1.notifyIcon1.Icon = new
Icon(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Listener1.connecting.ico"));
clsListening myLis = new clsListening(f1);
myLis.StartListening(f1);
}
I had to do it this way because I could not pass "this" to the class
because ThreadProc was static and new WaitCallBack(ThreadProc)
required a static method.
What I don't understand is how the thread part can run before the
Application.Run() can. How can it be executing code before it is
running?
Also, let me see if I am correct. The ThreadPool.QueueUserWorkItem
sends the an instantiated new Form1() to the ThreadProc. stateInfo
becomes that object. I then assign that object to the type Form1 f1
(cast to Form1 so I know what kind of object (object stateInfo) is)
and then I get to work with my form.
It works, but am I correct here?
Thank you for helping me.
static void Main()
{
// Queue the task.
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc),new
Form1());
Application.Run();
}
static void ThreadProc(Object stateInfo)
{
// When no state object was passed to QueueUserWorkItem, so
// stateInfo is null.
Form1 f1 = (Form1) stateInfo; //the passed form
f1.notifyIcon1.Icon = new
Icon(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Listener1.connecting.ico"));
clsListening myLis = new clsListening(f1);
myLis.StartListening(f1);
}
I had to do it this way because I could not pass "this" to the class
because ThreadProc was static and new WaitCallBack(ThreadProc)
required a static method.
What I don't understand is how the thread part can run before the
Application.Run() can. How can it be executing code before it is
running?
Also, let me see if I am correct. The ThreadPool.QueueUserWorkItem
sends the an instantiated new Form1() to the ThreadProc. stateInfo
becomes that object. I then assign that object to the type Form1 f1
(cast to Form1 so I know what kind of object (object stateInfo) is)
and then I get to work with my form.
It works, but am I correct here?
Thank you for helping me.