|
-
Jun 2nd, 2006, 09:32 AM
#1
Thread Starter
Lively Member
[RESOLVED] Threads, Processes getting locked
Hi Everyone,
I'm writing a service in VB.NET, and it starts up fine, but I have a problem when I stop the service. If I launch an application from my service, when I stop the service, the other application is still running. How can I terminate it.
Note: I tried Posting a WM_CLOSE to wordpad.exe and it did not work.
Here is some code......
VB Code:
Imports System.ServiceProcess
Imports System.Threading
Imports System.IO
Public Class Service1
Inherits System.ServiceProcess.ServiceBase
Dim start As Diagnostics.Process
Dim thread1 As Thread
Dim mainPath As String
Protected Overrides Sub OnStart(ByVal args() As String)
thread()
End Sub
Public Sub thread()
thread1 = New Thread(AddressOf RunMyapp)
thread1.Start() 'starts the thread
End Sub
Public Sub RunMyapp()
mainPath = "C:\Program Files\Windows NT\Accessories\wordpad.exe"
start = New Diagnostics.Process
start.StartInfo.WindowStyle = ProcessWindowStyle.Maximized
start.Start(mainPath)
End Sub
Last edited by SpagettiProg; Jun 5th, 2006 at 09:57 AM.
Reason: Resolved
-
Jun 2nd, 2006, 10:10 AM
#2
Re: Threads, Processes getting locked
start a kill process onStop() to terminate your App
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 2nd, 2006, 05:28 PM
#3
Thread Starter
Lively Member
Re: Threads, Processes getting locked
Thanks but it still does not work after......
VB Code:
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
Try
start.Kill()
Catch ex As Exception
End Try
End Sub
wordpad.exe still shows up as a running process in the taskmanager
-
Jun 2nd, 2006, 06:34 PM
#4
Re: Threads, Processes getting locked
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 2nd, 2006, 06:37 PM
#5
Re: Threads, Processes getting locked
and you can use to make sure the process exited
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 4th, 2006, 10:10 AM
#6
Thread Starter
Lively Member
Re: Threads, Processes getting locked
Thanks my friend, but still no cigar.
I tried the following......
VB Code:
Try
start.CloseMainWindow()
start.WaitForExit()
Catch ex As Exception
End Try
and wordpad.exe still shows up in the task manager.
-
Jun 4th, 2006, 05:50 PM
#7
Re: Threads, Processes getting locked
That's because you've never assigned the Process object that you started to the 'start' variable. You have created one Process object and assigned it to the 'start' variable, but the Wordpad process you start is NOT that Process. The overload of Process.Start that you've used returns a Process object. In fact you've called it incorrectly because it is a Shared method. As your code is now you are not even setting its WindowStyle to Maximized. Try restoring the Wordpad window when it's open and then running your service again. You'll find that the Wordpad window will not be maximised. What you're doing is equivalent to this:
VB Code:
mainPath = "C:\Program Files\Windows NT\Accessories\wordpad.exe"
Process.Start(mainPath )
In that case you'd have to assign the Process object returned by Process.Start to the 'start' variable.
VB Code:
mainPath = "C:\Program Files\Windows NT\Accessories\wordpad.exe"
start = Process.Start(mainPath )
What you are actually trying to do is this:
VB Code:
mainPath = "C:\Program Files\Windows NT\Accessories\wordpad.exe"
start = New Diagnostics.Process
start.StartInfo.WindowStyle = ProcessWindowStyle.Maximized
start.StartInfo.FileName = mainPath
start.Start()
Some other points too. You should not specify the full path for Wordpad. It is contained in the system path so you can simply specify the file name. If you do specify the full path and Windows is not installed on C drive then your service will crash with an unhandled exception. Finally to close Wordpad you should use CloseMainWindow and WaitForExit, but you need to specify a timeout for WaitForExit or else, if it doesn't close for some reason, your service will wait indefinitely for it. This is the most appropriate code for ending processes:
VB Code:
If start.CloseMainWindow() Then
'The Close message was successfully sent so wait up to 10 seconds for the app to exit.
start.WaitForExit(10000)
End If
If Not start.HasExited Then
'Either the main window hasn't closed or there isn't one so force the process to end.
start.Kill()
End If
-
Jun 4th, 2006, 05:58 PM
#8
Re: Threads, Processes getting locked
 Originally Posted by jmcilhinney
Some other points too. You should not specify the full path for Wordpad. It is contained in the system path so you can simply specify the file name. If you do specify the full path and Windows is not installed on C drive then your service will crash with an unhandled exception. Finally to close Wordpad you should use CloseMainWindow and WaitForExit, but you need to specify a timeout for WaitForExit or else, if it doesn't close for some reason, your service will wait indefinitely for it. This is the most appropriate code for ending processes:
At some point the WordPad will close (maybe till shutdown) and if the service is supposed to do something after it's closed then it should wait indefinitely
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 5th, 2006, 09:52 AM
#9
Thread Starter
Lively Member
Re: Threads, Processes getting locked
Awesome! It works now. Thank you both very much for your help.
Both of you folks are getting reputation points from me!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|