locked
Stop a Thread RRS feed

  • Question

  •  

    Hi,

     

    In my application a user can start a process more then once. The application starts e new thread in which some monitoring is done. The threads get a unique name.

    After a while the user wants top stop a thread. I know the unique name but i don't know how to stop the correct thread.

     

    Help would be welcome!

    Steven

    Monday, May 26, 2008 9:36 AM

Answers

  • Hi Steven,

     

    This is the third time I have responded to this but the forums have problems today. What you need to do is create a hashtable or better yet a Dictionary(Of String, Thread) that you would use to store the created threads. So for example when you spawn a new thread add it to the hashtable/dictionary using the thread name as a key. Then you can use this key to lookup the thread and then abort it.

     

    Here is an example, not a fully working example, that demonstrates how you could do this.

     

    Dim threadTable as New Dictionary(Of String, Thread)

     

    Dim thrd1 as New Thread(AddressOf....)

    thrs1.Name = "Thread1"

     

    Dim thrd2 as New Thread(AddressOf....)

    thrs2.Name = "Thread2"

     

    threadTable.Add(thrd1.Name, thrd1)

    threadTable.Add(thrd2.Name, thrd2)

     

     

    Then when it came to finding the thread again

     

    Dim threadToStop as Thread = threadTable("Thread1")

     

    Something along the lines of this would work.

     

     

     

     

     

     

    Tuesday, May 27, 2008 4:30 PM

All replies

  •  

    Hi Steve,

     

    I have not tested it, but this should help you out:

     

    For Each ppp As Process In Process.GetProcesses

        If ppp.ProcessName = "YourProcessName" Then

           ppp.Close()

        End If

    Next

    Tuesday, May 27, 2008 2:36 PM
  • Hi Lars,

     

    Thank you for your response.

     

    I tried your solution but it didnt find my threads to stop. It found all the other running-processes but none of the threads started by mij application.

     

    Thx again,

    Steven

     

    Tuesday, May 27, 2008 2:49 PM
  • Hi Steven,

     

    This is the third time I have responded to this but the forums have problems today. What you need to do is create a hashtable or better yet a Dictionary(Of String, Thread) that you would use to store the created threads. So for example when you spawn a new thread add it to the hashtable/dictionary using the thread name as a key. Then you can use this key to lookup the thread and then abort it.

     

    Here is an example, not a fully working example, that demonstrates how you could do this.

     

    Dim threadTable as New Dictionary(Of String, Thread)

     

    Dim thrd1 as New Thread(AddressOf....)

    thrs1.Name = "Thread1"

     

    Dim thrd2 as New Thread(AddressOf....)

    thrs2.Name = "Thread2"

     

    threadTable.Add(thrd1.Name, thrd1)

    threadTable.Add(thrd2.Name, thrd2)

     

     

    Then when it came to finding the thread again

     

    Dim threadToStop as Thread = threadTable("Thread1")

     

    Something along the lines of this would work.

     

     

     

     

     

     

    Tuesday, May 27, 2008 4:30 PM
  • Thanks Derek,

     

    this works for me. The correct thread is stopped now.

     

    Many thanks.

    Greetings, Steven

     

    Wednesday, May 28, 2008 12:12 PM