Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem: My application prevents WinXP with SP2 from shutting down 1

Status
Not open for further replies.

ColdPhreze

Programmer
Apr 15, 2002
93
I have written a VB.NET 2003 application. It works perfectly with Win XP SP1.

However, when installed on a PC with Win XP SP2 (or if a PC is upgraded to SP2) if my application is running, the pc will NOT shutdown or restart! This IS NOT being caused by a virus or change in the code - it is directly correlated to SP2.

The _only_ way to get Win XP with SP2 to shutdown properly when my application is running is to:
1) Manually close my application and then click Start->Shutdown again.
2) Use the command line command: shutdown -f -t 01

Option 1 is not viable, because this is an app that runs in the system tray, and no-one wants to have to first close my app to shutdown Windows. I am using option 2 as a patch, but I need this fixed, it's very important.

I will post some code to assist if needed.

Thanks for all your help.

Check out my DeVry Senior Project: and my CarPC Hardware:
 
Might need some code for this one. Is it multi-threaded? I'm guessing when it gets the shut down message it is killing the worker thread, but some other thread sees the end of that thread and starts a new one. How is the application launched (what does your sub Main look like?), and is there anything threading or other advanced code bits?

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
and are you doing something in the closing event of any of the forms? Especially the main form?

Christiaan Baes
Belgium

"My new site" - Me
 
Good call, chrissie1.

I commented out the below code, and windows now shuts down properly.

Code:
Protected Overrides Sub OnClosing(ByVal e As _
System.ComponentModel.CancelEventArgs)
     'this overrides the X on the titlebar
     Me.Hide()
     e.Cancel = True
End Sub

But, I still would like to have the code to override the X button, so how should I go about doing this without causing the shutdown problem? I found the below code and think that may work. I'm trying it in a few minutes. Do you see any problems with it, or do you see a way I could do something similar with the above code?

Code:
Const WM_ENDSESSION As Integer = &H16
Dim osexit As Boolean = False

Protected Overrides Sub WndProc(ByRef e As Message)
    If (e.Msg = WM_ENDSESSION) Then
        osexit = True
        Application.Exit()
    End If
    MyBaseProc(e)
End Sub

Thanks,
KyferEz

Check out my DeVry Senior Project: and my CarPC Hardware:
 
I forgot the 2nd half of the below code, so here it is all over again:
Code:
Const WM_ENDSESSION As Integer = &H16
Dim osexit As Boolean = False

Protected Overrides Sub WndProc(ByRef e As Message)
    If (e.Msg = WM_ENDSESSION) Then
        osexit = True
        Application.Exit()
    End If
    MyBaseProc(e)
End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e _
As System.ComponentModel.CancelEventArgs) Handles _
MyBase.Closing

    If osexit Then
        'Not doing anything will let the app close

    Else
        e.Cancel = True 'This will cause the app to ignore
                        ' the close & continue running
        'Minimize the Window
        Me.WindowState = FormWindowState.Minimized
    End If

End Sub

Check out my DeVry Senior Project: and my CarPC Hardware:
 
if the user should never be able to close out of the app by using the X button, why not remove it?
 
There are instances when they will be able to... I am allowing the option to make the close button minimize or actually close the program. I kept the code snippits clear of that stuff to keep it easier to understand.

I did find a problem with the code above:
MyBaseProc(e) should actually be MyBase.WndProc(e)

KyferEz

Check out my DeVry Senior Project: and my CarPC Hardware:
 
kk...figured I'd ask the easy question since a lot of times that will be all you need :)
 
Ok, I tested the new code. It only kind-of works.

When my app is running with the new code, and you attempt to shutdown windows, the app closes. However, Windows Shutdown is halted and stays running.

If I goto Start->Shtudown again, the PC will shutdown.

If I remove the below code from the new code above, Windows Shutdown works fine.
Code:
e.Cancel = True 'This will cause the app to ignore

So there is obviously a problem with using that line of code. Any ideas? Can anyone duplicate this?

KyferEz

Check out my DeVry Senior Project: and my CarPC Hardware:
 
What you need is a singleton. Make your form a singleton. Or save it's state at onclosing instead of setting the e.cancel=true.

Christiaan Baes
Belgium

"My new site" - Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top