c# 子線程與主線程通訊二

以前寫過使用線程上下文實現線程同步,今天利用子線程向主線程發送事件,實現子線程與主線程的同步web

基本步驟函數

一、定義類ui

using System;this

using System.Collections.Generic;spa

using System.Linq;線程

using System.Text;orm

 

namespace DX_equip.Class事件

{ip

    /// <summary>ci

    /// 定義一個信息委託

    /// </summary>

    /// <param name="sender">發佈者</param>

    /// <param name="msg">發送內容</param>

    public delegate void DxMsgDlg(object sender,object msg);

    public class DxMessageHelper

    {

        /// <summary>

        /// 消息發送事件

        /// </summary>

        public static event DxMsgDlg EventSend;

        public static void DxSendMessage(object sender, object msg)

        {

            if (EventSend != null)//

            {

                EventSend(sender, msg);

            }

        }

    }

}

 

二、在子線程中發送事件,實現事件通知功能

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading;

using System.Diagnostics;

using System.Xml;

using System.Data;

using System.Windows.Forms;

 

namespace DX_equip.Class

{

    public class WebServiceConnect

    {

        private static bool circle = false;

        private static Thread taskThread;

        public static string getTaskMsg = "";

        public static int connCount = 0;

 

        private static SynchronizationContext mainThreadSynContext;

 

        public static void GetTaskInfo(string taskInfoIn0)

        {

            mainThreadSynContext = SynchronizationContext.Current;

            taskThread = new Thread(new ParameterizedThreadStart(webConnect));

            taskThread.Start(taskInfoIn0);

        }

 

        public static void webConnect(object taskInfoIn)

        {

            getTaskMsg = "";

            try

            {

                string taskInfoIn0 = taskInfoIn.ToString();

                TestWebReference.TDMTestTaskInterfaceServiceService taskInfoService = new TestWebReference.TDMTestTaskInterfaceServiceService();

                string taskName = SystemInfoClass.systemBaseInfo.taskName;

                getTaskMsg = taskInfoService.queryTaskInfo(taskInfoIn0, taskName);

 

                StackTrace st = new StackTrace();

                string callName = st.GetFrame(1).GetMethod().Name;

                DxMessageHelper.DxSendMessage(callName,getTaskMsg);

            }

            catch (Exception ex)

            {

                System.Net.WebException wex = ex as System.Net.WebException;

                System.Windows.Forms.MessageBox.Show("通ª¡§訊?未¡ä連¢?接¨®!ê?");

            }

        }

三、接收事件函數中註冊事件

        public MainForm()

        {

            InitializeComponent();

      

            DxMessageHelper.EventSend += new DxMsgDlg(Receive_DxEventSend);

 

                   }

 

        private void Receive_DxEventSend(object sender, object msg)

        {

            this.Invoke(new Action(() =>

            {

                labTaskMsg.Visible = true;

                labTaskMsg.Text = msg.ToString();

            }));

            Trace.WriteLine("***Info*** " + msg.ToString());

        }

相關文章
相關標籤/搜索