473,503 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Do I really need Application.Run in my Windows Forms application?

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 appllication using the API, are subclassed to a Form
contained within the API, and they are controlled (controlled in this
instance means, kept alive, displayed and hidden) at runtime by a thread
whose responsibility is this sole task.

In order to run the application, one also needs to further subclass an item
(BaseFormsApplicationStartup) contained within the API, whose required
parameter is the main Form of the application; for example ...

/ ************************************************** ****/
public class Startup : BaseFormsApplicationStartup
{
public Startup() : base(new MainUI())
{
AddDialog(new MessageWindow());
AddDialog(new GraphWindow());
AddDialog(new NetworkWindow());
DisplayMainApplicationDialog();
}
[STAThread]
static void Main() { new Startup(); }
}
/ ************************************************** ****/

The members AddDialog(BaseMessageApplicationWindow) and
DisplayMainApplicationDialog() are defined in the abstract class
BaseFormsApplicationStartup.

AddDialog simply places the application's Form under the control of a
DialogManager object. DisplayMainApplicationDialog, results (eventually) in
an instantiation of the following class for displaying the Form.

/ ************************************************** ****/
internal sealed class DialogDisplayThread
{
/// <summary>Internal Thread object</summary>
private Thread t = null;
/// <summary>Internal reference to the Form that owns the
Thread</summary>
private Form m_TheForm = null;
/// <summary>Constructor</summary>
/// <param name="f">The System.Windows.Forms.Form to display</param>
internal DialogDisplayThread(Form f)
{
m_TheForm = f;
t = new Thread(new ThreadStart(ThreadProc));
t.Name = f.Name + "DisplayThread";
t.IsBackground = true;
t.Start();
}
/// <summary>The ThreadStart method.</summary>
internal void ThreadProc() { m_TheForm.ShowDialog(); }
}
/ ************************************************** ****/

NOTE : I've used Form.ShowDialog() as opposed to Form.Show() because
the former is a blocking call, whilst the latter is not and as
soon as it
finishes, the Thread dies, which ultimately results in the Form
being
destroyed.

The point of all of this is that, I do not ever make use of
Application.Run(Form). I do not see this as a problem in itself, however, I
do have a problem in that my main application Form in a particular
implementation (making use of the API) is not displaying label text correctly.

I fire up the application and the Form is displayed, however where there are
System.Windows.Forms.Label controls on the Form, the text is not displayed,
and the control itself is completely transparent (through to the desktop or
underlying window, etc.), until I move the Form and cause an OnPaint event.
This behaviour continues on the form if it should be covered and then
uncovered, this OnPaint does not appear to affect the Label objects.

I have a sneaking suspicion that the problem would "go away" should I make a
call to Application.Run() passing in my main Form, but I'd rather know what
is going wrong so that I can address the issue rather than crowbarring the
API.

Thanks in advance ... if you want more info please do not hesitate to enquire.

Nov 17 '05 #1
6 2614
Odd that the some of the form isn’t actually being painted... I wonder if
some of what you are doing requires the message loop. The major difference
between using it and not is that Application.Run() “Begins running a standard
application message loop on the current thread.”

From:
http://msdn.microsoft.com/library/de...ssruntopic.asp

If that is the case, you’ve really got two options, both include calling
Application.Run, either as

new Startup();
Application.Run();

or

Application.Run( new Startup() );

My money however would be to go with the first option in either case.

Brendan
"billr" wrote:
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 appllication using the API, are subclassed to a Form
contained within the API, and they are controlled (controlled in this
instance means, kept alive, displayed and hidden) at runtime by a thread
whose responsibility is this sole task.

In order to run the application, one also needs to further subclass an item
(BaseFormsApplicationStartup) contained within the API, whose required
parameter is the main Form of the application; for example ...

/ ************************************************** ****/
public class Startup : BaseFormsApplicationStartup
{
public Startup() : base(new MainUI())
{
AddDialog(new MessageWindow());
AddDialog(new GraphWindow());
AddDialog(new NetworkWindow());
DisplayMainApplicationDialog();
}
[STAThread]
static void Main() { new Startup(); }
}
/ ************************************************** ****/

