473,756 Members | 7,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is this Application.Run () correct?

jm
This works, but I don't know why.

static void Main()
{
// Queue the task.
ThreadPool.Queu eUserWorkItem(n ew WaitCallback(Th readProc),new
Form1());
Application.Run ();
}

static void ThreadProc(Obje ct stateInfo)
{
// When no state object was passed to QueueUserWorkIt em, so
// stateInfo is null.
Form1 f1 = (Form1) stateInfo; //the passed form
f1.notifyIcon1. Icon = new
Icon(System.Ref lection.Assembl y.GetExecutingA ssembly().GetMa nifestResourceS tream("Listener 1.connecting.ic o"));
clsListening myLis = new clsListening(f1 );
myLis.StartList ening(f1);
}

I had to do it this way because I could not pass "this" to the class
because ThreadProc was static and new WaitCallBack(Th readProc)
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.Queu eUserWorkItem
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.
Nov 16 '05 #1
23 2498
jm <jo************ *@yahoo.com> wrote:
This works, but I don't know why.

static void Main()
{
// Queue the task.
ThreadPool.Queu eUserWorkItem(n ew WaitCallback(Th readProc),new
Form1());
Application.Run ();
}

static void ThreadProc(Obje ct stateInfo)
{
// When no state object was passed to QueueUserWorkIt em, so
// stateInfo is null.
Form1 f1 = (Form1) stateInfo; //the passed form
f1.notifyIcon1. Icon = new
Icon(System.Ref lection.Assembl y.GetExecutingA ssembly().GetMa nifestRes
ourceStream("Li stener1.connect ing.ico"));
clsListening myLis = new clsListening(f1 );
myLis.StartList ening(f1);
}

I had to do it this way because I could not pass "this" to the class
because ThreadProc was static and new WaitCallBack(Th readProc)
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?
The thread pool runs separately of the message queue. Application.Run
just starts a message pump for the given form, and waits until it
closes down.
Also, let me see if I am correct. The ThreadPool.Queu eUserWorkItem
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?


Apart from some terminology, that's basically right, yes.

