473,387 Members | 1,771 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,387 software developers and data experts.

Threading Issues (If you can answer this, I'll kiss you.)

Any insight here is greatly appreciated... (Eveything is in VB .NET)

Here's the baisc app flow:
-------------------------------------
Launch wrapper.exe:
wrapper.exe registers for the SessionEnding system event
wrapper.exe stores a pointer to its thread in MainThread
wrapper.exe declares, withevents, a process variable: np
wrapper.exe does some basic maintenance
wrapper.exe populates np with process info and sets
fp.EnableRaisingEvents to True
wrapper.exe launches fp
wrapper.exe suspends its thread <- Waiting for fp exit
wrapper.exe does some basic maintenance
Exit wrapper.exe

SessionEnding Handler: (User is logging off or shutting down without
exiting fp)
force fp to exit
End Handler

fp Exit Handler: (fp has exited either via user interaction or the
SessionEnding Handler)
resume MainThread
End Handler
------------------------------------

If the user exits fp then everythign works just fine. If the user tries to
shutdown or logoff while fp is still running, then my SessionEnding handler
never gets called. The message just waits in the queue and fp is never
forced to exit and the user gets a dialog box claiming that some .NET
process isn't ending. (This is the typical dialog seen when a process
doesn't play nice at shutdown.) While that dialog is on the screen, if the
user then shuts down fp, my code will continue to run.

So, here's the basic question. Why does the ExitHandler run while the
MainThread is suspended, but the SessionEnding Handler does not? (If you
need more info to provide some advice, please let me know...)

Thanks.

Jerry
Nov 20 '05 #1
4 1089
When you register for a SystemEvent a window is created to receive the OS
broadcast messages. In your case the window is created on your main thread
but that thread is suspended so when the session ending broadcast message is
sent it is not read from the message queue and your SessionEnding handler is
never called.

On the other hand when you register for the process exit event the system
simply registers a wait on the process handle with the ThreadPool. When the
process exits a threadpool thread then calls your event handler. So the Exit
handler is not blocked by suspending your thread since it is called on a
different thread.

I assume in your case that your app is a console app and you do not want to
use a form on your main thread to provide a message pump for the broadcast
window. If that is the case, I think adding a MTAThread attribute to your
Main function in wrapper.exe might do the trick. The framework assumes that
any STA thread has a UI and so creates the broadcast window on that thread
but if the thread is MTA then no UI is assumed and a thread is created
specifically to handle the broadcast messages. The VB compiler, by default,
makes your Main thread STA unless you add the MTAThread attribute.
"Jerry Camel" <rl*****@msn.com> wrote in message
news:OU***************@TK2MSFTNGP10.phx.gbl...
Any insight here is greatly appreciated... (Eveything is in VB .NET)

Here's the baisc app flow:
-------------------------------------
Launch wrapper.exe:
wrapper.exe registers for the SessionEnding system event
wrapper.exe stores a pointer to its thread in MainThread
wrapper.exe declares, withevents, a process variable: np
wrapper.exe does some basic maintenance
wrapper.exe populates np with process info and sets
fp.EnableRaisingEvents to True
wrapper.exe launches fp
wrapper.exe suspends its thread <- Waiting for fp exit
wrapper.exe does some basic maintenance
Exit wrapper.exe

SessionEnding Handler: (User is logging off or shutting down without
exiting fp)
force fp to exit
End Handler

fp Exit Handler: (fp has exited either via user interaction or the
SessionEnding Handler)
resume MainThread
End Handler
------------------------------------

If the user exits fp then everythign works just fine. If the user tries to shutdown or logoff while fp is still running, then my SessionEnding handler never gets called. The message just waits in the queue and fp is never
forced to exit and the user gets a dialog box claiming that some .NET
process isn't ending. (This is the typical dialog seen when a process
doesn't play nice at shutdown.) While that dialog is on the screen, if the user then shuts down fp, my code will continue to run.

So, here's the basic question. Why does the ExitHandler run while the
MainThread is suspended, but the SessionEnding Handler does not? (If you
need more info to provide some advice, please let me know...)

Thanks.

Jerry

Nov 20 '05 #2
That helps explain the what's happening. I had worked around it by spawning
a new thread that does nothing but register for the system event. Now I
understand why that works. I'll look into the MTAThread attribute as a
solution as well...

Here's the kiss I promised... >*< SMACK >*<

I promise not to kiss you again if you can answer this...

The session ending event will tell me if the user is logging off or shutting
down, but does not differentiate between a user shutting down and a user
restarting the box? How can I tell what action the user has tried to
initiate? Thanks for you insight.

Jerry

"Stephen Martin" <sm*****@removethis.emsoft.andthis.ca> wrote in message
news:%2*****************@TK2MSFTNGP12.phx.gbl...
When you register for a SystemEvent a window is created to receive the OS
broadcast messages. In your case the window is created on your main thread
but that thread is suspended so when the session ending broadcast message is sent it is not read from the message queue and your SessionEnding handler is never called.

On the other hand when you register for the process exit event the system
simply registers a wait on the process handle with the ThreadPool. When the process exits a threadpool thread then calls your event handler. So the Exit handler is not blocked by suspending your thread since it is called on a
different thread.

I assume in your case that your app is a console app and you do not want to use a form on your main thread to provide a message pump for the broadcast
window. If that is the case, I think adding a MTAThread attribute to your
Main function in wrapper.exe might do the trick. The framework assumes that any STA thread has a UI and so creates the broadcast window on that thread
but if the thread is MTA then no UI is assumed and a thread is created
specifically to handle the broadcast messages. The VB compiler, by default, makes your Main thread STA unless you add the MTAThread attribute.
"Jerry Camel" <rl*****@msn.com> wrote in message
news:OU***************@TK2MSFTNGP10.phx.gbl...
Any insight here is greatly appreciated... (Eveything is in VB .NET)

Here's the baisc app flow:
-------------------------------------
Launch wrapper.exe:
wrapper.exe registers for the SessionEnding system event
wrapper.exe stores a pointer to its thread in MainThread
wrapper.exe declares, withevents, a process variable: np
wrapper.exe does some basic maintenance
wrapper.exe populates np with process info and sets
fp.EnableRaisingEvents to True
wrapper.exe launches fp
wrapper.exe suspends its thread <- Waiting for fp exit
wrapper.exe does some basic maintenance
Exit wrapper.exe

SessionEnding Handler: (User is logging off or shutting down without
exiting fp)
force fp to exit
End Handler

fp Exit Handler: (fp has exited either via user interaction or the
SessionEnding Handler)
resume MainThread
End Handler
------------------------------------

If the user exits fp then everythign works just fine. If the user tries

to
shutdown or logoff while fp is still running, then my SessionEnding

handler
never gets called. The message just waits in the queue and fp is never
forced to exit and the user gets a dialog box claiming that some .NET
process isn't ending. (This is the typical dialog seen when a process
doesn't play nice at shutdown.) While that dialog is on the screen, if

the
user then shuts down fp, my code will continue to run.

So, here's the basic question. Why does the ExitHandler run while the
MainThread is suspended, but the SessionEnding Handler does not? (If you need more info to provide some advice, please let me know...)

Thanks.

Jerry


Nov 20 '05 #3
I don't think you can tell what the shutdown reason is. The underlying
WM_QUERYENDSESSION message only tells you Shutdown or Logoff. There may be
something in WMI that would give you more information but I'm not overly
familiar with WMI so I can't give you any guidance there.
"Jerry Camel" <rl*****@msn.com> wrote in message
news:Oo**************@TK2MSFTNGP11.phx.gbl...
That helps explain the what's happening. I had worked around it by spawning a new thread that does nothing but register for the system event. Now I
understand why that works. I'll look into the MTAThread attribute as a
solution as well...

Here's the kiss I promised... >*< SMACK >*<

I promise not to kiss you again if you can answer this...

The session ending event will tell me if the user is logging off or shutting down, but does not differentiate between a user shutting down and a user
restarting the box? How can I tell what action the user has tried to
initiate? Thanks for you insight.

Jerry

"Stephen Martin" <sm*****@removethis.emsoft.andthis.ca> wrote in message
news:%2*****************@TK2MSFTNGP12.phx.gbl...
When you register for a SystemEvent a window is created to receive the OS
broadcast messages. In your case the window is created on your main thread but that thread is suspended so when the session ending broadcast message
is
sent it is not read from the message queue and your SessionEnding
handler is
never called.

On the other hand when you register for the process exit event the

system simply registers a wait on the process handle with the ThreadPool. When

the
process exits a threadpool thread then calls your event handler. So the

Exit
handler is not blocked by suspending your thread since it is called on a
different thread.

I assume in your case that your app is a console app and you do not want

to
use a form on your main thread to provide a message pump for the broadcast window. If that is the case, I think adding a MTAThread attribute to your Main function in wrapper.exe might do the trick. The framework assumes

that
any STA thread has a UI and so creates the broadcast window on that thread but if the thread is MTA then no UI is assumed and a thread is created
specifically to handle the broadcast messages. The VB compiler, by

default,
makes your Main thread STA unless you add the MTAThread attribute.
"Jerry Camel" <rl*****@msn.com> wrote in message
news:OU***************@TK2MSFTNGP10.phx.gbl...
Any insight here is greatly appreciated... (Eveything is in VB .NET)

Here's the baisc app flow:
-------------------------------------
Launch wrapper.exe:
wrapper.exe registers for the SessionEnding system event
wrapper.exe stores a pointer to its thread in MainThread
wrapper.exe declares, withevents, a process variable: np
wrapper.exe does some basic maintenance
wrapper.exe populates np with process info and sets
fp.EnableRaisingEvents to True
wrapper.exe launches fp
wrapper.exe suspends its thread <- Waiting for fp exit
wrapper.exe does some basic maintenance
Exit wrapper.exe

SessionEnding Handler: (User is logging off or shutting down without
exiting fp)
force fp to exit
End Handler

fp Exit Handler: (fp has exited either via user interaction or the
SessionEnding Handler)
resume MainThread
End Handler
------------------------------------

If the user exits fp then everythign works just fine. If the user tries
to
shutdown or logoff while fp is still running, then my SessionEnding

handler
never gets called. The message just waits in the queue and fp is
never forced to exit and the user gets a dialog box claiming that some .NET
process isn't ending. (This is the typical dialog seen when a process
doesn't play nice at shutdown.) While that dialog is on the screen,

if the
user then shuts down fp, my code will continue to run.

So, here's the basic question. Why does the ExitHandler run while the
MainThread is suspended, but the SessionEnding Handler does not? (If

you need more info to provide some advice, please let me know...)

Thanks.

Jerry



Nov 20 '05 #4
From what I know, actual ForceReboot or something like that key is set into
registry in Windows. However, MS is not very specific which version puts the
key where. One of KBs points to CurrentControlSet/Control/Windows but I
never seen this key there.
You might try this one
http://msdn.microsoft.com/library/de...oot_action.asp
I have slight suspicion that restart is implemented using this feature.
HTH
Alex

"Stephen Martin" <sm*****@removethis.emsoft.andthis.ca> wrote in message
news:u9**************@TK2MSFTNGP11.phx.gbl...
I don't think you can tell what the shutdown reason is. The underlying
WM_QUERYENDSESSION message only tells you Shutdown or Logoff. There may be
something in WMI that would give you more information but I'm not overly
familiar with WMI so I can't give you any guidance there.
"Jerry Camel" <rl*****@msn.com> wrote in message
news:Oo**************@TK2MSFTNGP11.phx.gbl...
That helps explain the what's happening. I had worked around it by

spawning
a new thread that does nothing but register for the system event. Now I
understand why that works. I'll look into the MTAThread attribute as a
solution as well...

Here's the kiss I promised... >*< SMACK >*<

I promise not to kiss you again if you can answer this...

The session ending event will tell me if the user is logging off or

shutting
down, but does not differentiate between a user shutting down and a user
restarting the box? How can I tell what action the user has tried to
initiate? Thanks for you insight.

Jerry

"Stephen Martin" <sm*****@removethis.emsoft.andthis.ca> wrote in message
news:%2*****************@TK2MSFTNGP12.phx.gbl...
When you register for a SystemEvent a window is created to receive the OS broadcast messages. In your case the window is created on your main thread but that thread is suspended so when the session ending broadcast message
is
sent it is not read from the message queue and your SessionEnding handler
is
never called.

On the other hand when you register for the process exit event the

system simply registers a wait on the process handle with the ThreadPool. When the
process exits a threadpool thread then calls your event handler. So
the
Exit
handler is not blocked by suspending your thread since it is called on
a different thread.

I assume in your case that your app is a console app and you do not want to
use a form on your main thread to provide a message pump for the broadcast window. If that is the case, I think adding a MTAThread attribute to your Main function in wrapper.exe might do the trick. The framework assumes

that
any STA thread has a UI and so creates the broadcast window on that thread but if the thread is MTA then no UI is assumed and a thread is created
specifically to handle the broadcast messages. The VB compiler, by

default,
makes your Main thread STA unless you add the MTAThread attribute.
"Jerry Camel" <rl*****@msn.com> wrote in message
news:OU***************@TK2MSFTNGP10.phx.gbl...
> Any insight here is greatly appreciated... (Eveything is in VB
..NET) >
> Here's the baisc app flow:
> -------------------------------------
> Launch wrapper.exe:
> wrapper.exe registers for the SessionEnding system event
> wrapper.exe stores a pointer to its thread in MainThread
> wrapper.exe declares, withevents, a process variable: np
> wrapper.exe does some basic maintenance
> wrapper.exe populates np with process info and sets
> fp.EnableRaisingEvents to True
> wrapper.exe launches fp
> wrapper.exe suspends its thread <- Waiting for fp exit
> wrapper.exe does some basic maintenance
> Exit wrapper.exe
>
> SessionEnding Handler: (User is logging off or shutting down without > exiting fp)
> force fp to exit
> End Handler
>
> fp Exit Handler: (fp has exited either via user interaction or the
> SessionEnding Handler)
> resume MainThread
> End Handler
> ------------------------------------
>
> If the user exits fp then everythign works just fine. If the user

tries to
> shutdown or logoff while fp is still running, then my SessionEnding
handler
> never gets called. The message just waits in the queue and fp is never > forced to exit and the user gets a dialog box claiming that some ..NET > process isn't ending. (This is the typical dialog seen when a process > doesn't play nice at shutdown.) While that dialog is on the screen, if the
> user then shuts down fp, my code will continue to run.
>
> So, here's the basic question. Why does the ExitHandler run while the > MainThread is suspended, but the SessionEnding Handler does not?

