C# 多线程 举例使用

使用多线程的几种方式

(1)不需要传递参数,也不需要返回参数

ThreadStart是一个委托,这个委托的定义为void ThreadStart(),没有参数与返回值。

    /// <summary>
        /// 连接
        /// </summary>
        /// <param name="host">服务器名称</param>
        /// <param name="port">服务器端口</param>
        public void Connect(string host, int port)
        {
            try
            {
                clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                clientSock.Connect(host, port);
                if (OnClientConn != null)
                {
                    Session clientSession = new Session(clientSock);
                    OnClientConn(this, new NetEventArgs(clientSession));
                }
                Thread th = new Thread(new ThreadStart(ReceiveDataThread)); //也可简写为new Thread(ThreadMethod);              
                th.Start(); //启动线程
               
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw;
            }
        }

    /// <summary>
        /// 从服务器接收数据
        /// </summary>
        private  void ReceiveDataThread()
        {
            while (clientSock != null)
            {
                try
                {
                    Session clientSession = new Session(clientSock);

                    // Receives data from a bound Socket.
                    int bytesRec = clientSock.Receive(recvDataBuffer);

                    // Converts byte array to string
                    clientSession.Datagram = this.recvCoder.GetEncodingString(recvDataBuffer, bytesRec);

                    // Continues to read the data till data isn‘t available
                    while (clientSock.Available > 0)
                    {
                        bytesRec = clientSock.Receive(recvDataBuffer);
                        clientSession.Datagram += this.recvCoder.GetEncodingString(recvDataBuffer, bytesRec);
                    }
                    if(OnDataRecv!=null)
                        OnDataRecv(this, new NetEventArgs(clientSession));
                }
                catch (Exception ex)
                {
                    DisConnect();
                    Debug.WriteLine(ex.Message);
                }
            }
        }

(2)使用匿名方法(常用)

使用匿名方法启动线程可以有多个参数和返回值,而且使用非常方便!

      /// <summary>
        /// 弹出情报板消息框
        /// </summary>
        /// <param name="name">情报板的名称</param>
        /// <param name="msg">要显示的信息</param>
        private void _ucinfoboardview_OnShowAlert(string name, string msg)
        {
             this.ThreadMsnStart(new ThreadStart(delegate()
             {
                 this.ThreadInfoBoard(name, msg);
             }));
        }

    /// <summary>
        /// 执行多线程弹窗
        /// </summary>
        /// <param name="threadstart">ThreadStart 委托</param>
        private void ThreadMsnStart(ThreadStart threadstart)
        {
            Thread tst = new Thread(threadstart);
            tst.Start();
        }

   /// <summary>
        /// 线程入口 -- 情报板发布信息提示
        /// </summary>
        /// <param name="name">情报板的名称</param>
        /// <param name="msg">情报板提示信息</param>
        private void ThreadInfoBoard(string name, string msg)
        {
                ClsMsgForMSN tt = new ClsMsgForMSN();
                tt.MyParentControl = this;
                tt.InfoBoardName = name;
                tt.InfoBoardMsg = msg;
                //定义一个委托实例,该实例执行打开窗口代码  
                MethodInvoker mi = new MethodInvoker(tt.ShowInfoBoardMsg);
                BeginInvoke(mi);
        }

(3)使用委托开启多线程(多线程深入)

1、用委托(Delegate)的BeginInvoke和EndInvoke方法操作线程

BeginInvoke方法可以使用线程异步地执行委托所指向的方法。然后通过EndInvoke方法获得方法的返回值(EndInvoke方法的返回值就是被调用方法的返回值),或是确定方法已经被成功调用。

    class Program

    {

        private delegate int NewTaskDelegate(int ms);

        private static int newTask(int ms)

        {

            Console.WriteLine("任务开始");

            Thread.Sleep(ms);

            Random random = new Random();

            int n = random.Next(10000);

            Console.WriteLine("任务完成");

            return n;

        }

        static void Main(string[] args)

        {

            NewTaskDelegate task = newTask;

            IAsyncResult asyncResult = task.BeginInvoke(2000, null, null);

            //EndInvoke方法将被阻塞2秒

            int result = task.EndInvoke(asyncResult);

            Console.WriteLine(result);

            Console.Read();

        }

    }

