bmw325ist
New member
- Joined
- Apr 29, 2010
- Messages
- 3
- Programming Experience
- 1-3
I want to close the main form after another process has exited if the user says yes. I have the MainWindow Class and a modProcess Class coded below. I'm using VS 2010. For whatever reason, the object Me or MainWindow is not accessible, how do I fix it? Thanks.
Second Class:
VB.NET:
Public Class MainWindow
Dim msProc As New Process
Private Shared strPathname As String = "C:\Windows\notepad.exe"
Private Shared strProcname As String = System.IO.Path.GetFileNameWithoutExtension(strPathname)
Private Sub MainWindow_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loaded
CheckStatus()
End Sub
Public Sub CheckStatus()
If (modProcess.IsProcessRunning(strProcname)) Then
modProcess.MonitorProcess(strProcname)
Else
If MsgBox(strProcname & " is Not Running, would you like to open " & strProcname & "?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
modProcess.StartProcess(strPathname)
System.Threading.Thread.Sleep(5000)
CheckStatus()
Else
Me.Close()
End If
End If
End Sub
Public Shared Sub ProcessExited(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim msProc As Process
msProc = DirectCast(sender, Process)
If MessageBox.Show(msProc.ProcessName & " has exited at: " & msProc.ExitTime & _
Environment.NewLine & "Would you like to restart " & msProc.ProcessName & "?", _
"Attention", MessageBoxButton.YesNo, MessageBoxImage.Question) = MessageBoxResult.Yes Then
msProc.Close()
modProcess.StartProcess(strPathname)
System.Threading.Thread.Sleep(500)
Me.Close()
Else
msProc.Close()
End If
End Sub
End Class
Second Class:
VB.NET:
Class modProcess
Public Shared Function IsProcessRunning(ByVal strProcName As String) As Boolean
Try
'create an array of Processes
Dim npProc() As Process
'GetProcesses returns an array of processes that are running
npProc = Process.GetProcessesByName(strProcName)
If npProc.Length > 0 Then
Return True
Else
Return False
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
Public Shared Sub StartProcess(ByVal strFilename As String)
Dim strProcName As String = IO.Path.GetFileNameWithoutExtension(strFilename)
Dim proc As New Process
proc.StartInfo.FileName = strFilename
proc.Start()
End Sub
Public Shared Sub MonitorProcess(ByVal strProcessName As String)
Dim procs() As Process
Dim proc As Process
procs = Process.GetProcessesByName(strProcessName)
proc = procs.FirstOrDefault
proc.EnableRaisingEvents = True
AddHandler proc.Exited, AddressOf MainWindow.ProcessExited
End Sub
Public Shared Sub MonitorProcess(ByVal lProc As Long)
Dim msProc As Process
msProc = Process.GetProcessById(lProc)
msProc.EnableRaisingEvents = True
AddHandler msProc.Exited, AddressOf MainWindow.ProcessExited
End Sub
Public Shared Sub KillProcess(ByVal strProcessName As String)
Dim procs() As Process
Dim proc As Process
procs = Process.GetProcessesByName(strProcessName)
proc = procs.FirstOrDefault
proc.Kill()
End Sub
End Class