The members AddDialog(BaseMessageApplicationWindow) and
DisplayMainApplicationDialog() are defined in the abstract class
BaseFormsApplicationStartup.

AddDialog simply places the application's Form under the control of a
DialogManager object. DisplayMainApplicationDialog, results (eventually) in
an instantiation of the following class for displaying the Form.

/ ************************************************** ****/
internal sealed class DialogDisplayThread
{
/// <summary>Internal Thread object</summary>
private Thread t = null;
/// <summary>Internal reference to the Form that owns the
Thread</summary>
private Form m_TheForm = null;
/// <summary>Constructor</summary>
/// <param name="f">The System.Windows.Forms.Form to display</param>
internal DialogDisplayThread(Form f)
{
m_TheForm = f;
t = new Thread(new ThreadStart(ThreadProc));
t.Name = f.Name + "DisplayThread";
t.IsBackground = true;
t.Start();
}
/// <summary>The ThreadStart method.</summary>
internal void ThreadProc() { m_TheForm.ShowDialog(); }
}
/ ************************************************** ****/

NOTE : I've used Form.ShowDialog() as opposed to Form.Show() because
the former is a blocking call, whilst the latter is not and as
soon as it
finishes, the Thread dies, which ultimately results in the Form
being
destroyed.

The point of all of this is that, I do not ever make use of
Application.Run(Form). I do not see this as a problem in itself, however, I
do have a problem in that my main application Form in a particular
implementation (making use of the API) is not displaying label text correctly.

I fire up the application and the Form is displayed, however where there are
System.Windows.Forms.Label controls on the Form, the text is not displayed,
and the control itself is completely transparent (through to the desktop or
underlying window, etc.), until I move the Form and cause an OnPaint event.
This behaviour continues on the form if it should be covered and then
uncovered, this OnPaint does not appear to affect the Label objects.

I have a sneaking suspicion that the problem would "go away" should I make a
call to Application.Run() passing in my main Form, but I'd rather know what
is going wrong so that I can address the issue rather than crowbarring the
API.

Thanks in advance ... if you want more info please do not hesitate to enquire.

Nov 17 '05 #2
Brendan,

thanks for that, it is the solution I seem to have stumbled accross (
calling Application.Run() after instantiating my Startup class), and it does
fix the missing text issue.

Does anyone know why this should be the case?

--

--

Of all words of tongue and pen, the saddest are: "It might have been"
"Brendan Grant" wrote:
Odd that the some of the form isn’t actually being painted... I wonder if
some of what you are doing requires the message loop. The major difference
between using it and not is that Application.Run() “Begins running a standard
application message loop on the current thread.”

From:
http://msdn.microsoft.com/library/de...ssruntopic.asp

If that is the case, you’ve really got two options, both include calling
Application.Run, either as

new Startup();
Application.Run();

or

Application.Run( new Startup() );

My money however would be to go with the first option in either case.

Brendan
"billr" wrote:
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 appllication using the API, are subclassed to a Form
contained within the API, and they are controlled (controlled in this
instance means, kept alive, displayed and hidden) at runtime by a thread
whose responsibility is this sole task.

In order to run the application, one also needs to further subclass an item
(BaseFormsApplicationStartup) contained within the API, whose required
parameter is the main Form of the application; for example ...

/ ************************************************** ****/
public class Startup : BaseFormsApplicationStartup
{
public Startup() : base(new MainUI())
{
AddDialog(new MessageWindow());
AddDialog(new GraphWindow());
AddDialog(new NetworkWindow());
DisplayMainApplicationDialog();
}
[STAThread]
static void Main() { new Startup(); }
}
/ ************************************************** ****/

The members AddDialog(BaseMessageApplicationWindow) and
DisplayMainApplicationDialog() are defined in the abstract class
BaseFormsApplicationStartup.

AddDialog simply places the application's Form under the control of a
DialogManager object. DisplayMainApplicationDialog, results (eventually) in
an instantiation of the following class for displaying the Form.

