How to do a log off upon a Form.Close()?

J

Joe Duchtel

Hello -

I have a form that will process some files over a long period of
time. I have it set up to be capable of automatically closing upon
completion (via Me.Close()) but how can I programatically log off the
user too?

The command for this should be
System.Diagnostics.Process.Start("shutdown", "-l") ... right? But
when do I call it? If I call it before Me.Close(), it seems like the
log off will be initiated before the form has been closed.

I looked at the Disposed event but I'm not sure this is the right
place for the "shutdown' either ... any suggestions on how I can do
this gracefully?

Thanks!
Joe
 
A

Armin Zingler

Am 21.09.2010 20:51, schrieb Joe Duchtel:
Hello -

I have a form that will process some files over a long period of
time. I have it set up to be capable of automatically closing upon
completion (via Me.Close()) but how can I programatically log off the
user too?

The command for this should be
System.Diagnostics.Process.Start("shutdown", "-l") ... right? But
when do I call it? If I call it before Me.Close(), it seems like the
log off will be initiated before the form has been closed.

I looked at the Disposed event but I'm not sure this is the right
place for the "shutdown' either ... any suggestions on how I can do
this gracefully?

I wouldn't start an external application for this purpose. Instead I'd
directly call ExitWindowsEx after the main loop exits:

Public Class Main

Public Const EWX_LOGOFF As UInteger = 0
Public Const SHTDN_REASON_MAJOR_APPLICATION As UInteger = &H40000
Public Const SHTDN_REASON_MINOR_OTHER As UInteger = 0

Declare Function ExitWindowsEx Lib "user32.dll" (ByVal Flags As UInteger, ByVal Reason As UInteger) As Boolean

Shared Sub Main()

Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New Form)

ExitWindowsEx(EWX_LOGOFF, SHTDN_REASON_MAJOR_APPLICATION Or SHTDN_REASON_MINOR_OTHER)

End Sub

End Class

Or one of the other reasons:
http://msdn.microsoft.com/en-us/library/aa376885(VS.85).aspx
 
O

Onur Güzel

Hello -

I have a form that will process some files over a long period of
time.  I have it set up to be capable of automatically closing upon
completion (via Me.Close()) but how can I programatically log off the
user too?

The command for this should be
System.Diagnostics.Process.Start("shutdown", "-l") ... right?  But
when do I call it?  If I call it before Me.Close(), it seems like the
log off will be initiated before the form has been closed.

I looked at the Disposed event but I'm not sure this is the right
place for the "shutdown' either ... any suggestions on how I can do
this gracefully?

Thanks!
Joe

Could you try calling shutdown command in your form's form_closed
event handler which is fired after the form is closed:

Private Sub Form1_closed _
(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.FormClosedEventArgs) _
Handles MyBase.FormClosed

System.Diagnostics.Process.Start("shutdown", "-l")

End Sub


HTH,

Onur Güzel
 
J

Joe Duchtel

Could you try calling shutdown command in your form's form_closed
event handler which is fired after the form is closed:

Private Sub Form1_closed _
(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.FormClosedEventArgs) _
Handles MyBase.FormClosed

System.Diagnostics.Process.Start("shutdown", "-l")

End Sub

HTH,

Onur Güzel- Hide quoted text -

- Show quoted text -

Thanks guys! I'll give both a try ...

Joe
 
J

Joe Duchtel

Am 21.09.2010 20:51, schrieb Joe Duchtel:





I wouldn't start an external application for this purpose. Instead I'd
directly call ExitWindowsEx after the main loop exits:

Public Class Main

   Public Const EWX_LOGOFF As UInteger = 0
   Public Const SHTDN_REASON_MAJOR_APPLICATION As UInteger = &H40000
   Public Const SHTDN_REASON_MINOR_OTHER As UInteger = 0

   Declare Function ExitWindowsEx Lib "user32.dll" (ByVal Flags As UInteger, ByVal Reason As UInteger) As Boolean

   Shared Sub Main()

      Application.EnableVisualStyles()
      Application.SetCompatibleTextRenderingDefault(False)
      Application.Run(New Form)

      ExitWindowsEx(EWX_LOGOFF, SHTDN_REASON_MAJOR_APPLICATION Or SHTDN_REASON_MINOR_OTHER)

   End Sub

End Class

Or one of the other reasons:http://msdn.microsoft.com/en-us/library/aa376885(VS.85).aspx

Hello Armin -

The FormXYZ I'm using does not use the Shared Sub Main() to get
launched but the FormXYZ_Load so I'm not sure where I would put the
ExitWindowsEx as I'm not calling the Run(). Is using a Shared Sub
Main() a "cleaner" solution over the FormXYZ_Load?

