温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C# .NET多线程应用举例分析

发布时间:2021-12-03 10:14:08 来源:亿速云 阅读:127 作者:iii 栏目:编程语言

这篇文章主要介绍“C# .NET多线程应用举例分析”,在日常操作中,相信很多人在C# .NET多线程应用举例分析问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C# .NET多线程应用举例分析”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

a.启动线程

顾名思义,“启动线程”就是新建并启动一个线程的意思,如下代码可实现:   

Thread thread1 = new Thread(new ThreadStart( Count));

其中的 Count 是将要被新线程执行的函数。

b.杀死线程

“杀死线程”就是将一线程斩草除根,为了不白费力气,在杀死一个线程前***先判断它是否还活着(通过 IsAlive 属性),然后就可以调用 Abort 方法来杀死此线程。

c.暂停线程

它的意思就是让一个正在运行的线程休眠一段时间。如 thread.Sleep(1000); 就是让线程休眠1秒钟。

d.优先级

这个用不着解释了。Thread类中有一个ThreadPriority属性,它用来设置优先级,但不能保证操作系统会接受该优先级。一个线程的优先级可分为5种:Normal, AboveNormal, BelowNormal, Highest, Lowest。具体实现例子如下: 

thread.Priority = ThreadPriority.Highest;

e.挂起线程

Thread类的Suspend方法用来挂起线程,知道调用Resume,此线程才可以继续执行。如果线程已经挂起,那就不会起作用。

if (thread.ThreadState = ThreadState.Running)  {       thread.Suspend();  }

f.恢复线程

用来恢复已经挂起的线程,以让它继续执行,如果线程没挂起,也不会起作用。

if (thread.ThreadState = ThreadState.Suspended)  {       thread.Resume();  }

下面将列出一个例子,以说明简单的线程处理功能。此例子来自于帮助文档。

using System;  using System.Threading;   // Simple threading scenario:  Start a static method running  // on a second thread.  public class ThreadExample {      // The ThreadProc method is called when the thread starts.      // It loops ten times, writing to the console and yielding      // the rest of its time slice each time, and then ends.      public static void ThreadProc() {          for (int i = 0; i <  10; i++) {              Console.WriteLine("ThreadProc: {0}", i);              // Yield the rest of the time slice.              Thread.Sleep(0);          }      }       public static void Main() {          Console.WriteLine("Main thread: Start a second thread.");          // The constructor for the Thread class requires a ThreadStart          // delegate that represents the method to be executed on the          // thread.  C# simplifies the creation of this delegate.          Thread t = new Thread(new ThreadStart(ThreadProc));          // Start ThreadProc.  On a uniprocessor, the thread does not get          // any processor time until the main thread yields.  Uncomment          // the Thread.Sleep that follows t.Start() to see the difference.          t.Start();          //Thread.Sleep(0);           for (int i = 0; i <  4; i++) {              Console.WriteLine("Main thread: Do some work.");              Thread.Sleep(0);          }           Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");          t.Join();          Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");          Console.ReadLine();      }  }

此代码产生的输出类似如下内容:

Main thread: Start a second thread.  Main thread: Do some work.  ThreadProc: 0  Main thread: Do some work.  ThreadProc: 1  Main thread: Do some work.  ThreadProc: 2  Main thread: Do some work.  ThreadProc: 3  Main thread: Call Join(), to wait until ThreadProc ends.  ThreadProc: 4  ThreadProc: 5  ThreadProc: 6  ThreadProc: 7  ThreadProc: 8  ThreadProc: 9  Main thread: ThreadProc.Join has returned.  Press Enter to end program.

到此,关于“C# .NET多线程应用举例分析”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI