Visual C# Developer Center > Visual C# Forums > Visual C# General > How to use second thread to abort primary thread?
Ask a questionAsk a question
 

Proposed AnswerHow to use second thread to abort primary thread?

  • Saturday, November 07, 2009 12:23 AMsummy00 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have a password verify function for example, I want use this function in second thread to check the password. If the password is correct, second thread exit. Otherwise, the two thread are all exit. And the primary thread can prompts the second thread to check password at any moment.

    My solution is: creat a second thread, use ThreadStart.(verifyfunction) to verify password, but I find I can't abort these two thread when I get the wrong password. How to do it ?  Is implemention  in primary thread or second thread?  My purpose is using second thread to verify password so it do not affect the primary thread.(verifying the password  takes to much time. When it verifying, the primary thread do its own work.

    Thank you!

All Replies

  • Saturday, November 07, 2009 7:02 AMGeek13 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
    As I understand the purpose of your secondary thread is only to verify the password. You did not mention how it will intimate the primary thread of the result. Is it through some shared variable?

    I assume that you are using this from a WinForm. A good approach in this case would be to use a backgroundworker thread. You can pass in the required inputs to the DoWork event handler and assign the result (success or failure) to the Result property of DoWorkEventArgs. Now examine this in the RunWorkerCompleted event. For a detailed explanation of how to use the BackgroundWorker class see the below msdn link

    http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
    .NET Developer
  • Monday, November 09, 2009 1:29 AMsummy00 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thank you! I have tested the sample, but I have two other question:

    1. When I double click the button, which contain the RunWorkerAsync function, It will cause a error:

        System.InvalidOperationException: This BackgroundWorker is currently busy and cannot run multiple tasks concurrently.
           at System.ComponentModel.BackgroundWorker.RunWorkerAsync(Object argument)
           at System.ComponentModel.BackgroundWorker.RunWorkerAsync()

        How to avoid this error?

    2. If I have two
    time-consuming function, I add two BackgroundWorker components,  each Dowork() fucntion includes the relative time-consuming function, but it often only one time-consuming function runs corrently, the other usually failed. I use e.Result in each RunWorkerCompleted().
    How to solve mutiple time-consuming functions?

    Thank you very much!

    kamil
  • Monday, November 09, 2009 9:30 AMsummy00 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi, Geek, I have solve the first error by using BackgroundWorker.IsBusy property.

    But I want to use thread Mutex or thread protection when I use BackgroundWorker to solve the second problem. Do it can implement?

    Thank you!

    kamil