工作线程操作UI线程元素的方法
2017-04-26 15:20
204 查看
C#中,工作线程无法直接在线程函数中操作UI主线程中的UI对象,必须通过线程切换到UI线程去执行相应的操作界面元素的代码。
主要有两种方法可以在工作线程中完成UI界面元素的操作:
1.Control.Invoke
2.SynchronizationContext
下面的例子同时使用了这两个方法做演示(SynchronizationContext也可以使用Send来调用UI线程中的方法委托)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WF_SynchronizationContext_Test
{
public partial class Form1 : Form
{
private SynchronizationContext sc = null;
public delegate void ShowText(object o);
public Form1()
{
InitializeComponent();
Thread.CurrentThread.Name = "mainthread";
sc = SynchronizationContext.Current;
}
public void ShowStatus(object o)
{
this.label1.Text = string.Format("in sub thread:{0}", Thread.CurrentThread.Name);
}
private void button1_Click(object sender, EventArgs e)
{
ShowText showTxt = new ShowText(ShowStatus);
Thread thread = new Thread(o=>
{
Thread.CurrentThread.Name = "subthread";
Form1 f = o as Form1;
if (f != null)
{
f.Invoke(showTxt);
string s = Thread.CurrentThread.Name;
}
});
thread.IsBackground = true;
thread.Start(this);
}
private void button2_Click(object sender, EventArgs e)
{
Thread thread = new Thread(o =>
{
Thread.CurrentThread.Name = "subthread";
SynchronizationContext sc = o as SynchronizationContext;
if (sc != null)
{
sc.Post(ShowStatus, null);
}
string s = Thread.CurrentThread.Name;
});
thread.IsBackground = true;
thread.Start(this.sc);
}
}
}
主要有两种方法可以在工作线程中完成UI界面元素的操作:
1.Control.Invoke
2.SynchronizationContext
下面的例子同时使用了这两个方法做演示(SynchronizationContext也可以使用Send来调用UI线程中的方法委托)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WF_SynchronizationContext_Test
{
public partial class Form1 : Form
{
private SynchronizationContext sc = null;
public delegate void ShowText(object o);
public Form1()
{
InitializeComponent();
Thread.CurrentThread.Name = "mainthread";
sc = SynchronizationContext.Current;
}
public void ShowStatus(object o)
{
this.label1.Text = string.Format("in sub thread:{0}", Thread.CurrentThread.Name);
}
private void button1_Click(object sender, EventArgs e)
{
ShowText showTxt = new ShowText(ShowStatus);
Thread thread = new Thread(o=>
{
Thread.CurrentThread.Name = "subthread";
Form1 f = o as Form1;
if (f != null)
{
f.Invoke(showTxt);
string s = Thread.CurrentThread.Name;
}
});
thread.IsBackground = true;
thread.Start(this);
}
private void button2_Click(object sender, EventArgs e)
{
Thread thread = new Thread(o =>
{
Thread.CurrentThread.Name = "subthread";
SynchronizationContext sc = o as SynchronizationContext;
if (sc != null)
{
sc.Post(ShowStatus, null);
}
string s = Thread.CurrentThread.Name;
});
thread.IsBackground = true;
thread.Start(this.sc);
}
}
}
相关文章推荐
- android4.0以上访问网络不能在主线程中进行以及在线程中操作UI的解决方法
- android-----在子线程中更新UI操作的方法
- wpf调用外部线程操作ui方法
- Android 子线程操作更新UI方法
- Spark PG3. RDD 操作一 基础 ,放入方法,闭包,输出元素, 使用 Key-Value 工作
- Android开发之子线程操作UI的几种方法
- 4种在线程中操作UI的方法
- Silverlight端费时操作不阻塞UI线程处理方法
- 308_子线程操作UI方法
- 在子线程中操作Ui的几种小方法
- 关于子线程里不能更新UI操作的解决方法
- winform线程间操作UI的五种方法
- android 访问网络不能在主线程中进行以及在线程中操作UI的解决方法
- !!c# 委托与多线程 -- 相比background来说,本方法针对当需要通过异步代理发起UI操作,而后线程中进行sleep操作的时使用
- 工作线程与UI线程交互的方法列举
- 工作线程(耗时操作)与UI线程实现异步更新
- C#跨线程操作控件的线程安全方法
- 关于线程间操作无效的处理方法
- Java 线程的基本概念 创建方法 和 基本操作
- UI与逻辑的关系、主线程与工作线程的通讯及对观察者模式、消息总线机制的思考