/ ************************************************** ****/
internal sealed class DialogDisplayThread
{
/// <summary>Internal Thread object</summary>
private Thread t = null;
/// <summary>Internal reference to the Form that owns the
Thread</summary>
private Form m_TheForm = null;
/// <summary>Constructor</summary>
/// <param name="f">The System.Windows.Forms.Form to display</param>
internal DialogDisplayThread(Form f)
{
m_TheForm = f;
t = new Thread(new ThreadStart(ThreadProc));
t.Name = f.Name + "DisplayThread";
t.IsBackground = true;
t.Start();
}
/// <summary>The ThreadStart method.</summary>
internal void ThreadProc() { m_TheForm.ShowDialog(); }
}
/ ************************************************** ****/

NOTE : I've used Form.ShowDialog() as opposed to Form.Show() because
the former is a blocking call, whilst the latter is not and as
soon as it
finishes, the Thread dies, which ultimately results in the Form
being
destroyed.

The point of all of this is that, I do not ever make use of
Application.Run(Form). I do not see this as a problem in itself, however, I
do have a problem in that my main application Form in a particular
implementation (making use of the API) is not displaying label text correctly.

I fire up the application and the Form is displayed, however where there are
System.Windows.Forms.Label controls on the Form, the text is not displayed,
and the control itself is completely transparent (through to the desktop or
underlying window, etc.), until I move the Form and cause an OnPaint event.
This behaviour continues on the form if it should be covered and then
uncovered, this OnPaint does not appear to affect the Label objects.

I have a sneaking suspicion that the problem would "go away" should I make a
call to Application.Run() passing in my main Form, but I'd rather know what
is going wrong so that I can address the issue rather than crowbarring the
API.

Thanks in advance ... if you want more info please do not hesitate to enquire.

Nov 17 '05 #3
On Fri, 5 Aug 2005 07:31:03 -0700, billr wrote:
thanks for that, it is the solution I seem to have stumbled accross (
calling Application.Run() after instantiating my Startup class), and it does
fix the missing text issue.

Does anyone know why this should be the case?


I don't really have the time to fully understand what you are doing in your
framework, sorry, but one thing that i've noticed is that you are talking
about and using threads. Be very carefull NOT to use multiple threads to
create, show or access your UI elements (Forms or Controls). All your
Controls and classes derived from Control MUST be created, shown and
accessed from the UI thread only. You should never call a function or
access a property of a Control from a worker thread (that is, a thread that
is not the UI thread) or you'll run into loads of troubles.

Apart from that, i do not see any reason why you would have to use
Application.Run. Form.ShowDialog does starts its own message loop so i
would think that it's enough.
Nov 17 '05 #4
The reason why you use the Application class is because some controls
will not work properly if you don't. I believe the splitter control is one
of these classes. Some controls register message filters with the
Application class to provide certain functionality. If you don't use the
Application.Run method, these controls will have unexpected effects.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mehdi" <vi****@REMOVEME.gmail.com> wrote in message
news:c6****************************@40tude.net...
On Fri, 5 Aug 2005 07:31:03 -0700, billr wrote:
thanks for that, it is the solution I seem to have stumbled accross (
calling Application.Run() after instantiating my Startup class), and it
does
fix the missing text issue.

Does anyone know why this should be the case?


I don't really have the time to fully understand what you are doing in
your
framework, sorry, but one thing that i've noticed is that you are talking
about and using threads. Be very carefull NOT to use multiple threads to
create, show or access your UI elements (Forms or Controls). All your
Controls and classes derived from Control MUST be created, shown and
accessed from the UI thread only. You should never call a function or
access a property of a Control from a worker thread (that is, a thread
that
is not the UI thread) or you'll run into loads of troubles.

Apart from that, i do not see any reason why you would have to use
Application.Run. Form.ShowDialog does starts its own message loop so i
would think that it's enough.

Nov 17 '05 #5
thanks guys.

--

--

