c# 中的线程和同步

一、新建线程的3种方法

  a)异步委托;b)Thread类;c)线程池;

二、异步委托

  1、简单使用,检查委托是否完成其任务 

    a) 通过 BeginInvoke()  的返回值IAsyncResult 中的成员IsCompleted判断

    b)通过 BeginInvoke()  的返回值IAsyncResult 中的成员AsyncWaitHandle.WaitOne(50,false) 函数判断

    c)通过异步回调判断

  2、获取返回值

    通过EndInvoke 函数获取

三、Thread类

  1、简单使用

  2、给线程传递数据(可以将执行的耗时函数放到一个类中,通过类成员变量传递参数)

四、线程池 (ThreadPool 类来管理线程) 

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ThreadExam
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public delegate int AsyncDelegate(int data, int ms);

        static int timeConsumingWork(int data, int ms)
        {
            return 0;
        }
        static void workForThread(object data)
        {
            Debug.WriteLine(data);
        }
        static void resultCompleted(IAsyncResult ar)
        {
            int result = (ar.AsyncState as AsyncDelegate).EndInvoke(ar);
            Debug.WriteLine(result);
        }
        private void async_Click(object sender, RoutedEventArgs e)
        {
            Button asyBtn = sender as Button;
            switch (asyBtn.Name)
            {
                case "async1":
                    AsyncDelegate asyDeleg = timeConsumingWork;
                    IAsyncResult ar = asyDeleg.BeginInvoke(1, 3000, null, null);
                    while (!ar.IsCompleted)  //一直判断状态
                    {
                        Console.Write(".");
                        Thread.Sleep(50);
                    } 
                    int result = asyDeleg.EndInvoke(ar);
                    Debug.WriteLine(result);
                    break;
                case "async2":
                    AsyncDelegate asyDeleg2 = timeConsumingWork;
                    IAsyncResult ar2 = asyDeleg2.BeginInvoke(1, 3000, null, null);
                    while (true)
                    {
                        Console.Write(".");
                        if (ar2.AsyncWaitHandle.WaitOne(50, false))  //等待50毫秒后看状态
                        {
                            break;
                        }
                    } 
                    int result2 = asyDeleg2.EndInvoke(ar2);
                    Debug.WriteLine(result2);
                    break;
                case "async3":
                    AsyncDelegate asyDeleg3 = timeConsumingWork;
                    asyDeleg3.BeginInvoke(1, 2000, resultCompleted, asyDeleg3);
                    break;
                case "thread1":
                    new Thread(workForThread).Start(100000);
                    break;
                case "pool1":
                    for (int i = 0; i < 100;i++ )
                        ThreadPool.QueueUserWorkItem(workForThread, 123);
                    break;
            }
        }
    }
}
View Code

 链接:http://pan.baidu.com/s/1boXqVvx 密码:hqc3

参考:http://www.cnblogs.com/nokiaguy/archive/2008/07/13/1241817.html

  

posted @ 2016-09-26 13:30  lwn6  阅读(221)  评论(0编辑  收藏  举报
什么是幸福?天天在做自己想做的事情,家人、同事、朋友、客户、网友都和和睦睦,身体健康、钱包鼓鼓、女朋友天天开心、生活无忧无虑就是最大的幸福