使用C#多线程程序(2)

文档及代码下载: http://www.dingos.cn/index.php?topic=805.0

 

Program 8

using System;
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来捕获所有的异常。

 

Program 9
using System;
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。

程序的运行结果:
Before start thread
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异常没有被捕获。看下面的程序:

Program 10
using System;
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");
    }
}

程序输出:
Before start Thread
End of Main

这个输出结果很明显的表面线程没有执行。一个线程终止后就不能在开始。看下面的程序。

Program 11
using System;
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异常。现在使用异常捕获后关闭程序而不是用异常终止。

 

Program 12

using System;
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结果的引用,看下面的程序。

 

Program 13

using System;
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属性。下面程序显示了这个用法。

 

Program 14

using System;
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");
    }
}


程序输出:

Before start thread
Thread 1=0
Thread 2=0
Thread 1=1
Thread 2=1
End of Main


输出显示当应用程序终止时两个背景线程也都终止。

也可以为线程设置优先权。可以使用Thread类的ThreadPriority属性来设置线程的优先级。下面程序显示使用线程的属性设置有效级。

 

Program 15

using System;
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()方法返回线程执行的执行文件的名。这个静态方法使用类名来访问。

 

Program 16

using System;
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();

    }
}


程序输出结果:

Before start thread
Name: prog16.exe
No context policies.

0
1
2
3
4
5
6
7
8
9


这里介绍了C#多线程程序。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值