【WPF】窗体调用类中线程(实时)

来源:互联网 发布:中国进口贸易数据分析 编辑:程序博客网 时间:2024/06/05 14:39


最近需要做一个窗体中实时调用类中的一个线程问题,在网上找了很多都不符合需求,终于搞定这个问题,Mark一下要不然时间久了就忘记了~

MainWindow:

Class1 oid = newClass1();

 public MainWindow()

{

  InitializeComponent();

  oid.show += C_show;

 }    


publicvoid C_show(string d)

{

   try{

         this.Dispatcher.Invoke(newAction(() =>

          {MessageBox.Show(d);   }));

      }catch{}

}

privatevoid Button_Click(object sender, RoutedEventArgs e)

{

oid.demo();

}


Class1:


publicdelegatevoidExe(string check); //委托---将类中的线程数据传回主窗体下

publiceventExe show;

int i = 0;

///<summary>

/// 开启获取数据线程 

///</summary>

publicvoid demo()

{

var th1=new Thread(()=>

{

while (true){

show(i++.ToString());}});

th1.SetApartmentState(ApartmentState.STA);

th1.IsBackground =true;

th1.Start();

}






     








               


       










               


           



           




          



           


       



阅读全文
0 0
原创粉丝点击