473,378 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Application.Run()

Hi,

Recently had an issue with the Audio object from
Microsoft.DirectX.AudioVideoPlayback. None of the events were firing from
the object during tests (NUnit or Console). Knowing I had it working
perfectly well in the application running on my tablet PC next to me, I
managed to narrow down the defining factor between the two applications and
found that it was a one line difference.

Application.Run().

I found I could "fix" my test by putting Application.Run at the end of the
thread that initializes the object. This is of course equivalent to loading
it into a Form and running the same thread in the LoadEvent of the form.
Having never encountered this problem before I had some questions that I
wondered if anyone could help me understand.

What exactly is the "standard application message loop" (from MS
documentation:
http://msdn.microsoft.com/library/de...ssRunTopic.asp)
and how does this differ to a standard thread event model?
Are there ways in which you can find out if an object requires the "standard
application message loop" other than trial and error?
Does anyone have any other information on this subject as I find it a
fascinating situation.

If anyone could drop some knowledge i'd appreciate it as I don't understand
this at all.

Kind Regards

Jax

Nov 17 '05 #1
2 2713
Uchiha Jax wrote:
What exactly is the "standard application message loop" (from MS
documentation:
http://msdn.microsoft.com/library/de...ssRunTopic.asp)
and how does this differ to a standard thread event model?
Are there ways in which you can find out if an object requires the
"standard application message loop" other than trial and error?
Does anyone have any other information on this subject as I find it a
fascinating situation.
new Form();

just creates a form object, it does not create the form window

Form f = new Form();
f.Visible = true;

the second line will create the window. In this property accessor a
Win32 window class is registered, then a window is created. Every window
has to have a windows procedure that handles messages. The messages are
put into a queue for the thread and a loop on the thread calls
GetMessage() to get a message from this queue and then passes it to the
windows procedure by calling DispatchMessage().

There is another aspect that you should be aware of. If a thread
procedure returns, the thread dies. So if your application has just one
thread, your application will live as long as the thread lives. (This
assumes that the thread is a foreground thread.) Even if the application
has a window and you have *not* clicked on the close button, if the
thread procedure finishes then the application will die. Try this:

class App
{
static void Main()
{
Form f = new Form();
f.Visible = true;
}
}

What happens? The form shows briefly and then goes away. The reason is
that the window is created but the thread procedure completes so the
process dies and the window is destroyed. Also, this window will not
respond to windows messages. to get round this you can add a call to
Application.Run() after the call to set the Visible property.

Now the window will remain, because Run implements a
GetMessage/DispatchMessage loop, so the main thread does not die. The
loop also means that the form will handle messages sent by the OS.
However there is a problem. If you click on the close button of the
form, the window will go away, but if you run task manager you'll see
that the process is still running. The problem is that the Visible set
accessor provides a windows procedure that handles the click on the
close button to close the window, but this windows procedure has no
connection to the application - it cannot tell the application to end.
This needs something called an application context, which is a
connection between the window and the message queue. If you pass the
form object to Application.Run an application context will be created. I
won't go into the details, but basically the application context is
based on the form, and if the form dies the application context breaks
the message loop and so the process dies. If the message queue dies then
the application context tells the form object (as opposed to the window)
to allow your code to clean up (Dispose is called).

Thus the life of the process is determined by the life of the form. Any
other form in the application will not have the application context and
so will not kill the process when it is closed.
I found I could "fix" my test by putting Application.Run at the end
of the thread that initializes the object. This is of course
equivalent to loading it into a Form and running the same thread in
the LoadEvent of the form. Having never encountered this problem
before I had some questions that I wondered if anyone could help me
understand.


Hopefully the above will explain what's going on.

I wrote an article for DDJ about this, but unfortunately you have to
register to read it:

http://www.ddj.com/documents/s=9698/ddj0505l/0505l.html

I am considering writing a workshop on how windows forms work (on the
lines of my workshops on Fusion and on Security), but to be honest at
the moment I'm quite pissed off with Microsoft so I don't seem to have
any incentive to do so.

Richard
--
http://www.grimes.demon.co.uk/workshops/fusionWS.htm
http://www.grimes.demon.co.uk/workshops/securityWS.htm
Nov 17 '05 #2
That's excellent, thanks for the information!
I appreciate that you have trawled back through the posts to answer this
question that was left unanswered so long ago.

Many, many thanks.

UchihaJax (Simon)

"Richard Grimes" <ri******@mvps.org> wrote in message
news:uG**************@TK2MSFTNGP15.phx.gbl...
Uchiha Jax wrote:
What exactly is the "standard application message loop" (from MS
documentation:
http://msdn.microsoft.com/library/de...ssRunTopic.asp) and how does this differ to a standard thread event model?
Are there ways in which you can find out if an object requires the
"standard application message loop" other than trial and error?
Does anyone have any other information on this subject as I find it a
fascinating situation.


new Form();

just creates a form object, it does not create the form window

Form f = new Form();
f.Visible = true;

the second line will create the window. In this property accessor a
Win32 window class is registered, then a window is created. Every window
has to have a windows procedure that handles messages. The messages are
put into a queue for the thread and a loop on the thread calls
GetMessage() to get a message from this queue and then passes it to the
windows procedure by calling DispatchMessage().

There is another aspect that you should be aware of. If a thread
procedure returns, the thread dies. So if your application has just one
thread, your application will live as long as the thread lives. (This
assumes that the thread is a foreground thread.) Even if the application
has a window and you have *not* clicked on the close button, if the
thread procedure finishes then the application will die. Try this:

class App
{
static void Main()
{
Form f = new Form();
f.Visible = true;
}
}

What happens? The form shows briefly and then goes away. The reason is
that the window is created but the thread procedure completes so the
process dies and the window is destroyed. Also, this window will not
respond to windows messages. to get round this you can add a call to
Application.Run() after the call to set the Visible property.

Now the window will remain, because Run implements a
GetMessage/DispatchMessage loop, so the main thread does not die. The
loop also means that the form will handle messages sent by the OS.
However there is a problem. If you click on the close button of the
form, the window will go away, but if you run task manager you'll see
that the process is still running. The problem is that the Visible set
accessor provides a windows procedure that handles the click on the
close button to close the window, but this windows procedure has no
connection to the application - it cannot tell the application to end.
This needs something called an application context, which is a
connection between the window and the message queue. If you pass the
form object to Application.Run an application context will be created. I
won't go into the details, but basically the application context is
based on the form, and if the form dies the application context breaks
the message loop and so the process dies. If the message queue dies then
the application context tells the form object (as opposed to the window)
to allow your code to clean up (Dispose is called).

Thus the life of the process is determined by the life of the form. Any
other form in the application will not have the application context and
so will not kill the process when it is closed.
I found I could "fix" my test by putting Application.Run at the end
of the thread that initializes the object. This is of course
equivalent to loading it into a Form and running the same thread in
the LoadEvent of the form. Having never encountered this problem
before I had some questions that I wondered if anyone could help me
understand.


Hopefully the above will explain what's going on.

I wrote an article for DDJ about this, but unfortunately you have to
register to read it:

http://www.ddj.com/documents/s=9698/ddj0505l/0505l.html

I am considering writing a workshop on how windows forms work (on the
lines of my workshops on Fusion and on Security), but to be honest at
the moment I'm quite pissed off with Microsoft so I don't seem to have
any incentive to do so.

Richard
--
http://www.grimes.demon.co.uk/workshops/fusionWS.htm
http://www.grimes.demon.co.uk/workshops/securityWS.htm

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: robert | last post by:
How do I get a remote XP Pro computer user on one domain to access and run a ..NET application on a different domain? Scenario: I was given a .NET application on domain (B) that I need to allow...
20
by: Michael A. Covington | last post by:
See: http://www.ai.uga.edu/mc/SingleInstance.html While attempting to use a mutex to allow only one instance of my app to run at a time (Recipe 4.12 in C# Programmer's Cookbook), I found that if...
6
by: orekin | last post by:
Hi There I have been trying to come to grips with Application.Run(), Application.Exit() and the Message Pump and I would really appreciate some feedback on the following questions .. There are...
6
by: billr | last post by:
I have developed a small API for taking care of a lot of boiler plate stuff in a multi formed windows application, for example setting up a messaging thread framework. New Forms, in the...
21
by: Chris | last post by:
I'm trying to get an existing VS.NET project up on my Win2003 server and I get the following error (on the actual website page): "It is an error to use a section registered as...
13
by: Bob Day | last post by:
Using vs2003, vb.net I start a thread, giving it a name before start. Code snippet: 'give each thread a unique name (for later identification) Trunk_Thread.Name = "Trunk_0_Thread" ' allow...
20
by: Peter Oliphant | last post by:
How does one launch multiple forms in an application? Using Photoshop as an example, this application seems to be composed of many 'disjoint' forms. Yet, they all seem somewhat 'active' in...
1
by: whitehorse | last post by:
When the warehousecontroller service is invoked, the following error message is sent to the application log: Event Type: Error Event Source: TFS Warehouse Event Category: None Event ID: 3000...
9
by: Brett Wesoloski | last post by:
I am new to VS2005. I changed my program.cs file to be a different form I am working on. But when I go to run the application it still brings up the form that was originally declared as new. ...
60
by: jim | last post by:
I am looking for an application that will wrap my .Net application (and any needed .Net parts) into a single exe. I know of Thinstall ($4,000 for application and per copy fees for your exes) and...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.