0

I am using a Windows form application due to added security measures i have to work around for session in my application.Currently i am using a Timer to achieve the functionality I am able to close the form but i need to again restart the application to return to the login form.I am using the below code

Private Sub sessionTimer_Tick(sender As Object, e As EventArgs) Handles sessionTimer.Tick
    Try
        Me.sessionTimer.Stop()
        Me.sessionTimer.Enabled = False           
        Process.Start(Application.StartupPath + "\application.exe")
        Process.GetCurrentProcess().Kill()
    Catch ex As Exception

    End Try

End Sub 

I am getting an exception when i use the above method and it doesn't serve the purpose,also i have already tried using Application.Restart didn't work out.Please help i am new to windows form. Also adding to this in order to reset the timer i am using the below code.

Private Sub frmMain_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove
    Me.sessionTimer.Stop()
    Me.sessionTimer.Start()
End Sub

But this doesn't seem to work the main form has menu's which i am using to navigate to other forms so the idle time should not include the time spent in other forms which are opened via the menu's. What event should i use in frmMain to handle this problem.Thanks

S Philp
  • 452
  • 3
  • 14
Roy Devjyoti
  • 127
  • 1
  • 1
  • 11
  • 2
    What is the exception exactly? And `c#` tag seems unnecessary. – Soner Gönül Apr 08 '15 at 07:37
  • Why would you want to kill the application? isn't it better to just show the login form and close all other forms that are currently open? – Zohar Peled Apr 08 '15 at 07:54
  • I have tried using the below code @ZoharPeled instead of Process.Start & Kill Me.Hide() Dim f1 As New frmLogin f1.Show() But this doesn't seem to work although the login page is displayed but the main form is not displayed may be because there are other forms linked before the login form.I am having difficulty finding the point of entry or the first set of code which is exceuted. – Roy Devjyoti Apr 08 '15 at 08:46
  • As for the exception @SonerGönül it is "A problem caused the program to stop working correctly.Please close the Program" – Roy Devjyoti Apr 08 '15 at 08:59

2 Answers2

0

Iam using this code to restart my application. It works very well.

  System.Diagnostics.Process.Start(Application.ExecutablePath) 'First start a new instance
  Me.Close() 'Close the current application

If this doesnt work. I think there is no way around than using another application which restarts the Process. Here is a code example (second application)

Private Shared Sub RestartApp(pid As Integer, applicationName As String, arguments As String)
    ' Wait for the process to terminate
    Dim process__1 As Process = Nothing
    Try
        process__1 = Process.GetProcessById(pid)
        process__1.WaitForExit(1000)
            ' ArgumentException to indicate that the 
            ' process doesn't exist?
    Catch ex As ArgumentException
    End Try
    Process.Start(applicationName, arguments)
    'Arguments?
End Sub

Source of the code

Community
  • 1
  • 1
C0d1ngJammer
  • 550
  • 1
  • 6
  • 21
0

Just let the Framework do the work.

Application.Restart()

If your session Timer fires from a background thread (maybe you use System.Timers.Timer instead of System.Windows.Forms.Timer) you propable have to sync with your main thread.

Me.Invoke(new MethodInvoker(Addressof Application.Restart))

If Application.Restart does not work there is propably something wrong with your app. You should try the following.

  • If you created threads, be sure they are created with "thread.IsBackground = true" otherwise they will keep your process open
  • Stop your timers and background workers.
  • Be sure you don't have forms that handle the FormClosing event and set e.Cancel = true. In that case you have to take e.CloseReason into account.
  • User Mark Hurd posted a great post about what happens during Application.Restart, have a look here
Community
  • 1
  • 1
Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
  • i tried an Application.restart didn't workout i imported `System.Windows.Forms.Timer` to my frmMain can you tell me exactly how to sync my timer.I dragged a timer from my toolbox to frmMain. Also for the 'Me.Invoke(new MethodInvoker(Application.Restart))' it throws a compilation error – Roy Devjyoti Apr 08 '15 at 12:08
  • @RoyDevjyoti Sorry, I converted C# to VB.NET and didn't try it before. It should be `Me.Invoke(new MethodInvoker(Addressof Application.Restart))` I updated my answer. I also added some suggestions what to do next. – Jürgen Steinblock Apr 08 '15 at 12:52