(If you
> need more info to provide some advice, please let me know...)
>
> Thanks.
>
> Jerry
>
>



Nov 20 '05 #5

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

Similar topics

12
by: Jerry Sievers | last post by:
Greetings Pythonists; I have limited experience with threaded apps and plenty with old style forked heavyweight multi-processing apps. Using Python 2.3.3 on a Redhat 7.x machine. Wondering...
4
by: Charles A. Lackman | last post by:
Hello and thank you for your assistance. I have attempted to accomplish what I need using delegates with no success. i.e. //Button Click// Dim PollThread As Threading.Thread PollThread = New...
1
by: jiing | last post by:
bool foundOrNot = 0; while (! foundOrNot){ // from here do some calculations here ...... ...... // till here foundOrNot = doSomeWastingTimeThing(); // This function will spend a lot of...
21
by: Illumineo | last post by:
My application uses multiple-threads and is a kind of AI simulation or ALife. This works fine but I was wondering if and how one can make use of hyper-threading in C#? Thanks for any hint,...
6
by: CJ Taylor | last post by:
Hey Everyone, Alright, now I cannot get this to work, I may be dumb and don't see it, but sickness will do that every now and then. So I have a program with 4 threads performing unique...
15
by: aikwee | last post by:
hi all, i have a software written in vb.net which has many thread in it. The software basically use about 8 threads to monitor in coming data from serial port, and some other thread that...
7
by: Anthony Nystrom | last post by:
What is the correct way to stop a thread? abort? sleep? Will it start up again... Just curious... If the thread is enabling a form, if the form is disposed is the thread as well? Thanks, ...
10
by: sara | last post by:
I have been volunteered to write a simple system to help a non-profit enter and track information on the elders they serve. (It's actually a fascinating activity, and very rewarding to be helping...
126
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.