2、使用IAsyncResult.IsCompleted属性来判断异步调用是否完成

    class Program

    {

        private delegate int NewTaskDelegate(int ms);

        private static int newTask(int ms)

        {

            Console.WriteLine("任务开始");

            Thread.Sleep(ms);

            Random random = new Random();

            int n = random.Next(10000);

            Console.WriteLine("任务完成");

            return n;

        }

        static void Main(string[] args)

        {

            NewTaskDelegate task = newTask;

            IAsyncResult asyncResult = task.BeginInvoke(2000, null, null);

            //等待异步执行完成

            while (!asyncResult.IsCompleted)

            {

                Console.Write("*");

                Thread.Sleep(100);

            }

            // 由于异步调用已经完成,因此, EndInvoke会立刻返回结果

            int result = task.EndInvoke(asyncResult);

            Console.WriteLine(result);

            Console.Read();

        }

    }

3、使用WaitOne方法等待异步方法执行完成

   WaitOne的第一个参数表示要等待的毫秒数,在指定时间之内,WaitOne方法将一直等待,直到异步调用完成,并发出通知,WaitOne方法才返 回true。当等待指定时间之后,异步调用仍未完成,WaitOne方法返回false,如果指定时间为0,表示不等待,如果为-1,表示永远等待,直到 异步调用完成。

    class Program

    {

        private delegate int NewTaskDelegate(int ms);

        private static int newTask(int ms)

        {

            Console.WriteLine("任务开始");

            Thread.Sleep(ms);

            Random random = new Random();

            int n = random.Next(10000);

            Console.WriteLine("任务完成");

            return n;

        }

        static void Main(string[] args)

        {

            NewTaskDelegate task = newTask;

            IAsyncResult asyncResult = task.BeginInvoke(2000, null, null);

            //等待异步执行完成

            while (!asyncResult.AsyncWaitHandle.WaitOne(100, false))

            {

                Console.Write("*");

            }

            int result = task.EndInvoke(asyncResult);

            Console.WriteLine(result);

            Console.Read();

        }

    }

4、使用回调方式返回结果

要注意的是“my.BeginInvoke(3,300, MethodCompleted, my)”,BeginInvoke方法的参数传递方式:

前面一部分(3,300)是其委托本身的参数。

倒数第二个参数(MethodCompleted)是回调方法委托类型,他是回调方法的委托,此委托没有返回值,有一个IAsyncResult类型的参数,当method方法执行完后,系统会自动调用MethodCompleted方法。

最后一个参数(my)需要向MethodCompleted方法中传递一些值,一般可以传递被调用方法的委托,这个值可以使用IAsyncResult.AsyncState属性获得。

    class Program

    {

        private delegate int MyMethod(int second, int millisecond);

        //线程执行方法

        private static int method(int second, int millisecond)

        {

            Console.WriteLine("线程休眠" + (second * 1000 + millisecond) + "毫秒");

            Thread.Sleep(second * 1000 + millisecond);

            Random random = new Random();

            return random.Next(10000);

        }

        //回调方法

        private static void MethodCompleted(IAsyncResult asyncResult)

        {

            if (asyncResult == null || asyncResult.AsyncState == null)

            {

                Console.WriteLine("回调失败!!!");

                return;

            }

            int result = (asyncResult.AsyncState as MyMethod).EndInvoke(asyncResult);

            Console.WriteLine("任务完成,结果:" + result);

        }

        static void Main(string[] args)

        {

            MyMethod my = method;

            IAsyncResult asyncResult = my.BeginInvoke(3,300, MethodCompleted, my);

            Console.WriteLine("任务开始");

            Console.Read();

        }

    }

5、其他组件的BeginXXX和EndXXX方法

在其他的.net组件中也有类似BeginInvoke和EndInvoke的方法,如System.Net.HttpWebRequest类的BeginGetResponse和EndGetResponse方法。其使用方法类似于委托类型的BeginInvoke和EndInvoke方法,例如:

    class Program

    {

        //回调函数

        private static void requestCompleted(IAsyncResult asyncResult)

        {

            if (asyncResult == null || asyncResult.AsyncState==null)

            {

                Console.WriteLine("回调失败");

                return;

            }

            HttpWebRequest hwr = asyncResult.AsyncState as HttpWebRequest;

            HttpWebResponse response = (HttpWebResponse)hwr.EndGetResponse(asyncResult);

            StreamReader sr = new StreamReader(response.GetResponseStream());

            string str = sr.ReadToEnd();

            Console.WriteLine("返回流长度:"+str.Length);

        }

        static void Main(string[] args)

        {

            HttpWebRequest request =

(HttpWebRequest)WebRequest.Create("http://www.baidu.com");

            //异步请求

            IAsyncResult asyncResult = request.BeginGetResponse(requestCompleted, request);

            Console.WriteLine("任务开始");

            Console.Read();

        }

    }

C# 多线程 举例使用

上一篇:iOS推送小结(证书的生成、客户端的开发、服务端的开发)


下一篇:时隔一年,再议代码提取