1. 开始一个Thread
开始一个Thread很简单,声明一个Thread实例,然后调用Start方法即可
Thread.Start
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->Thread threadA = new Thread(new ThreadStart(WorkMethod));
threadA.Start();
Thread threadB = new Thread(new ParameterizedThreadStart(WorkMethodWithParam));
threadB.Start(obj);
2. Thread.Join阻塞调用线程,直到某个线程终止为止。
我按照msdn上的解释的理解是:
threadA.Join()方法是用来阻塞threadA线程,直到在threadA开始执行之后启动的线程执行完毕(或者Sleep了)之后,才开始执行threadA线程的方法
但是事实并非如我所想,下面是msdn上的例子,先声明了一个thread实例,然后将一个方法放入ThreadPool中执行,并调用thread的Join方法以阻塞thread的执行。显示结果在预料之中
MSDN Thread.Join例子
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->using System;
using System.Threading;
class IsThreadPool
{
static void Main()
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
Thread regularThread =
new Thread(new ThreadStart(ThreadMethod));
regularThread.Start();
ThreadPool.QueueUserWorkItem(new WaitCallback(WorkMethod),
autoEvent);
// Wait for foreground thread to end.
regularThread.Join();
// Wait for background thread to end.
autoEvent.WaitOne();
}
static void ThreadMethod()
{
Console.WriteLine("ThreadOne, executing ThreadMethod, " +
"is {0}from the thread pool.",
Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");
}
static void WorkMethod(object stateInfo)
{
Console.WriteLine("ThreadTwo, executing WorkMethod, " +
"is {0}from the thread pool.",
Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");
// Signal that this thread is finished.
((AutoResetEvent)stateInfo).Set();
}
}
执行结果是:ThreadTwo, executing WorkMethod, is from the thread pool.
ThreadOne, executing ThreadMethod, is not from the thread pool.
但是当我对这个例子稍作修改之后就出现了不可理解的问题,我在WorkMethod中加上Thread.Sleep让线程池中的线程休眠2秒,这样操作之后执行结果是:
执行结果
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->ThreadTwo, executing WorkMethod, is from the thread pool.
Sleep
ThreadOne, executing ThreadMethod, is not from the thread pool.
Sleep end
可以看出当线程池中的线程Sleep之后,被Join的thread即获得执行权,开始执行,这个似乎和msdn的解释有出入
另外我还做了一个测试,把另一个线程用Thread启动,而不是放在线程池中执行,看是否可以正常的阻塞线程,但是结果却是出人意料的,不能按预期阻塞,请看下面的实例代码:
Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> class IsThreadPool
{
static void Main()
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
Thread threadA =
new Thread(new ThreadStart(ThreadMethod));
Thread threadB = new Thread(new ParameterizedThreadStart(WorkMethod));
threadA.Start();
// Wait for foreground thread to end.
threadA.Join();
threadB.Start(autoEvent);
// Wait for background thread to end.
autoEvent.WaitOne();
Console.Read();
}
static void ThreadMethod()
{
Console.WriteLine("ThreadOne, executing ThreadMethod, " +
"is {0}from the thread pool.",
Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");
}
static void WorkMethod(object stateInfo)
{
Console.WriteLine("ThreadTwo, executing WorkMethod, " +
"is {0}from the thread pool.",
Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");
// Signal that this thread is finished.
((AutoResetEvent)stateInfo).Set();
}
}
不知道什么原因,上面的代码中threadA并没有被阻塞,难道是只能阻塞放在ThreadPool中执行的线程?没有道理,我反复做了实验都没有成功,把这个结果贴出来,请大家释疑。
分享到:
相关推荐
.Net 3.5 下使用的 System.Threading.Tasks。...安装完成后,添加引用时只需要在安装目录 C:\Program Files (x86)\Microsoft Reactive Extensions\Redist\DesktopV2 下找到 System.Threading.dll,添加即可
.Net 3.5支持Plinq的相关信息已被微软撤掉了, 而引用此dll可以使3.5的Linq支持AsParallel()方法, 3.5可以使用1.0.3058.34407版本。...using System.Threading.Tasks; using System.Threading; using System.Linq;
system.Threading.Timer的使用
本文实例讲述了C#中Forms.Timer、Timers.Timer、Threading.Timer的用法分析,分享给大家供大家参考。具体分析如下: 在.NET Framework里面提供了三种Timer ① System.Windows.Forms.Timer ② System.Timers.Timer ③...
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN"); System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("zh-...
我在做一个兼容WindowsXP项目时用到,用梯子到外面找来的。 由于 .NET 3.5下并没有官方实现的 Task 库,所以,是通过 VS 中 NuGet 取得的 非官方 实现的 Task 库,调用接口与官方.NET 4.0 后的应该是差不多的。
• System.Windows.Threading.DispatcherObject类:WPF 中的大多数对象是从 DispatcherObject 派生的,这提供了用于处理并发和线程的基本构造。WPF 基于调度程序实现的消息系统。 • System.Windows....
System.Diagnostics.Process的简单包装器,将其公开为System.Threading.Tasks.zip
AsyncAwaitBestPractices System.Threading.Tasks.Task扩展。 受( )的博客文章启发:。AsyncAwaitBestPractices 在NuGet上可用: : SafeFireAndForget 一种安全触发并忘记Task或ValueTask的扩展方法确保Task将重新...
System.Threading.Timer 代码 启动 已 注释 后台线程对住线程UI的写法
一组扩展,用于优化/简化System.Threading.Channels的用法。 强调 读写 具有可选的并发级别。 读取通道中的所有条目。 将所有条目从源写入通道。 将所有条目管道传输(使用)到缓冲区(通道)。 .NET Standard ...
本篇文章是对C#利用System.Threading.Thread.Sleep即时输出信息进行了详细的分析介绍,需要的朋友参考下
多加线程点,主线程的成功调用
AsyncConsoleDemo_2 System.Threading.Tasks,异步任务,Task.Run(),等待,Console.ReadKey(),Console.KeyAvailable
有时候运行项目莫名其妙的报缺少Microsoft.Threading.Tasks.dll的引用,直接下载这个引用就好了
3)....Fixed: spawned by ecc32/emake processes now start with the same priority 4)....Fixed: ThreadID = 0 in StandardEurekaNotify 5)....Fixed: Dialog auto-close timer may reset without user input 6)......
Devexpress官方的汉化文件,通过资源文件进行汉化,适用于原版或适用Danny Su的DevExpress.Registration注册插件安装的版本,和原版一致的强名,支持9.1各个版本汉化。 使用方法: 1、zh-CHS目录拷贝到生成的EXE程序...
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN"); System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(...
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN"); System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("zh-...