PC Review


Reply
Thread Tools Rate Thread

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

 
 
Joe Duchtel
Guest
Posts: n/a
 
      21st Sep 2010
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
 
Reply With Quote
 
 
 
 
Armin Zingler
Guest
Posts: n/a
 
      21st Sep 2010
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


--
Armin
 
Reply With Quote
 
Onur Güzel
Guest
Posts: n/a
 
      22nd Sep 2010
On Sep 21, 9:51*pm, Joe Duchtel <duch...@gmail.com> wrote:
> 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
 
Reply With Quote
 
Joe Duchtel
Guest
Posts: n/a
 
      22nd Sep 2010
On Sep 22, 4:52*am, Onur Güzel <kimiraikkone...@gmail.com> wrote:
> On Sep 21, 9:51*pm, Joe Duchtel <duch...@gmail.com> wrote:
>
>
>
>
>
> > 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- Hide quoted text -
>
> - Show quoted text -


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

Joe
 
Reply With Quote
 
Joe Duchtel
Guest
Posts: n/a
 
      22nd Sep 2010
On Sep 21, 5:47*pm, Armin Zingler <az.nos...@freenet.de> wrote:
> 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
>
> --
> Armin


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


 
Reply With Quote
 
Onur Güzel
Guest
Posts: n/a
 
      22nd Sep 2010
On Sep 22, 3:44*pm, Joe Duchtel <duch...@gmail.com> wrote:
> On Sep 21, 5:47*pm, Armin Zingler <az.nos...@freenet.de> 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
 
Reply With Quote
 
Joe Duchtel
Guest
Posts: n/a
 
      22nd Sep 2010
On Sep 22, 9:21*am, Onur Güzel <kimiraikkone...@gmail.com> wrote:
> On Sep 22, 3:44*pm, Joe Duchtel <duch...@gmail.com> wrote:
>
> > On Sep 21, 5:47*pm, Armin Zingler <az.nos...@freenet.de> 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


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
 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      22nd Sep 2010
Am 22.09.2010 14:44, schrieb Joe Duchtel:
>> 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

>
> 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.

--
Armin
 
Reply With Quote
 
Joe Duchtel
Guest
Posts: n/a
 
      22nd Sep 2010
On Sep 22, 9:54*am, Armin Zingler <az.nos...@freenet.de> wrote:
> Am 22.09.2010 14:44, schrieb Joe Duchtel:
>
>
>
>
>
> >> 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 AsUInteger, 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

>
> > 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.
>
> --
> Armin- Hide quoted text -
>
> - Show quoted text -


Makes sense ... thanks a lot!

Joe
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
docmd.close forms - doesn't close the form Wuelf Microsoft Access Reports 1 18th Nov 2008 01:05 PM
Close button on a form will not close form - error 2585 - action cannot be carried out while processing a form Angus Comber Microsoft Access 2 19th Jul 2006 12:21 PM
Clear Checkbox field on close of form or close of report =?Utf-8?B?QnJvb2s=?= Microsoft Access Form Coding 2 14th Jan 2006 09:31 PM
How to close parent form and not to close a child form also? Viper Microsoft C# .NET 5 15th Oct 2004 08:33 PM
How to close a userform in a forusers, close protected form? De Wilde Eddy Microsoft Excel New Users 1 7th Jun 2004 06:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:37 AM.