Hello Onur -

I tried your suggestion to just output a MsgBox() and it popped up
with the form still showing. How is that a FormClosed if I can still
see it? I also need to access a .Checked of a menu item before I call
the System.Diagnostics.Process.Start("shutdown", "-l"). Is it safe to
access that if the form is truly closed?

Thanks!
Joe
 
O

Onur Güzel

On Sep 21, 5:47 pm, Armin Zingler <[email protected]> wrote:
Hello Onur -

I tried your suggestion to just output a MsgBox() and it popped up
with the form still showing.  How is that a FormClosed if I can still
see it?  I also need to access a .Checked of a menu item before I call
the System.Diagnostics.Process.Start("shutdown", "-l").  Is it safe to
access that if the form is truly closed?

Thanks!
Joe- Hide quoted text -

Joe,
You did "not" mention that you'll output a MsgBox() but run shutdown
command. If you show a MsgBox on FormClosed event, MsgBox and the
caller form will remain alive for user input in order to return a
MessageBoxResult to the caller because of being "modal". After you
click something on MsgBox, form is closed and rest of the code is
executed. When i try with process.start, (eg: launch notepad.exe) on
formclosed event, form is closed and process is launched perfectly
fine, in that case you need to try "shudown -l" command on formclosed
event.

Besides that, you can try thread.sleep before launching "shutdown"
command to let app take a breath.

HTH,

Onur Güzel
 
J

Joe Duchtel

Joe,
You did "not" mention that you'll output a MsgBox() but run shutdown
command. If you show a MsgBox on FormClosed event, MsgBox and the
caller form will remain alive for user input in order to return a
MessageBoxResult to the caller because of being "modal". After you
click something on MsgBox, form is closed and rest of the code is
executed. When i try with process.start, (eg: launch notepad.exe) on
formclosed event, form is closed and process is launched perfectly
fine, in that case you need to try "shudown -l" command on formclosed
event.

Besides that, you can try thread.sleep before launching "shutdown"
command to let app take a breath.

HTH,

Onur Güzel

Hello -

I only put the MsgBox() in there to see what is happening when timing-
wise. Makes sense that the form stays alive while showing it. I'll
just put the System.Diagnostics.Process.Start("shutdown", "-l") in
there.

Thanks!
Joe
 
A

Armin Zingler

Am 22.09.2010 14:44, schrieb Joe Duchtel:
Hello Armin -

The FormXYZ I'm using does not use the Shared Sub Main() to get
launched but the FormXYZ_Load so I'm not sure where I would put the
ExitWindowsEx as I'm not calling the Run(). Is using a Shared Sub
Main() a "cleaner" solution over the FormXYZ_Load?

Sub Main is always the application entry. It's usually directly compiled
into the Assembly. It looks like the one above (w/o calling ExitWindowsEx).
That's ok if the default behavior is acceptable. Otherwise, use your own
Sub Main. Just insert my class into your project. You must also tell the
compiler to use it in the project's properties on the "application" tab.
There, disable the "application framework", then set the "startup object" to
Sub Main.

Predefined behavior is nice because it can save work. The disadavantage is that
it's only modifiable within limits. If you rely on it and need one single
thing that is not offered out-of-the-box, you have to (partially) redesign
and make it 100% your own, fully modifiable solution. From my experience,
it (almost) always turns out this way. That's why I always start with my
own Sub Main.

Instead, you could handle the Application's ShutDown event, but I think,
why handle an event if the two things I want to do are
1. show a form (and wait til it's closed)
2. Call ExitWindowsEx
? This seems to me to be the more straightforward approach.
 
J

Joe Duchtel

Am 22.09.2010 14:44, schrieb Joe Duchtel:








Sub Main is always the application entry. It's usually directly compiled
into the Assembly. It looks like the one above (w/o calling ExitWindowsEx).
That's ok if the default behavior is acceptable. Otherwise, use your own
Sub Main. Just insert my class into your project. You must also tell the
compiler to use it in the project's properties on the "application" tab.
There, disable the "application framework", then set the "startup object"to
Sub Main.

Predefined behavior is nice because it can save work. The disadavantage is that
it's only modifiable within limits. If you rely on it and need one single
thing that is not offered out-of-the-box, you have to (partially) redesign
and make it 100% your own, fully modifiable solution. From my experience,
it (almost) always turns out this way. That's why I always start with my
own Sub Main.

Instead, you  could handle the Application's ShutDown event, but I think,
why handle an event if the two things I want to do are
1. show a form (and wait til it's closed)
2. Call ExitWindowsEx
? This seems to me to be the more straightforward approach.

Makes sense ... thanks a lot!

Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top