WPF:如何在工作线程中更新窗体的UI元素(Dis
1个回答
展开全部
360问答
如何在工作线程中更新窗体的UI元素
woshirhj LV12
2014-12-07
满意答案
hxmaitt
LV12
2014-12-08
using System; using System.Windows; using System.Threading; namespace WpfApplication1 { /// /// Window1.xaml 的交互逻辑 /// public partial class Window1 : Window { publicWindow1() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { //错误的写法:直接更新 new Thread(() => { progressBar1.Value = 20; }).Start(); } private void button2_Click(object sender, RoutedEventArgs e) { //正确的写法:通知主线程去完成更新 new Thread(()=>{ this.Dispatcher.Invoke(new Action(()=>{ progressBar1.Value=20; })); }).Start(); } } }
如何在工作线程中更新窗体的UI元素
woshirhj LV12
2014-12-07
满意答案
hxmaitt
LV12
2014-12-08
using System; using System.Windows; using System.Threading; namespace WpfApplication1 { /// /// Window1.xaml 的交互逻辑 /// public partial class Window1 : Window { publicWindow1() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { //错误的写法:直接更新 new Thread(() => { progressBar1.Value = 20; }).Start(); } private void button2_Click(object sender, RoutedEventArgs e) { //正确的写法:通知主线程去完成更新 new Thread(()=>{ this.Dispatcher.Invoke(new Action(()=>{ progressBar1.Value=20; })); }).Start(); } } }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询