c#多線程更新視窗(winform)GUI的資料

來源:互聯網
上載者:User

標籤:winform   http   使用   io   資料   for   ar   art   

1. 在.net framwork 2.0中,可以通過以下代碼來實現:

1 2 3 4 5 6 7 8 9 10 11 12 private delegate void SetControlPropertyThreadSafeDelegate(Control control, string propertyName, object propertyValue); public static void SetControlPropertyThreadSafe(Control control, string propertyName, object propertyValue) {    if (control.InvokeRequired)    {      control.Invoke( new SetControlPropertyThreadSafeDelegate(SetControlPropertyThreadSafe), new object [] { control, propertyName, propertyValue });    }    else    {      control.GetType().InvokeMember(propertyName, BindingFlags.SetProperty, null , control, new object [] { propertyValue });    } }

  調用方式如下:  

1 2 3 // thread-safe equivalent of // myLabel.Text = status; SetControlPropertyThreadSafe(myLabel, "Text" , status);

2.在.net 3.0或者更新的版本中,你可以重寫上面的方法作為一個Control類的擴充方法,可以簡化調用方式,具體代碼如下:  

1 myLabel.SetPropertyThreadSafe( "Text" , status);

 在.net 3.0以上的版本完整的調用步驟如下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 private delegate void SetPropertyThreadSafeDelegate<TResult>(Control @ this , Expression<Func<TResult>> property, TResult value); public static void SetPropertyThreadSafe<TResult>( this Control @ this , Expression<Func<TResult>> property, TResult value) {    var propertyInfo = (property.Body as MemberExpression).Member as PropertyInfo;    if (propertyInfo == null ||        [email protected] this .GetType().IsSubclassOf(propertyInfo.ReflectedType) ||        @ this .GetType().GetProperty(propertyInfo.Name, propertyInfo.PropertyType) == null )    {      throw new ArgumentException( "The lambda expression ‘property‘ must reference a valid property on this Control." );    }    if (@ this .InvokeRequired)    {      @ this .Invoke( new SetPropertyThreadSafeDelegate<TResult>(SetPropertyThreadSafe), new object [] { @ this , property, value });    }    else    {      @ this .GetType().InvokeMember(propertyInfo.Name, BindingFlags.SetProperty, null , @ this , new object [] { value });    } }

   通過使用LINQ和lambda運算式使代碼更加簡潔:  

1 myLabel.SetPropertyThreadSafe(() => myLabel.Text, status); // status has to be a string or this will fail to compile

3.最簡單的匿名方法調用:

1 2 3 4 5 6 ///...blah blah updating files string newText = "abc" ; // running on worker thread this .Invoke((MethodInvoker) delegate {      someLabel.Text = newText; // runs on UI thread }); ///...blah blah more updating files

以上方法搜集自網路,僅供參考。


轉載請註明:文章轉載自:[169IT-最新最全的IT資訊]
本文標題:c#多線程更新視窗(winform)GUI的資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.