使用C#多线程程序(2)
来源:互联网 发布:淘宝死人衣服视频 编辑:程序博客网 时间:2024/06/05 10:23
文档及代码下载: http://www.dingos.cn/index.php?topic=805.0 Program 8 Program 12 Program 13 Program 14 Program 15 Program 16
using System.Threading;
public class MyThread {
public void Thread1() {
for (int i = 0; i < 10; i++) {
Thread thr = Thread.CurrentThread;
Console.WriteLine(thr.Name + "=" + i);
Thread.Sleep(1);
}
}
}
public class MyClass {
public static void Main() {
Console.WriteLine("Before start thread");
MyThread thr1 = new MyThread();
MyThread thr2 = new MyThread();
Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );
tid1.Name = "Thread 1";
tid2.Name = "Thread 2";
tid1.Start();
tid2.Start();
}
}
为了防止应用程序崩溃可以在可能发生异常的地方捕获异常。可能在Sleep方法中抛出异常,可以用try来捕获所有的异常。
using System.Threading;
using System.Security;
public class MyThread {
public void Thread1() {
for (int i = 0; i < 10; i++) {
Thread thr = Thread.CurrentThread;
Console.WriteLine(thr.Name + "=" + i);
try {
Thread.Sleep(1);
} catch (ArgumentException ae) {
Console.WriteLine(ae.ToString() );
} catch (ThreadInterruptedException tie) {
Console.WriteLine(tie.ToString() );
} catch (SecurityException se) {
Console.WriteLine(se.ToString() );
}
}
}
}
public class MyClass {
public static void Main() {
Console.WriteLine("Before start thread");
MyThread thr1 = new MyThread();
MyThread thr2 = new MyThread();
Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );
tid1.Name = "Thread 1";
tid2.Name = "Thread 2";
tid1.Start();
tid2.Start();
}
}
这个程序捕获了三个异常。看这段代码的第一行。多用了一个命名空间:System.Security。SecurityException异常类定义在这个命名空间。
在创建线程实例的方法中为线程命名。可以通过CurrentThread获得线程的名称。CurrentThread是线程类的静态方法。当调用者不适合安全访问这个属性抛出的一个异常:SecurityException。
程序的运行结果:
Thread 1=0
Thread 2=0
Thread 1=1
Thread 2=1
Thread 1=2
Thread 2=2
Thread 1=3
Thread 2=3
Thread 1=4
Thread 2=4
Thread 1=5
Thread 2=5
Thread 1=6
Thread 2=6
Thread 1=7
Thread 2=7
Thread 1=8
Thread 2=8
Thread 1=9
Thread 2=9
可以使用Abort()终止线程。Abort有两个重载方法。第一个是执行没有参数的方法,第二个是带有Object参数的方法。这个方法抛出
ThreadAbortException异常没有被捕获。看下面的程序:
using System.Threading;
using System.Security;
public class MyThread {
public void Thread1() {
for (int i = 0; i < 10; i++) {
Thread thr = Thread.CurrentThread;
Console.WriteLine(thr.Name + "=" + i);
try {
Thread.Sleep(1);
} catch (ArgumentException ae) {
Console.WriteLine(ae.ToString() );
} catch (ThreadInterruptedException tie) {
Console.WriteLine(tie.ToString() );
} catch (SecurityException se) {
Console.WriteLine(se.ToString() );
}
}
}
}
public class MyClass {
public static void Main() {
Console.WriteLine("Before start thread");
MyThread thr1 = new MyThread();
MyThread thr2 = new MyThread();
Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );
tid1.Name = "Thread 1";
tid2.Name = "Thread 2";
tid1.Start();
tid2.Start();
try {
tid1.Abort();
tid2.Abort();
} catch (ThreadAbortException tae) {
Console.WriteLine(tae.ToString() );
}
Console.WriteLine("End of Main");
}
}
程序输出:
End of Main
这个输出结果很明显的表面线程没有执行。一个线程终止后就不能在开始。看下面的程序。
using System.Threading;
public class MyThread {
public void Thread1() {
for (int i = 0; i < 10; i++) {
Thread thr = Thread.CurrentThread;
Console.WriteLine(thr.Name + "=" + i);
Thread.Sleep(1);
}
}
}
public class MyClass {
public static void Main() {
Console.WriteLine("Before start thread");
MyThread thr1 = new MyThread();
MyThread thr2 = new MyThread();
Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );
tid1.Name = "Thread 1";
tid2.Name = "Thread 2";
tid1.Start();
tid2.Start();
tid1.Abort();
tid2.Abort();
Console.WriteLine("After Abort");
tid1.Start();
tid2.Start();
Console.WriteLine("End of Main");
}
}
这个程序抛出一个System.Threading.ThreadStateException异常。现在使用异常捕获后关闭程序而不是用异常终止。
using System.Threading;
public class MyThread {
public void Thread1() {
for (int i = 0; i < 10; i++) {
Thread thr = Thread.CurrentThread;
Console.WriteLine(thr.Name + "=" + i);
Thread.Sleep(1);
}
}
}
public class MyClass {
public static void Main() {
Console.WriteLine("Before start thread");
MyThread thr1 = new MyThread();
MyThread thr2 = new MyThread();
Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );
tid1.Name = "Thread 1";
tid2.Name = "Thread 2";
try {
tid1.Start();
tid2.Start();
} catch (ThreadStateException te) {
Console.WriteLine(te.ToString() );
}
tid1.Abort();
tid2.Abort();
try {
tid1.Start();
tid2.Start();
} catch (ThreadStateException te) {
Console.WriteLine(te.ToString() );
}
Console.WriteLine("End of Main");
}
}
这里捕获ThreadStatException异常并用这个类的ToString方法。ToString方法返回一个异常的全缀名,和可能的错误信息,异常内部名和堆跟踪信息。
可以用Join方法来等待终端的线程。这个方法有三个重载方法。第一个没有参数等待仍然逝去的线程,第二个是带有一个int参数在期待的时间内等待逝去的线程,第三个是带TimeSpan结果的引用,看下面的程序。
using System.Threading;
public class MyThread {
public void Thread1() {
for (int i = 0; i < 10; i++) {
Thread thr = Thread.CurrentThread;
Console.WriteLine(thr.Name + "=" + i);
Thread.Sleep(1);
}
}
}
public class MyClass {
public static void Main() {
Console.WriteLine("Before start thread");
MyThread thr1 = new MyThread();
MyThread thr2 = new MyThread();
Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );
tid1.Name = "Thread 1";
tid2.Name = "Thread 2";
try {
tid1.Start();
tid2.Start();
} catch (ThreadStateException te) {
Console.WriteLine(te.ToString() );
}
tid1.Join();
tid2.Join(new TimeSpan(0, 0, 1) );
Console.WriteLine("End of Main");
}
}
程序等等第一个线程知道他完成为止第二个线程等待1秒。
线程执行有两种方法:后台和前台。当应用程序终止时后台线程完成,另一个前台线程不等的应用程序终止。设置线程执行使用IsBackground属性。下面程序显示了这个用法。
using System.Threading;
public class MyThread {
public void Thread1() {
for (int i = 0; i < 10; i++) {
Thread thr = Thread.CurrentThread;
Console.WriteLine(thr.Name + "=" + i);
Thread.Sleep(1);
}
}
}
public class MyClass {
public static void Main() {
Console.WriteLine("Before start thread");
MyThread thr1 = new MyThread();
MyThread thr2 = new MyThread();
Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );
tid1.Name = "Thread 1";
tid2.Name = "Thread 2";
tid1.IsBackground = true;
tid2.IsBackground = true;
try {
tid1.Start();
tid2.Start();
} catch (ThreadStateException te) {
Console.WriteLine(te.ToString() );
}
Thread.Sleep(10);
Console.WriteLine("End of Main");
}
}
程序输出:
Thread 1=0
Thread 2=0
Thread 1=1
Thread 2=1
End of Main
输出显示当应用程序终止时两个背景线程也都终止。
也可以为线程设置优先权。可以使用Thread类的ThreadPriority属性来设置线程的优先级。下面程序显示使用线程的属性设置有效级。
using System.Threading;
public class MyThread {
public void Thread1() {
for (int i = 0; i < 10; i++) {
Thread thr = Thread.CurrentThread;
Console.WriteLine(thr.Name + "=" + i);
Thread.Sleep(1);
}
}
}
public class MyClass {
public static void Main() {
Console.WriteLine("Before start thread");
MyThread thr1 = new MyThread();
MyThread thr2 = new MyThread();
Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );
tid1.Name = "Thread 1";
tid2.Name = "Thread 2";
tid1.Priority = ThreadPriority.Highest;
tid2.Priority = ThreadPriority.Lowest;
try {
tid1.Start();
tid2.Start();
} catch (ThreadStateException te) {
Console.WriteLine(te.ToString() );
}
tid1.Join();
tid2.Join();
Console.WriteLine("End of Main");
}
}
线程1的优先级是Highest线程2是Lowest。另外线程的优先级是AboveNormal, BelowNormal 和 Normal.
GetDomain()方法返回线程执行的执行文件的名。这个静态方法使用类名来访问。
using System.Threading;
public class MyThread {
public void Thread1() {
Console.WriteLine(Thread.GetDomain() );
for (int i = 0; i < 10; i++) {
Thread thr = Thread.CurrentThread;
Console.WriteLine(i);
}
}
}
public class MyClass {
public static void Main() {
Console.WriteLine("Before start thread");
MyThread thr1 = new MyThread();
Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
tid1.Start();
}
}
程序输出结果:
Name: prog16.exe
No context policies.
0
1
2
3
4
5
6
7
8
9
这里介绍了C#多线程程序。
- 使用C#多线程程序(2)
- 使用C#多线程程序(2)
- 使用C#多线程程序(1)
- 使用C#多线程程序(1)
- C#多线程 BackgroundWorker类使用小例-控制台程序
- C#多线程 BackgroundWorker类使用小例-WPF程序
- c#中多线程---使用多线程
- C#多线程强制退出程序
- C#多线程强制退出程序
- C#多线程的界面程序
- C#多线程强制退出程序
- C#多线程强制退出程序
- C#多线程强制退出程序
- c# 多线程网络聊天程序
- C#多线程强制退出程序
- c#中使用多线程(图)一
- c#中使用多线程(图)
- C#中的线程(三)使用多线程
- SQL Server数据库性能优化技术[转]
- as连接FMS服务器报错状态汇总~~
- 使用C#多线程程序(1)
- 我们工作到底为了什么(这篇文章很重要)
- SMTP
- 使用C#多线程程序(2)
- SQL优化-索引[转]
- NOSQL之旅---HBase
- RHEL 5 非法关机导致文件系统无法加载
- Csharp中的注释
- [翻译]Windows Mobile应用程序设计指导-界面导航【转】
- 网址前加图标
- 认识4G地址空间的局限----MMIO内存映射的问题
- 关于struts2遇见的几个问题的看法