C# 多線程刷新UI


2.利用委托調用--最常見的辦法(僅WinForm有效) 
  

using System; 
using System.Threading; 
using System.Windows.Forms; 

namespace ThreadTest 
{ 
    public partial class Form1 : Form 
    { 
        delegate void D(object obj); 

        public Form1() 
        { 
            InitializeComponent();             
        } 
        

        private void btnSet_Click(object sender, EventArgs e) 
        {            
            Thread t = new Thread(new ParameterizedThreadStart(SetTextBoxValue)); 
            t.Start("Hello World"); 
        } 


        void SetTextBoxValue(object obj)  
        { 
            if (textBox1.InvokeRequired) 
            { 
                D d = new D(); 
                textBox1.Invoke(d,obj); 

            } 
            else  
            { 
                this.textBox1.Text = obj.ToString(); 
            } 
        } 


        void DelegateSetValue(object obj)  
        { 
            this.textBox1.Text = obj.ToString(); 
        } 
    } 
} 
3.利用SynchronizationContext上下文 -- 最神秘的方法(Winform/Silverlight能用) 

之所以說它神秘,是因為msdn官方對它的解釋據說也是不清不楚  
  

using System; 
using System.Threading; 
using System.Windows.Forms; 

namespace ThreadTest 
{ 
    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent();             
        }        

        private void btnSet_Click(object sender, EventArgs e) 
        { 
            Thread t = new Thread(new ParameterizedThreadStart(Run)); 
            MyPram _p = new MyPram() { context = SynchronizationContext.Current, parm = "Hello World" }; 
            t.Start(_p); 
        } 

        void Run(object obj)  
        { 
            MyPram p = obj as MyPram; 
            p.context.Post(SetTextValue, p.parm); 
        } 


        void SetTextValue(object obj)  
        { 
            this.textBox1.Text = obj.ToString(); 
        } 
    } 


    public class MyPram  
    { 
        public SynchronizationContext context { set; get; } 
        public object parm { set; get; } 
    } 
} 
4.利用BackgroundWorker --最偷懶的辦法(Winform/Silverlight通用) 

BackgroundWorker會在主線程之外,另開一個后台線程,我們可以把一些處理放在后台線程中處理,完成之后,后台線程會把結果傳遞給主線程,同時結束自己。 

using System; 
using System.ComponentModel; 
using System.Windows.Forms; 

namespace ThreadTest 
{ 
    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent();             
        }        

        private void btnSet_Click(object sender, EventArgs e) 
        { 
            //MessageBox.Show(Thread.CurrentThread.ManagedThreadId.ToString()); 
            using (BackgroundWorker bw = new BackgroundWorker()) 
            { 
                bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted); 
                bw.DoWork += new DoWorkEventHandler(bw_DoWork); 
                bw.RunWorkerAsync("Hello World"); 
            } 
        } 

        void bw_DoWork(object sender, DoWorkEventArgs e) 
        { 
            //MessageBox.Show(Thread.CurrentThread.ManagedThreadId.ToString()); 
            e.Result = e.Argument;//這里只是簡單的把參數當做結果返回,當然您也可以在這里做復雜的處理后,再返回自己想要的結果(這里的操作是在另一個線程上完成的) 
        } 

        void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
        { 
            //這時后台線程已經完成,並返回了主線程,所以可以直接使用UI控件了 
            this.textBox1.Text = e.Result.ToString(); 
            //MessageBox.Show(Thread.CurrentThread.ManagedThreadId.ToString()); 
        }        
    }     
} 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM