C# multi-thread start, pause, resume simple example

tags: C#  Multithreading  time out

 


namespace Test
{ 
    
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
            Label.CheckForIllegalCrossThreadCalls = false;
        }
        Thread thread;
        ManualResetEvent ma;
        bool on_off = false;
        bool stop = false;

        private void button1_Click(object sender, EventArgs e)
        {
            thread = new Thread(Runtime);
            thread.Start();
        }


        void Runtime()
        {
            for (int i = 1; i <= 100; i++)
            {
                if (stop)
                    return;
                if (on_off)
                {
                    ma = new ManualResetEvent(false);
                    ma.WaitOne();
                }
                textBox1.AppendText("Timer:" + i + "\r\n");
                Thread.Sleep(100);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            on_off = true;
                         textBox1.AppendText("Pause in :\r\n");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            on_off = false;
            ma.Set();
                         textBox1.AppendText("Continue timing :\r\n");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            stop = true;
                         textBox1.AppendText("stop timing \r\n");
        }
    }
}

 

Intelligent Recommendation

Android control thread pause and resume

Sometimes we encounter problems like this in Android. I want to draw a graph, but I want it to be paused and resumed. First of all, the drawing must use the knowledge of multithreading. Java's Thread ...

Use suspend and resume pause and resume a thread

Use the suspend and resume There are two methods in this class Thread: suspend and resume, the two methods appear in pairs. Role suspend () method is to thread a suspend (pause), Role sucked resume ()...

Android-Multi-thread breakpoint resume download example

Android-Multi-thread breakpoint resume download example Original July 15, 2015 21:02:39 label: android / download / Multithreading / Breakpoint I. Overview In the previous blog post "Android-Mult...

A simple example of C# multi-thread synchronization

An example of a thread is often encountered in development. If a background operation is time consuming, we can start a thread to perform that time-consuming operation while the program continues to e...

A simple example of C multi-thread synchronization

Share the artificial intelligence tutorial of my teacher, God! Zero basis, easy to understand! You are also welcome to reprint this article. Sharing knowledge, benefiting the people and realizing the ...

More Recommendation

Java thread pause and start

2019 Unicorn Enterprise Heavy Glour Recruitment Python Engineer Standard >>> The best way to interrupt the thread is the most recommended way to send a signal using a shared variable, telling...

WPF control animation start, stop, pause and resume

1. Gossip I haven't updated a blog in a long time. I'm a bit lazy, and I'm really busy for a few months. Fan monitoring software, there is such a small demand in the project: when the normal fan is ro...

iOS development-thread simulation pause and resume operation

The table view opens the thread to download the remote network interface, which will inevitably affect the scrolling of the page and reduce the user experience. In response to this situation, when the...

Java thread stop, pause and resume state control

JDK1.0 defines the stop and suspend methods.Stop is used to directly terminate the thread, and suspend will block the thread until another thread calls resume. stop and suspend have something in commo...

Copyright  DMCA © 2018-2023 - All Rights Reserved - www.programmersought.com  User Notice

Top