C#线程调用_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > C#线程调用

C#线程调用

 2014/7/21 17:46:09  陶潜别梦  程序员俱乐部  我要评论(0)
  • 摘要:一.线程的优点1.服务器一般负载的情况下线程可以提高效率;2.使用线程执行的代码出现故障不会影响主程序,提高程序稳定和可靠性。二.线程的创建及其常用属性1.线程创建ThreadStartts1=newThreadStart(function2);//线程定义执行Threadt1=newThread(ts1);t1.Start();或者Threadt1=newThread(newThreadStart(function2));t1.Start();2.线程常用属性Priority获取或设置
  • 标签:C# 线程

class="p0">一.线程的优点

1.服务器 一般负载的情况下 线程可以提高效率;

2.使用线程执行的代码出现故障不会影响主程序,提高程序稳定和可靠性。

 

二.线程的创建及其常用属性

1.线程创建

ThreadStart ts1 = new ThreadStart(function2);//线程定义 执行                                       

Thread t1 = new Thread(ts1);

t1.Start();

 或者

Thread t1=new Thread(new ThreadStart(function2));  

t1.Start();  

2.线程常用属性

Priority 获取或设置,线程优先级

IsBackgroud 获取或设置,是否为后台线程

Abort() :终止线程

Start() 开始线程

三.程序实例 

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Thread2
{
    class Program
    {
        static void function1()
        {
            for (int i = 0; i < 40; i++)
            {
                Console.WriteLine(i);
            }
        }
        static void function2()
        {
            for (int i = 41; i <200; i++)
            {
               
                Console.WriteLine(i);
                int c = 2;
                int x = 0;
                int y = c / x;
            }
        }
        static void function3()
        {
            for (int i = 200; i < 205; i++)
            {
                Console.WriteLine(i);
                //int c = 2;
                //int x = 0;
                //int y = c / x;


            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Main begin !");

            ThreadStart ts = new ThreadStart(function1);//线程定义 执行
            Thread t = new Thread(ts);
            t.Priority = ThreadPriority.Highest;
            t.Start();

            Console.WriteLine("Main end!");

            ThreadStart ts1 = new ThreadStart(function2);//线程定义 执行
            Thread t1 = new Thread(ts1);
            //t1.Priority = ThreadPriority.Highest;
            //t1.IsBackground = true;//没执行完 主程序也退出
            t1.Start();

            function3();//直接执行

            Console.WriteLine("Main1111 end!");
        }


    }
}

 

上一篇: 小米发布会终极预告片:年轻 发烧 美好 下一篇: 没有下一篇了!
发表评论
用户名: 匿名