Restarting app

J

John

Hi

Is there a way to restart a winform app in case of a serious error?

Thanks

Regards
 
K

kimiraikkonen

Hi

Is there a way to restart a winform app in case of a serious error?

Thanks

Regards

Hello,
A longer way which you may consider:
' My sample...

Public Class Form1
Dim haderror As Boolean = False

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
' Do some invalid stuff
Dim a As Integer = 1
Dim b As Integer = 0
Dim c As Integer
c = a / b

Catch
' Determine error is occured
haderror = True
' Close app
Me.Close()

End Try
End Sub

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

If haderror = True Then
' Start application again
System.Diagnostics.Process.Start("WindowsApplication1.exe")
End If
End Sub
End Class

or..just put Process.Start into Catch block where you know when an
error may happen without using Form_Closed event:

Try
' Do some invalid stuff
Dim a As Integer = 1
Dim b As Integer = 0
Dim c As Integer
c = a / b
Catch
MsgBox("Error! Restarting application...")
Me.Close()
System.Diagnostics.Process.Start("WindowsApplication1.exe")
End Try


Thanks,

Onur Güzel
 
J

JR

Hello,
A longer way which you may consider:
' My sample...

Public Class Form1
Dim haderror As Boolean = False

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
' Do some invalid stuff
Dim a As Integer = 1
Dim b As Integer = 0
Dim c As Integer
c = a / b

Catch
' Determine error is occured
haderror = True
' Close app
Me.Close()

End Try
End Sub

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

If haderror = True Then
' Start application again
System.Diagnostics.Process.Start("WindowsApplication1.exe")
End If
End Sub
End Class

or..just put Process.Start into Catch block where you know when an
error may happen without using Form_Closed event:

Try
' Do some invalid stuff
Dim a As Integer = 1
Dim b As Integer = 0
Dim c As Integer
c = a / b
Catch
MsgBox("Error! Restarting application...")
Me.Close()
System.Diagnostics.Process.Start("WindowsApplication1.exe")
End Try

Thanks,

Onur Güzel

Also take a look at aplication events specialy UnhandledException
 

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

Similar Threads


Top