If the thread task class and the thread class are used correctly in C #

Source: Internet
Author: User

Use thread task class and thread class summary in C #

Just contact c#3 months or so, has been using C + + development, because the company's needs, the use of C # development, mainly control the real-time operation of equipment, this is the background.

For task and thread in C # I do not introduce here, to learn more, if you look at the considerable information. Using task and thred in this project, let me debug for nearly two weeks to find out the problem, so write to prevent me as just contact with C #, but also need a thread of real-time requirements of developers some personal summary of considerations.

The 1.Task is suitable for multiprocessor, and I-series multiprocessor.

The 2.Thread is suitable for all processors with higher real-time availability.

Here is my personal test code:

The use of the right or wrong may be my personal understanding of C # thread is not enough to cause, if there is incorrect use of wrong, look Big bird guidance.

        private static void Threadandtasktest () {Stopwatch watch = new Stopwatch (); Watch. Start ();<p>           //Thread threadTest1 = new Thread (() =>           //{            //    Thread.Sleep (;    )        //    Debug.WriteLine ("Thread 1 End consumption time: {0}", watch. Elapsedmilliseconds);           //});            //threadtest1.start ();</p><p>            //thread threadTest2 = new Thread (() =>            //{            //    Thread.Sleep (a);           //     Debug.WriteLine ("Thread 2 End consumption time: {0}", watch. Elapsedmilliseconds);           //});            //threadtest2.start ();</p><p>            //thread threadTest3 = new Thread (() =>            //{            //    thread.sleep (2900);            //    Debug.WriteLine ("Thread 2 End consumption time: {0}", watch. Elapsedmilliseconds);           //});                //threadtest3.start ();</p><p> </p>       var Task1 = Task.Factory.StartNew (() = {thread.sleep (2500); Debug.WriteLine ("Thread 1 End consumption time: {0}", watch.            Elapsedmilliseconds);            });                var Task2 = Task.Factory.StartNew (() = {Thread.Sleep (2700); Debug.WriteLine ("Thread 2 End consumption time: {0}", watch.            Elapsedmilliseconds);            });                var Task3 = Task.Factory.StartNew (() = {Thread.Sleep (2900); Debug.WriteLine ("Thread 3 End consumption time: {0}", watch.            Elapsedmilliseconds);            }); while (watch.                Elapsedmilliseconds <=) {//if (!threadtest.isalive &&!threadtest1.isalive)                    if (task1.iscompleted && task2.iscompleted && task3.iscompleted) { Debug.WriteLine ("Monitoring End consumption time: {0}", watch.)                    Elapsedmilliseconds);                Break } else Thread.sLeep (1); }        }

The results of the task test above are as follows:

Thread 1 End consumption time: 2503
Thread 2 End consumption time: 2703
thread 3 End consumption time: 3944 (theory should be 2905)

The same code uses the thread's Test knot as follows:

Thread 2 End consumption time: 2003
Thread 1 End consumption time: 2002
thread 2 End consumption time: 2905

The above test environment is in:

Processor: Pentium (R) dual-core CPU E6700 @3.20ghxz

Installation memory (RAM): 4.0GB

System type: 32-bit operating system

If you use the i5 series of CPUs, the other hardware environment is the same, there will be no such timeout situation.

There are also some problems: more than 20 threads running at the same time, the real-time difference of the thread is also very large, the same thread function gap 700ms.

If the thread task class and the thread class are used correctly in C #

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.