locked
Stop Thread Pool RRS feed

  • Question

  • Hi,

    Is there any direct way to stop a threadpool when user press Cancel Task button? I got SmartThreadPool but using that i could not arrange my code. Its becoming difficult.

     

    Here is my simple code:

    'Delegaet to update UI
    Public Delegate Sub updateClassDelegate(ByVal index As Integer, ByVal status As String)
    
    Sub StartTask()
    For i as integer = 0 to ListView1.Items.Count -1
    Dim wwresult as new wwresult
    
    wwresult.ind=i
    
    wwresult.taskstring="Something"
    wwresult.updatedelegate=Address Of updatelist
    
    Interlocked.Increment(threadcount)
    
          Try
            ThreadPool.SetMaxThreads(5, 5)
            ThreadPool.QueueUserWorkItem(AddressOf wwresult.getresult)
          Catch ex As Exception
            MsgBox(ex.Message)
          End Try
    
    End Sub
    
    
    'Update UI Helper
    Public Sub updatelist (byval ind as integer,byval status as string)
     If ListView1.InvokeRequired Then
          ListView1.Invoke(New updateClassDelegate(AddressOf updatemxresult), New Object() {index, status})
        Else
          updatemxresult(index, status)
        End If
    End Sub
    
    Public Sub UpdatemxResult (byval ind as integer,byval status as string)
    'Update ListView
    End Sub
    
    'wwresult is a class
    Private Class wwresult
    public ind as integer
    public taskstring as String
    Public updatedelegate as UpdateClassDelegate
    Public Sub getresult (Byval callback as Object)
    'Do some calculation
    UpdateDelegate(Ind,Result)
    End Sub
    End Class


    The code works excellent and great performance. But i need an automated way to stop the threadpool when user press Cancel Task button. I got SmartThreadPool.codeplex.com but i have very little knowledge to implement that in my code. If someone with experience can guide me that will help a lot.

     

    Thanks in advance.


    If my post answers your question then mark as answer.
    Wednesday, July 20, 2011 2:53 PM

Answers

  • Hi,

    Thanks for replying. I am fed up with BackGroundWorker. It is very amazing. I used the same class with 5 background worker. Everything goes fine but it don't update the listview correctly. If you have some sample that will work my class you are very welcomed to post it here. So that i can try that background worker code.

     

    Thanks.


    If my post answers your question then mark as answer.


    Hi Shaoun,

    Lucky, here is a code snippet about using backgroundworker to update label:

      Public cancel As Boolean = False
      Public Delegate Sub SetTitleDelegate(ByVal TitleText As String)
      Private Sub SetLabel(ByVal TitleText As String)
        If Me.InvokeRequired Then
          Me.Invoke(New SetTitleDelegate(AddressOf SetLabel), TitleText)
        Else
          Me.Label1.Text = TitleText
        End If
      End Sub
    
      Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim i As Integer = 0
        While cancel = False
          i += 1
          'Threading.Thread.Sleep(100)
          If System.IO.File.Exists("D:\A\mydata.csv") Then
            SetLabel("find it " & i.ToString)
          Else
            SetLabel("not yet " & i.ToString)
          End If
        End While
      End Sub
    

    You can set the public variable Cacel to true in wherever to stop the backgroundworker.

    And here is a related thread: http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/69689dfd-d895-4230-bd4b-059c8a0c0069

    I hope this will be helpful.

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Shaoun1000 Monday, July 25, 2011 1:14 PM
    Monday, July 25, 2011 9:54 AM

All replies

  • Hi,

    Anyone?


    If my post answers your question then mark as answer.
    Thursday, July 21, 2011 1:13 AM
  • Shaoun,

    The answer is simple, you can not stop a thread (the abort will do that at a certain moment)

    So if you have a process that should be ended (or interupted) before the end, than don't use threads.

    Microsoft is busy with a new Async process, but that is still in beta.

    Be aware, threads are mostly a reason of slowing down the total durance of a process, with the exception when you use more data input from an not direct connected source.

    The only thing you can do is give the impression that a process goes quicker by showing intermediate results.


    Success
    Cor
    • Proposed as answer by Mike Feng Monday, July 25, 2011 9:48 AM
    Thursday, July 21, 2011 6:40 AM
  • Hi Cor,

    Thanks. Yes you are right. After searching so many hours i got a technique. Also i found Abortable Thread Pool. Trying to implement that.

     

    You told beta Async process. Where i can find that? I understand it is in beta but i would love to view that also.

     

    Thanks.


    If my post answers your question then mark as answer.
    Thursday, July 21, 2011 6:54 AM
  • A complete overview for Async programming in Net

    http://msdn.microsoft.com/en-us/vstudio/gg316360


    Success
    Cor
    Thursday, July 21, 2011 6:58 AM
  • You actually can use a Background worker if I'm not mistaken. Using a background worker might be a little different from how you're doing it now, but it should work still.

     

    The code is:

     

    'If the worker is running
        If MyWorker.IsBusy Then
    
          'If you set the worker to allow cancellation
          If MyWorker.WorkerSupportsCancellation Then
    
            'Cancel worker
            MyWorker.CancelAsync()
          End If
        End If
    


    • Proposed as answer by Mike Feng Monday, July 25, 2011 9:55 AM
    Thursday, July 21, 2011 1:39 PM
  • Hi,

    Thanks for replying. I am fed up with BackGroundWorker. It is very amazing. I used the same class with 5 background worker. Everything goes fine but it don't update the listview correctly. If you have some sample that will work my class you are very welcomed to post it here. So that i can try that background worker code.

     

    Thanks.


    If my post answers your question then mark as answer.
    Thursday, July 21, 2011 10:30 PM
  •  "I am fed up with BackGroundWorker."

    I don't blame you. Microsoft has not defined the difference between foreground and background. The forground/background distictiom....well who knows whether it's good engineering or not? Real operating systems don't make such a distiction or tie their developers hands with such a bizarre distinction.

    Renee



    Friday, July 22, 2011 2:18 AM
  • Hi,

    Thanks for replying. I am fed up with BackGroundWorker. It is very amazing. I used the same class with 5 background worker. Everything goes fine but it don't update the listview correctly. If you have some sample that will work my class you are very welcomed to post it here. So that i can try that background worker code.

     

    Thanks.


    If my post answers your question then mark as answer.


    Hi Shaoun,

    Lucky, here is a code snippet about using backgroundworker to update label:

      Public cancel As Boolean = False
      Public Delegate Sub SetTitleDelegate(ByVal TitleText As String)
      Private Sub SetLabel(ByVal TitleText As String)
        If Me.InvokeRequired Then
          Me.Invoke(New SetTitleDelegate(AddressOf SetLabel), TitleText)
        Else
          Me.Label1.Text = TitleText
        End If
      End Sub
    
      Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim i As Integer = 0
        While cancel = False
          i += 1
          'Threading.Thread.Sleep(100)
          If System.IO.File.Exists("D:\A\mydata.csv") Then
            SetLabel("find it " & i.ToString)
          Else
            SetLabel("not yet " & i.ToString)
          End If
        End While
      End Sub
    

    You can set the public variable Cacel to true in wherever to stop the backgroundworker.

    And here is a related thread: http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/69689dfd-d895-4230-bd4b-059c8a0c0069

    I hope this will be helpful.

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Shaoun1000 Monday, July 25, 2011 1:14 PM
    Monday, July 25, 2011 9:54 AM
  • Actually I think Microsoft should eliminate "Background" as a status. I'd love to see it. The status has yet to be defined.

    Renee


    Monday, July 25, 2011 9:58 AM