473,836 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.EnableRaisin gEvents 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 1110
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.co m> wrote in message
news:OU******** *******@TK2MSFT NGP10.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.EnableRaisin gEvents 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*****@remove this.emsoft.and this.ca> wrote in message
news:%2******** *********@TK2MS FTNGP12.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.co m> wrote in message
news:OU******** *******@TK2MSFT NGP10.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.EnableRaisin gEvents 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_QUERYENDSESS ION 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.co m> wrote in message
news:Oo******** ******@TK2MSFTN GP11.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*****@remove this.emsoft.and this.ca> wrote in message
news:%2******** *********@TK2MS FTNGP12.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.co m> wrote in message
news:OU******** *******@TK2MSFT NGP10.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.EnableRaisin gEvents 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 CurrentControlS et/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*****@remove this.emsoft.and this.ca> wrote in message
news:u9******** ******@TK2MSFTN GP11.phx.gbl...
I don't think you can tell what the shutdown reason is. The underlying
WM_QUERYENDSESS ION 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.co m> wrote in message
news:Oo******** ******@TK2MSFTN GP11.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*****@remove this.emsoft.and this.ca> wrote in message
news:%2******** *********@TK2MS FTNGP12.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.co m> wrote in message
news:OU******** *******@TK2MSFT NGP10.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.EnableRaisin gEvents 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
18904
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 if there is a simple way from a main python program to kill a running thread? I see with the 'threading' module the way daemonic threads behave when the main program finishes.
4
1605
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 Threading.Thread(AddressOf PollThreadAddress) PollThread.Start() End Sub
1
1126
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 time to get the answer of returnValue // do some other calculations here, for example
21
6635
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, Francois ____________________________ The Netron Project, http://netron.sf.net
6
1191
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 operations. Syncing isn't really important in this, just 4 different watches that happen. So anyways, these "watchers" are on these threads, all raise an event called LogEvent to the main thread. LogEvent is in a module so it can be called
15
3059
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 process those data. What happen is the software will suddenly "dissapear" after running for some time.
7
318
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, Anthony Nystrom
10
1623
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 people like this!) I have a bunch of look-up tables to store information I'll have the users choose form drop-down boxes (aides - hearing aid, cain, dentures, etc) (Language the client speaks: English, Spanish, Russian, etc). I want the...
126
6765
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: http://www.cse.wustl.edu/~schmidt/ACE.html the same way that the STL (and subsequently BOOST) have been subsumed? Since it already runs on zillions of platforms, they have obviously worked most of the kinks out of the generalized threading and processes idea (along with many...
0
9671
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10846
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...
1
10595
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
10254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9379
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...
1
7793
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
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
5828
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3116
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.