Restarting a thread in .NET (using C#)

后端 未结 7 2307
再見小時候
再見小時候 2020-12-06 01:28

I\'m looking for a way to restart a thread that has been stopped by Abort()..

public partial class MyProgram : Form
{
  private Thread MyThread = new Thread(         


        
相关标签:
7条回答
  • 2020-12-06 01:43

    The easiest way is to not abort the thread.

    0 讨论(0)
  • 2020-12-06 01:47

    I really don't understand why people provide information if they do not know that is correct.. How can a real programmer suspend or stop processing a thread for sometime and then release it and thereby making the code vulnerable... @Brad-- m sorry.. but your idea was not good.. @Rhythmic - You need to work on your way to approach things..

    BFree was somewhat right if you people got him the same way he wanted to say.. You just need to re-declare that..

    below is the example:

    Public Shared Sub ResetAbort()
    
        Dim ThreadPleaseWait As New Thread(New ThreadStart(AddressOf YourSubName))
    
        YourThreadName.Start()
    
        Thread.Sleep(2000)
    
        YourThreadName.Abort()
    
    End Sub
    

    Now you can use this Sub anywhere you want to start the thread. It will automatically abort the thread.

    If you want to start the thread on Button1_click() event and stop it on Button2_Click() event use this:

    in Button1_click() event

        Dim ThreadPleaseWait As New Thread(New ThreadStart(AddressOf YourSubName))
    
        YourThreadName.Start()
    

    in Button2_click() event

        YourThreadName.Start()
    

    doing this way you will abort you thread where ever you want and will initialize it again. You can also use YourThreadName.ThreadState.Running property to check if the thread is running or not(Just to avoid multiple instances of the same thread.....

    0 讨论(0)
  • 2020-12-06 01:50

    The simple answer is, you can't. Once a thread has been aborted, you can't restart it. Just create a method or something, that returns a Thread object just how you need it. When you need a new Thread, just get it from that method.

    0 讨论(0)
  • 2020-12-06 01:50

    If you really need to interrupt the thread function and resume, you should set a condition and then check it periodically during processing.

    That would allow you to stop processing for some amount of time and then resume.

    I've used events and Wait calls to accomplish a similar task.

    0 讨论(0)
  • 2020-12-06 01:51

    No, there isn't, but why would you want to? Just start up a new thread, with the same ThreadStart, and the same parameter (if any).

    0 讨论(0)
  • 2020-12-06 01:57

    Once you have aborted your thread, you cannot start it again.

    But your actual problem is that you are aborting your thread. You should never use Thread.Abort().

    If your thread should be paused and continued several times, you should consider using other mechanisms (like AutoResetEvent, for example).

    [EDIT]

    The simplest solution to abort a thread, as mentioned by Ian Griffiths in the link above, is:

    The approach I always recommend is dead simple. Have a volatile bool field that is visible both to your worker thread and your UI thread. If the user clicks cancel, set this flag. Meanwhile, on your worker thread, test the flag from time to time. If you see it get set, stop what you're doing.

    The only thing that you need to do to make it work properly, is to rearrange your background method so that it runs in a loop - so that you can periodically check if your flag has been set by a different thread.

    If you need to have pause and resume functionality for the same worker thread, instead of the simple volatile bool flag approach, you could go for a slightly more complex approach, a synchronizing construct such as AutoResetEvent. These classes also provide a way to put the worker thread to sleep for a specified (or indefinite) amount of time between signals from the non-worker thread.

    This thread contains a concrete example with Start, Pause, Resume and Stop methods. Note how Brannon's example never aborts the thread. It only fires an event, and then waits until the thread finishes gracefully.

    0 讨论(0)
提交回复
热议问题