C # Training 2019-10-11 multithreading thread waiting

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_41617697/article/details/102501776

Multithreading:

Considering the multi-threading is because in doing animation within Trahan, when the number of mobile becomes large or move a long time, will form the phenomenon of stuck, until the end of the movement will be displayed.
Find information about Discovery and thread (or multiple threads can be resolved).

1, thread creation

//创建线程
Thread thread = new Thread( new ParameterizedThreadStart( AnimateDiskMove ) );
	
//启动线程
thread.Start();

//强制关闭线程
thread.Abort();


2, the method needs to pass into the value of the thread

For example: Method thread of the call is void AddNumber (int na , int nb)
to provide a method of
use stated ParameterizedThreadStartbelow

Thread thread = new Thread( new ParameterizedThreadStart( AnimateMovement ) );

When this call, you can use

			int y= 10;
			int[] xyArray = new int[] { x,y};
			thread.Start( xyArray );

When you define a method, passing in as object类型

void AddNumber ( object coord)
{
}

3, when the child thread to form the operating space Form

In the void Form1_Load( object sender, EventArgs e )case adding the following code, which is not cross-thread call to check whether the form

			// other thread can control Forms
			Control.CheckForIllegalCrossThreadCalls = false;

4, when to wait for the child thread is finished before continuing to execute when the main thread.

First, the definition of a lock in vitro method

object locker = new object();

Then call the sub-thread of Start( )the method add the following code:
which finishcountrepresents the number of sub-thread execution, will be written in the sub-thread execution method last;
_ThreadCountrepresents the main thread to wait several sub-thread

            lock (locker)
            {
                while (finishcount != _ThreadCount)
                {
                    Monitor.Wait(locker);//等待
                }
            }

Finally, the last to join in the process of child threads in:

lock (locker)
            {
                finishcount++;
                Monitor.Pulse(locker); //完成,通知等待队列,告知已完,执行下一个。
            }

Note: For multiple calls, you will need to finishcountbe cleared.
The complete code is as follows:

using System.Threading;

namespace ThreadStudy
{
    class StopAllSubThread
    {
        int _ThreadCount = 5;
        int finishcount = 0;
        object locker = new object();
        public void Main()
        {
            for (int i = 0; i < _ThreadCount; i++)
            {
                Thread trd = new Thread(new ParameterizedThreadStart(ThreadMethod));
                trd.Start(i);
            }
            lock (locker)
            {
                while (finishcount != _ThreadCount)
                {
                    Monitor.Wait(locker);//等待
                }
            }
            Console.WriteLine("Thread Finished!");
        }

        private void ThreadMethod(object obj)
        {
            //模拟执行程序
            Thread.Sleep(3000);
            Console.WriteLine("Thread execute at {0}", obj.ToString());
            lock (locker)
            {
                finishcount++;
                Monitor.Pulse(locker); //完成,通知等待队列,告知已完,执行下一个。
            }
        }
    }
}

4, the thread is terminated ( Abort()) can not start up again Start()( )

可以先挂起suspend,再resume()

Guess you like

Origin blog.csdn.net/qq_41617697/article/details/102501776