Why are you doing it, out of interest?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Jon Skeet [C# MVP] <sk***@pobox.co m> wrote:
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?


The thread pool runs separately of the message queue. Application.Run
just starts a message pump for the given form, and waits until it
closes down.
Also, let me see if I am correct. The ThreadPool.Queu eUserWorkItem
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?


<snip>

I've just had another look at the code, and what you're doing it *not*
all right - you shouldn't be touching the form from any thread other
than the one which created it. You shouldn't be changing the icons etc
from a threadpool thread.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
jm
Jon Skeet [C# MVP] <sk***@pobox.co m> wrote in message news:<MP******* *************** **@msnews.micro soft.com>...
Jon Skeet [C# MVP] <sk***@pobox.co m> wrote:
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?


The thread pool runs separately of the message queue. Application.Run
just starts a message pump for the given form, and waits until it
closes down.
Also, let me see if I am correct. The ThreadPool.Queu eUserWorkItem
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?


<snip>

I've just had another look at the code, and what you're doing it *not*
all right - you shouldn't be touching the form from any thread other
than the one which created it. You shouldn't be changing the icons etc
from a threadpool thread.


I thought I only created it in the Threadpool since I didn't do it in
Application.Run ().
Nov 16 '05 #4
jm <jo************ *@yahoo.com> wrote:
I've just had another look at the code, and what you're doing it *not*
all right - you shouldn't be touching the form from any thread other
than the one which created it. You shouldn't be changing the icons etc
from a threadpool thread.


I thought I only created it in the Threadpool since I didn't do it in
Application.Run ().


No, you created it in the main thread, then passed it to the thread
pool thread.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
jm
Jon Skeet [C# MVP] <sk***@pobox.co m> wrote in message news:<MP******* *************** **@msnews.micro soft.com>...
jm <jo************ *@yahoo.com> wrote:
I've just had another look at the code, and what you're doing it *not*
all right - you shouldn't be touching the form from any thread other
than the one which created it. You shouldn't be changing the icons etc
from a threadpool thread.


I thought I only created it in the Threadpool since I didn't do it in
Application.Run ().


No, you created it in the main thread, then passed it to the thread
pool thread.


Could you point me to something I could read up on to learn more about
what I am doing wrong? I'm not sure what I should have done here.

Thanks.
Nov 16 '05 #6
jm <jo************ *@yahoo.com> wrote:
No, you created it in the main thread, then passed it to the thread
pool thread.


Could you point me to something I could read up on to learn more about
what I am doing wrong? I'm not sure what I should have done here.


See http://www.pobox.com/~skeet/csharp/t...winforms.shtml for
general info about Windows Forms threading. I'm not entirely sure which
bit you're having problems with though - or why you're doing stuff on
the thread pool thread in the first place.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
j m


I have a listening class that I modified from MSDN. I have Form1 with
the notifyIcon1 contextmenu, etc.

When the program loads I create Form1, but I do not run it. I only
wanted it to run as an icon in the system Tray. I also didn't want it
in Application.Run () because I needed to call my listening class from
the threadpool. I call from the threadpool because I want it to start
listening (with the while(true) in the listening class.) The Threapool
kicks off the new Form1() and the listener.

I put the new Form1() in Threadpool because I had to have some way to
instantiate Form1 *and* be able to reference it in my listening class.
Since the ThreadProc threadpool method is static, I could not send via
the "this" keyword, thus all my sending to the the Threadpool.

By sending it this way, I am still working (I thought, anyway) with the
original form at Main and can access the context menu at its events,
etc. (I have a context menu with events.)

Thanks again.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #8
j m <jo************ *@yahoo.com> wrote:
I have a listening class that I modified from MSDN. I have Form1 with
the notifyIcon1 contextmenu, etc.

When the program loads I create Form1, but I do not run it. I only
wanted it to run as an icon in the system Tray. I also didn't want it
in Application.Run () because I needed to call my listening class from
the threadpool. I call from the threadpool because I want it to start
listening (with the while(true) in the listening class.) The Threapool
kicks off the new Form1() and the listener.

I put the new Form1() in Threadpool because I had to have some way to
instantiate Form1 *and* be able to reference it in my listening class.
Since the ThreadProc threadpool method is static, I could not send via
the "this" keyword, thus all my sending to the the Threadpool.

By sending it this way, I am still working (I thought, anyway) with the
original form at Main and can access the context menu at its events,
etc. (I have a context menu with events.)


No - the problem is that you're changing things from the thread pool
thread, and you shouldn't be. Any time you need to change something in
the UI from a thread other than the one which created it, you need to
use Control.Invoke or Control.BeginIn voke.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
j m


What would be the consequences of doing it like I am doing it or am I
just wrong?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #10

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

Similar topics

6
20086
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 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
21
2175
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 allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS." I've been told by the previous developer that the website needs to be set as a "application". I...
12
2152
by: Simone | last post by:
Hi! I'm an Italian Boy, Excuse Me for my incorrect language. I have a Server with Windows Server 2003 Enterprise Architect and SQL Server 2000 Enterprise. I'm developing an ASP Web Application, but when i try to connect to my SQL Server Database by this connectionstring "data source=MYSERVER;initial catalog=MYDATABASE;password=MYPWD;user id=Sa"
13
5095
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 only 1 thread per line Trunk_Thread.ApartmentState = ApartmentState.STA
13
3940
by: Lee Newson | last post by:
Hi, I have just written my first application using VB.NET. The app works fine when i am running it within .NET for debugging purposes, however when i try to run the app from the .exe file that .NET creates i get the following error message: "An unhandled exception of type 'System.IO.FileNotFoundException' occurred in VisioTimeline.exe
20
2718
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 contrast to one of them always being 'modal' (e.g., if you are moving over a picture the 'Info' form will update the (x,y) screen location in realtime even if not the selected form. Also note that this example implies the various forms can communicate...
7
1131
by: Aryan | last post by:
Hi Everybody, I am facing "Server Application Unavailable" problem with .NET Framework 2.0 Earlier Framework was working fine, but one fine day it stopped working for particular application. Where as other application running under framework 2.0 is working fine. Giving you all error discription generated by my application(i am using
9
4684
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. When I put in a break point the program does not stop. It is in debug mode. If I change the program.cs file back to the form that was originally being used. The program does go into debug mode. But if I change code in that file it isn't using...
1
5225
by: Bhrionn | last post by:
Hello World, I am working on implementing a build for my companies application. The scenario implemeted is producing the error: ‘Class does not support automation or does not support expected interface' when I try to run the RegFree COM interop application from a clients machine. The application works fine for all development machines when run locally or from the network. The application is run from the network so - the component is not...
0
9384
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9973
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9790
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9779
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8645
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 project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6473
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5069
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5247
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2612
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.