C#子线程向UI线程发送消息

1、定义

        SynchronizationContext syncContext = null;

2、注册UI线程

        public Form1()
        {
            InitializeComponent();

            syncContext = SynchronizationContext.Current;

        }

3、在子线程发送消息

                syncContext.Post(SetTextSafePost, "舱单已生成");

4、定义回调,此回调可以调用UI组件

        private void SetTextSafePost(object text) 
        {
            msgText.AppendText(text.ToString() + "\r\n");
        }


猜你喜欢

转载自blog.csdn.net/wzj0808/article/details/79149166
今日推荐