Of all words of tongue and pen, the saddest are: "It might have been"
"Nicholas Paldino [.NET/C# MVP]" wrote:
The reason why you use the Application class is because some controls
will not work properly if you don't. I believe the splitter control is one
of these classes. Some controls register message filters with the
Application class to provide certain functionality. If you don't use the
Application.Run method, these controls will have unexpected effects.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mehdi" <vi****@REMOVEME.gmail.com> wrote in message
news:c6****************************@40tude.net...
On Fri, 5 Aug 2005 07:31:03 -0700, billr wrote:
thanks for that, it is the solution I seem to have stumbled accross (
calling Application.Run() after instantiating my Startup class), and it
does
fix the missing text issue.

Does anyone know why this should be the case?


I don't really have the time to fully understand what you are doing in
your
framework, sorry, but one thing that i've noticed is that you are talking
about and using threads. Be very carefull NOT to use multiple threads to
create, show or access your UI elements (Forms or Controls). All your
Controls and classes derived from Control MUST be created, shown and
accessed from the UI thread only. You should never call a function or
access a property of a Control from a worker thread (that is, a thread
that
is not the UI thread) or you'll run into loads of troubles.

Apart from that, i do not see any reason why you would have to use
Application.Run. Form.ShowDialog does starts its own message loop so i
would think that it's enough.


Nov 17 '05 #6
I have made the Startup class subclass
System.Windows.Forms.ApplicationContext allowing for the following
implementation:

/** ******************************************* */
using System.Windows.Forms;
public abstract class BaseApplicationStartup : ApplicationContext,
IApplicationStartup
{
// implementation
}

public class Startup : BaseFormsApplicationStartup
{
public Startup() : base(new MainUI())
{
AddDialog(new MessageWindow());
/* add other dialogs */
DisplayMainApplicationDialog();
}

[STAThread]
static void Main() { Application.Run(new Startup()); }
}

/** ******************************************* */

This means no special treatment in the Main function, and it also means we
get to call Application.Run and set up the ApplicationMessageLoop which makes
all controls display properly on the forms.
--
--

Of all words of tongue and pen, the saddest are: "It might have been"
Nov 17 '05 #7

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

Similar topics

5
1700
by: Rob R. Ainscough | last post by:
This is more of a conceptual question: 1. More and more companies are using VPN's and locking out internet connectivity (for a host of reasons, security, productivity, etc.). 2. ASP.NET...
2
2130
by: Christian Kreimer | last post by:
Hi I created an C# windows forms application that holds a user control. The user control itself is an editor for geographical information systems and is based on an ActiveX Library for providing...
13
10045
by: Amjad | last post by:
Hi, Is there an equivalent to the "Application.Doevents" method in modules or Windows services? I want to make a Windows service that calls a DLL. The DLL would have all my functions and it...
18
6575
by: Dave Booker | last post by:
I have a Thread making a callback to a Form function that alters Form data. Of course, the debugger is telling me, "Cross-thread operation not valid: Control 'FormTest' accessed from a thread other...
22
9187
by: SQACSharp | last post by:
I'm trying to get the control name of an editbox in another window. The following code set the value "MyPassword" in the password EditBox but it fail to return the control name of the EditBox. ...
2
2419
by: Learning.Net | last post by:
hi , I have a application which reads files, directory,and its version and version information is written to text file.Its working fine if files in directory are less but problem arises when no...
11
3352
by: Peted | last post by:
Im using c# 2005 express edition Ive pretty much finished an winforms application and i need to significantly improve the visual appeal of the interface. Im totaly stuck on this and cant seem...
15
2023
by: active | last post by:
Below is a small but complete program that appears to show you can't retrive a Palette from the clipboard. This is true whether the palette is placed on the clipboard by Photoshop or Photoshop...
0
1393
by: Rambaldi | last post by:
Hi there, I can connect to the WebMethod from the WM6 emulator, but i cant connect from the device :S Shouldnt the device work as fine as the Emulator??? I change the web reference url to my...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7006
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
1
5021
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4685
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1519
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.