C # thread suspend and resume solution

Source: Internet
Author: User
C # thread suspend and resume solution

Yesterday, the teacher to each of us to make a program on the hacker, think about a day to find the port scan tool seems to be good to write points, from yesterday to today's basic almost finished, to everyone look at the effect, don't laugh at me oh Oh (~ ~)

Figure 1 Port Scanner

There are some parts of this program that are not very satisfying, that is, when the scan is paused and continues to be implemented, Using Thread.Suspend and Thread.Resume, and these two methods, in the VS2010 hint is outdated, not recommended, online access to some information, found that there is an event notification method is very good, the general principle of event notification is that the thread in the execution of the pause, wait until the other thread notification to continue to execute Go on, this really can play a pause and continue the effect. However, this pause is passive, what I need is active pauses, that is, click the button, the thread pauses, and then the button is clicked, and the thread continues to execute.

Finally, I think of a more alternative way, the general idea is as follows: or by the way of event notification, wait for the notification in the thread, until the notification to continue to execute, and the main thread (form thread) using a timer System.Windows.Forms.Timer to keep the notification thread, If the timer interval is set to a small enough amount, there is basically no pause at all. At this point, the program's pause and continue to implement is very simple, I believe you have thought, as long as the control timer by the Stop () and start () can control the thread's pause and continue.

Here is a demo below:

Run Screenshots:

Figure 2 Demo Running effect

C # source code:

Using System;
Using System.Windows.Forms;

Using System.Threading;  Namespace thread pause and continue implementation {public partial class Form1:form {//Timer private System.Windows.Forms.Timer
        TM = new System.Windows.Forms.Timer (); Automatic reset event class//The main use of its two methods WaitOne () and Set (), the former blocking the current thread, which notifies the blocking thread to continue to execute autoresetevent autoevent = new Autores

        Etevent (FALSE);
            Public Form1 () {InitializeComponent ();
            Progressbar.checkforillegalcrossthreadcalls = false; Tm.
            Interval = 1; Tm.
        Tick + = new EventHandler (Tm_tick);
        }//Timer event void Tm_tick (object sender, EventArgs e) {autoevent.set ();//notify blocked thread to continue execution //Start private void btnStart_Click (object sender, EventArgs e) {TM.

            Start ();
            Thread t = new thread (DoWork);
        T.start (); Methods performed in//Threads private void DoWork () {WHIle (progressBar1.Value < Progressbar1.maximum) {progressbar1.performstep ();  Autoevent.waitone (); 
        Blocks the current thread, waits for notification to continue executing}//pause private void Btnsuspend_click (object sender, EventArgs e) {TM.
        Stop (); //Continue private void Btnresume_click (object sender, EventArgs e) {TM.
        Start ();
 }
    }
}


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.