Results 1 to 9 of 9

Thread: [RESOLVED] Threads, Processes getting locked

  1. #1

    Thread Starter
    Lively Member SpagettiProg's Avatar
    Join Date
    Dec 2004
    Location
    Miami, Florida
    Posts
    82

    Resolved [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:
    1. Imports System.ServiceProcess
    2. Imports System.Threading
    3. Imports System.IO
    4.  
    5.  
    6.  
    7.  
    8. Public Class Service1
    9.     Inherits System.ServiceProcess.ServiceBase
    10.  
    11.     Dim start As Diagnostics.Process
    12.     Dim thread1 As Thread
    13.     Dim mainPath As String
    14.  
    15.  
    16.     Protected Overrides Sub OnStart(ByVal args() As String)
    17.        
    18.         thread()
    19.  
    20.     End Sub
    21.  
    22.  
    23.  
    24.     Public Sub thread()
    25.  
    26.         thread1 = New Thread(AddressOf RunMyapp)
    27.         thread1.Start() 'starts the thread
    28.  
    29.     End Sub
    30.  
    31.  
    32.     Public Sub RunMyapp()
    33.  
    34.    
    35.         mainPath = "C:\Program Files\Windows NT\Accessories\wordpad.exe"
    36.         start = New Diagnostics.Process
    37.         start.StartInfo.WindowStyle = ProcessWindowStyle.Maximized
    38.         start.Start(mainPath)
    39.  
    40.  
    41.     End Sub
    Last edited by SpagettiProg; Jun 5th, 2006 at 09:57 AM. Reason: Resolved

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  3. #3

    Thread Starter
    Lively Member SpagettiProg's Avatar
    Join Date
    Dec 2004
    Location
    Miami, Florida
    Posts
    82

    Re: Threads, Processes getting locked

    Thanks but it still does not work after......
    VB Code:
    1. Protected Overrides Sub OnStop()
    2.         ' Add code here to perform any tear-down necessary to stop your service.
    3.  
    4.  
    5.         Try
    6.             start.Kill()
    7.  
    8.         Catch ex As Exception
    9.  
    10.         End Try
    11.        
    12.  
    13.     End Sub

    wordpad.exe still shows up as a running process in the taskmanager

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Threads, Processes getting locked

    try this:
    VB Code:
    1. start.CloseMainWindow()
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Threads, Processes getting locked

    and you can use
    VB Code:
    1. start.WaitForExit()
    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

  6. #6

    Thread Starter
    Lively Member SpagettiProg's Avatar
    Join Date
    Dec 2004
    Location
    Miami, Florida
    Posts
    82

    Re: Threads, Processes getting locked

    Thanks my friend, but still no cigar.
    I tried the following......

    VB Code:
    1. Try
    2.             start.CloseMainWindow()
    3.             start.WaitForExit()
    4.         Catch ex As Exception
    5.   End Try

    and wordpad.exe still shows up in the task manager.
    If you feel my post has helped, please rate it
    http://www.silentthread.com

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. mainPath = "C:\Program Files\Windows NT\Accessories\wordpad.exe"
    2. Process.Start(mainPath )
    In that case you'd have to assign the Process object returned by Process.Start to the 'start' variable.
    VB Code:
    1. mainPath = "C:\Program Files\Windows NT\Accessories\wordpad.exe"
    2. start = Process.Start(mainPath )
    What you are actually trying to do is this:
    VB Code:
    1. mainPath = "C:\Program Files\Windows NT\Accessories\wordpad.exe"
    2. start = New Diagnostics.Process
    3. start.StartInfo.WindowStyle = ProcessWindowStyle.Maximized
    4. start.StartInfo.FileName = mainPath
    5. 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:
    1. If start.CloseMainWindow() Then
    2.     'The Close message was successfully sent so wait up to 10 seconds for the app to exit.
    3.     start.WaitForExit(10000)
    4. End If
    5.  
    6. If Not start.HasExited Then
    7.     'Either the main window hasn't closed or there isn't one so force the process to end.
    8.     start.Kill()
    9. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Threads, Processes getting locked

    Quote 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

  9. #9

    Thread Starter
    Lively Member SpagettiProg's Avatar
    Join Date
    Dec 2004
    Location
    Miami, Florida
    Posts
    82

    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!
    If you feel my post has helped, please rate it
    http://www.silentthread.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width