Hi There
I have been trying to come to grips with Application.Run (),
Application.Exi t() and the Message Pump and I would really appreciate
some feedback on the following questions .. There are quite a few
words in this post but the questions are actually quite similar and
should be fairly quick to answer ...
(1) What is Happening with the Threads
In the MSDN documentation for 'Application.Ru n Method' it says that
the method 'Begins running a standard application message loop on the
current thread ...'
In the web pages I have found, most refer to the 'UI Thread' (for
example Alex Dong's blog http://ms.mblogger.cn/dongxun/posts/8004.aspx
and the other links that the blog refers to).
Is the 'current thread' that MSDN talks about the same as the 'UI
Thread' ?
(2) Simple Example
Lets say I build a very simple application, with a StartUp class
(contained in a cs file) and a form. The start up class just has a
few lines:
public class StartUp
{
[STAThread]
static void Main()
{
Application.Run (new ExForm());
Debug.WriteLine ("Finished first Application Run, about to start
second");
Application.Run (new ExForm());
Debug.WriteLine ("Finished second Application Run");
}
}
And the form - ExForm - just has a 'close button', which calls
this.close() [where 'this' refers to the form itself]
In the above example, what is happening with threads? Is there a
thread that the 'static void Main()' function runs on, that is
separate from the 'UI Thread' ?
I am very confused about the threads because I am of the understanding
that the last three lines should not execute. What I would expect to
happen is that the form would show once, then when the close button is
clicked, execution stops. However if you run the above example, the
form DOES show a second time and both the Debug.WriteLine statements
are executed.
(3) Above Example, but with Close button click event calling
Application.Exi t()
Using the example in (1), Change the form to execute
Application.Exi t() in the close button's click event [rather than
this.close, which I was using in the first example]. What happens
here has me befuddled ... The form does show a second time but it
flashes on and off the screen. Also, both Debug.WriteLine statements
execute .?.?.?.
(4) Another scenario to round out the question
With the form's close button click event calling Application.Exi t(),
change the main function to:
ExForm myExForm = new ExForm();
Debug.WriteLine ("About to show form for first time");
Application.Run (myExForm);
myExForm = new ExForm();
Debug.WriteLine ("About to show form for second time");
Application.Run (myExForm);
Debug.WriteLine ("All finished");
In the above example, the form shows twice and all Debug.WriteLine 's
are executed.
However change the main function to:
ExForm myExForm = new ExForm();
Debug.WriteLine ("About to show form for first time");
Application.Run (myExForm);
myExForm = new ExForm();
Debug.WriteLine ("About to show form for second time");
Application.Run (myExForm);
Debug.WriteLine ("All finished");
And the second time the form shows, it flickers on and off, however
all Debug.WriteLine 's execute .?.?.?.
(5) Using a while statement works when I don't expect it to
I am of the understanding that a form needs a message pump to interact
with users. If that understanding is correct, why does this code work?
public class StartUp
{
[STAThread]
static void Main()
{
ExForm myExForm = new ExForm();
myExForm.Show() ;
while (myExForm.Visib le)
System.Windows. Forms.Applicati on.DoEvents();
}
}
(6) Working out Terminology
Alex Dong's web blog (http://ms.mblogger.cn/dongxun/posts/8004.aspx)
and John Conwell's article
(http://www.c-sharpcorner.com/Code/20...ntextClass.asp)
provide conflicting explanations when it comes to the event hookups
between ExitThread, OnAppThreadExit , OnMainFormDestr oyed and
HandleDestroyed . Could somebode please explain what event belongs to
what object and how they link together?
Thanks In Advance
Bill