网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > C#应用
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.SQL查询语句对象化的实现(C#).
.用Visual C#中实现DB2数据库编程.
.借用VB的My,C#照样条条大路通罗.
.提取HTML代码中文字的C#函数.
.从Internet上抓取指定URL的源码的.
.C#分析数据库结构,使用XSL模板自.
.使用C#开发SmartPhone程序入门.
.如何在C#中使用Win32和其他库.
..net中前台javascript与c#后台代.
.用C#动态创建Access数据库.
.使用C# 2.0泛型实现单例模式重用.
.C++与C#混合生成.NET程序 .
.用Visual C#来增加数据记录(2).
.在C#中使用属性控件添加属性窗口.
.C#中ref和out的使用小结.
.用Visual C#来清空回收站(1).
.C#快速排序类.
.浅析.NET开发中代理模式的使用.
.C#合并多个结构一样的Excel.
.像Asp一样轻松分页显示数据(C#) .

在C#中编写多线程应用程序,简单!

发表日期:2004-8-8


来自:www.kunwsoft.com

    以前在使用VB来实现多线程的时候,发现有一定的难度。虽然也有这样那样的方法,但都不尽人意,但在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.

上一篇:在C#应用程序与DLL交互中使用消息 人气:15170
下一篇:如何用C#把Doc文档转换成rtf格式 人气:14574
浏览全部C#的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