C#同时执行方法
下面是我学习留下的参考资料 也是以前刚学习时网上搜的
.NET将关于多线程的功能定义在System.Threading名字空间中。因此,要使用多线程,必须先声明引用此名字空间(using System.Threading;)。
即使你没有编写多线程应用程序的经验,也可能听说过“启动线程”“杀死线程”这些词,其实除了这两个外,涉及多线程方面的还有诸如“暂停线程”“优先级”“挂起线程”“恢复线程”等等。下面将一个一个的解释。
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.
.NET将关于多线程的功能定义在System.Threading名字空间中。因此,要使用多线程,必须先声明引用此名字空间(using System.Threading;)。
即使你没有编写多线程应用程序的经验,也可能听说过“启动线程”“杀死线程”这些词,其实除了这两个外,涉及多线程方面的还有诸如“暂停线程”“优先级”“挂起线程”“恢复线程”等等。下面将一个一个的解释。
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.
佚名
2024-06-03 06:07:58
类似问题(10)
-
佚名2024-06-03 08:00:00
问合伙公司,法人和营业执照是同一个人行吗?有什么利害
答可以的,公司法定代表人一般是一人,股东的权益,在工商局登记。 “法人”实为“法人代表人”,法定代表人只是对外代表公司,如果要追究责任的话,直接看营业执照上的负...
-
佚名2024-06-03 08:00:00
问民间借贷纠纷,法院也判决。请问强制执行申请交到法院要多少时间法院才执行?
答您好,这个是需要时间的一般程序是您提交了申请之后,法院会在一个星期之内给您发一个受理通知书,通知书上面会安排有执行法官,随后您就需要等执行法官那边的具体操作情况...
-
佚名2024-06-03 08:00:00
问一个淘宝C店,能不能卖同行业,不同类目的东西?
答你好,这样真卖不了的 ,确实会对降低权重 流量有影响的,你说同属于同个类目,但是卖手机壳又卖厨房电器的 ,东西太杂 没人会久留的 ,感觉乱这样的店铺
-
佚名2024-06-03 08:00:00
问两夫妻创业时,办理执照,南方出钱女方没有,营业执照法人代表写谁的名字
答个体户就谁出钱写谁的, 有限公司就无所谓,公司的话法人不重要,股东才重要。
-
佚名2024-06-03 08:00:00
问法院执行和个人要账问题
答法院是可以强制执行的,变卖对方财产来补偿原告损失,这是合法的,希望你别采取个人要账,到时候有理变成没理,事情就大了
-
佚名2024-06-03 08:00:00
问C#中多个线程调用同一个方法,方法的变量是公有变量,怎样进行方法的调用使各个线程互不影响。
答最简单的方法,把这个变量加锁,或者把函数方法加锁,使其同一时间只能被一个线程调用。但这样牺牲了效率,所以首先你还是得思考以下问题:1.这个变量值会被其他线程改变...
-
佚名2024-06-03 08:00:00
问求 c# 创建多线程的作用和方法
答首先不是为了多线程而多线程,多线程会极大的带来额外的出错的几率。C#中第一个打开窗口的线程是主线程,也是处理UI的线程,最好保持这个线程通畅,即不要有阻塞操作,...
-
佚名2024-06-03 08:00:00
问强制执行十二小时上班时间找谁说,河南省驻马店南方中天纺织厂,合同签订为八小时,非要叫上十二小时,
答在驻马店这就对了,毕竟骗子窝
-
佚名2024-06-03 08:00:00
问MFC应用程序如何调用C#类库中方法????
答C#生成的程序集要为COM互操作注册的。
-
佚名2024-06-03 08:00:00
问国际象棋有王和车同时一起走的方法么
答你小弟走的不合乎规则,是不正确的。即便是费舍尔的任意国际象棋也不会出现那个走法,建议你小弟了解下基本招法
风水
起名
网名
- 1 乌鲁木齐王跃网名冻结爱
- 2 微信网名女人盼
- 3 腾牛网男生网名霸气.繁体字
- 4 简单简洁情侣网名男生
- 5 爱我的人改网名
- 6 二次元动漫昵称
- 7 怎么把照片放在微信昵称里
- 8 快手昵称 霸道女
- 9 适合司机用的昵称
- 10 qq昵称可以更改字体吗
说说
- 1 七雄争霸个性签名
- 2 男生孤独个性签名
- 3 陈广辉个性签名
- 4 求晚安说说
- 5 纹身qq说说
- 6 怕长大了兄弟几个各奔东西的说说
- 7 谈谈灵说说怪2
- 8 qq队列说说
- 9 说说傅雷推崇世说新语的原因是什么
- 10 提醒同事出轨的说说