C#:多线程使用TextBox控件
生活随笔
收集整理的這篇文章主要介紹了
C#:多线程使用TextBox控件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Posted on 2008-06-23 14:39?SmartStone?閱讀(1334)?評論(2)?編輯?收藏? //聲明一個委托
publicdelegatevoidSetTextBoxValue(stringvalue);
????????//委托使用文本框
voidSetMyTextBoxValue(stringvalue)
????????{
????????????//?Control.InvokeRequired?屬性:?獲取一個值,該值指示調用方在對控件進行方法調用時是否必須調用?Invoke?方法,因為調用方位于創建控件所在的線程以外的線程中。當前線程不是創建控件的線程時為true,當前線程中訪問是False
????????????if?(this.TextBoxControl.InvokeRequired)
????????????{
????????????????SetTextBoxValue?objSetTextBoxValue?=?new?SetTextBoxValue(SetMyTextBoxValue);
????????????????//?IAsyncResult?接口:表示異步操作的狀態。不同的異步操作需要不同的類型來描述,自然可以返回任何對象。
????????????????//?Control.BeginInvoke?方法?(Delegate):在創建控件的基礎句柄所在線程上異步執行指定委托。
????????????????IAsyncResult?result?=?this.TextBoxControl.BeginInvoke(objSetTextBoxValue,?new?object[]{?value?});
????????????????try?{
????????????????????objSetTextBoxValue.EndInvoke(result);
????????????????}
????????????????catch?{
????????????????}
????????????}
????????????else
????????????{
????????????????this.TextBoxControl.Text?+=?value?+?Environment.NewLine;
????????????????this.TextBoxControl.SelectionStart?=?this.TextBoxControl.TextLength;
????????????????this.TextBoxControl.ScrollToCaret();
????????????}
????????}
示例代碼:
publicTestForm()
????????{
????????????InitializeComponent();
????????}
????????privatedelegatevoidSetTextBoxValue(stringvalue);
????????privatevoidSetMyTextBoxValue(stringvalue)
????????{
????????????if?(this.TextBoxControl.InvokeRequired)
????????????{
????????????????SetTextBoxValue?objSetTextBoxValue?=?new?SetTextBoxValue(SetMyTextBoxValue);
????????????????IAsyncResult?result?=?this.TextBoxControl.BeginInvoke(objSetTextBoxValue,?new?object[]?{?value?});
????????????????try
????????????????{
????????????????????objSetTextBoxValue.EndInvoke(result);
????????????????}
????????????????catch
????????????????{
????????????????}
????????????}
????????????else
????????????{
????????????????this.TextBoxControl.Text?+=?value?+?Environment.NewLine;
????????????????this.TextBoxControl.SelectionStart?=?this.TextBoxControl.TextLength;
????????????????this.TextBoxControl.ScrollToCaret();
????????????}
????????}
????????privatevoidExecuteNewThread()
????????{
????????????for?(int?i?=?0;?i?<?1000;?i++)
????????????{
????????????????SetMyTextBoxValue(i.ToString());
????????????}
????????}
????????privatevoidNewThreadButton_Click(objectsender,?EventArgs?e)
????????{
????????????Thread?objThread?=?new?Thread(new?ThreadStart(ExecuteNewThread));
????????????objThread.IsBackground?=?true;
????????????objThread.Start();
????????}
????}
publicdelegatevoidSetTextBoxValue(stringvalue);
????????//委托使用文本框
voidSetMyTextBoxValue(stringvalue)
????????{
????????????//?Control.InvokeRequired?屬性:?獲取一個值,該值指示調用方在對控件進行方法調用時是否必須調用?Invoke?方法,因為調用方位于創建控件所在的線程以外的線程中。當前線程不是創建控件的線程時為true,當前線程中訪問是False
????????????if?(this.TextBoxControl.InvokeRequired)
????????????{
????????????????SetTextBoxValue?objSetTextBoxValue?=?new?SetTextBoxValue(SetMyTextBoxValue);
????????????????//?IAsyncResult?接口:表示異步操作的狀態。不同的異步操作需要不同的類型來描述,自然可以返回任何對象。
????????????????//?Control.BeginInvoke?方法?(Delegate):在創建控件的基礎句柄所在線程上異步執行指定委托。
????????????????IAsyncResult?result?=?this.TextBoxControl.BeginInvoke(objSetTextBoxValue,?new?object[]{?value?});
????????????????try?{
????????????????????objSetTextBoxValue.EndInvoke(result);
????????????????}
????????????????catch?{
????????????????}
????????????}
????????????else
????????????{
????????????????this.TextBoxControl.Text?+=?value?+?Environment.NewLine;
????????????????this.TextBoxControl.SelectionStart?=?this.TextBoxControl.TextLength;
????????????????this.TextBoxControl.ScrollToCaret();
????????????}
????????}
示例代碼:
publicTestForm()
????????{
????????????InitializeComponent();
????????}
????????privatedelegatevoidSetTextBoxValue(stringvalue);
????????privatevoidSetMyTextBoxValue(stringvalue)
????????{
????????????if?(this.TextBoxControl.InvokeRequired)
????????????{
????????????????SetTextBoxValue?objSetTextBoxValue?=?new?SetTextBoxValue(SetMyTextBoxValue);
????????????????IAsyncResult?result?=?this.TextBoxControl.BeginInvoke(objSetTextBoxValue,?new?object[]?{?value?});
????????????????try
????????????????{
????????????????????objSetTextBoxValue.EndInvoke(result);
????????????????}
????????????????catch
????????????????{
????????????????}
????????????}
????????????else
????????????{
????????????????this.TextBoxControl.Text?+=?value?+?Environment.NewLine;
????????????????this.TextBoxControl.SelectionStart?=?this.TextBoxControl.TextLength;
????????????????this.TextBoxControl.ScrollToCaret();
????????????}
????????}
????????privatevoidExecuteNewThread()
????????{
????????????for?(int?i?=?0;?i?<?1000;?i++)
????????????{
????????????????SetMyTextBoxValue(i.ToString());
????????????}
????????}
????????privatevoidNewThreadButton_Click(objectsender,?EventArgs?e)
????????{
????????????Thread?objThread?=?new?Thread(new?ThreadStart(ExecuteNewThread));
????????????objThread.IsBackground?=?true;
????????????objThread.Start();
????????}
????}
轉載于:https://www.cnblogs.com/zhangchenliang/archive/2012/04/12/2443789.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的C#:多线程使用TextBox控件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何在MFC客户端调用COM DLL
- 下一篇: arcgis jsapi接口入门系列(7