C#多線程與跨線程訪問界面控件的方法
更新時間:2015年03月03日 11:19:58 作者:尋i
這篇文章主要介紹了C#多線程與跨線程訪問界面控件的方法,實例分析了C#多線程與跨線程訪問空間的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了C#多線程與跨線程訪問界面控件的方法。分享給大家供大家參考。具體分析如下:
在編寫WinForm訪問WebService時,常會遇到因為網(wǎng)絡(luò)延遲造成界面卡死的現(xiàn)象。啟用新線程去訪問WebService是一個可行的方法。
典型的,有下面的啟動新線程示例:
復(fù)制代碼 代碼如下:
private void LoadRemoteAppVersion()
{
if (FileName.Text.Trim() == "") return;
StatusLabel.Text = "正在加載";
S_Controllers_Bins.S_Controllers_BinsSoapClient service = new S_Controllers_Bins.S_Controllers_BinsSoapClient();
S_Controllers_Bins.Controllers_Bins m = service.QueryFileName(FileName.Text.Trim());
if (m != null)
{
//todo:
StatusLabel.Text = "加載成功";
}else
StatusLabel.Text = "加載失敗";
}
private void BtnLoadBinInformation(object sender, EventArgs e)
{
Thread nonParameterThread = new Thread(new ThreadStart(LoadRemoteAppVersion));
nonParameterThread.Start();
}
{
if (FileName.Text.Trim() == "") return;
StatusLabel.Text = "正在加載";
S_Controllers_Bins.S_Controllers_BinsSoapClient service = new S_Controllers_Bins.S_Controllers_BinsSoapClient();
S_Controllers_Bins.Controllers_Bins m = service.QueryFileName(FileName.Text.Trim());
if (m != null)
{
//todo:
StatusLabel.Text = "加載成功";
}else
StatusLabel.Text = "加載失敗";
}
private void BtnLoadBinInformation(object sender, EventArgs e)
{
Thread nonParameterThread = new Thread(new ThreadStart(LoadRemoteAppVersion));
nonParameterThread.Start();
}
運行程序的時候,如果要在線程里操作界面控件,可能會提示不能跨線程訪問界面控件,有兩種處理方法:
1.啟動程序改一下:
復(fù)制代碼 代碼如下:
/// <summary>
/// 應(yīng)用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
/// 應(yīng)用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
2.使用委托
復(fù)制代碼 代碼如下:
public delegate void LoadRemoteAppVersionDelegate(); //定義委托變量
private void BtnLoadBinInformation(object sender, EventArgs e)
{
LoadRemoteAppVersionDelegate func = new LoadRemoteAppVersionDelegate(LoadRemoteAppVersion);//<span style="font-family: Arial, Helvetica, sans-serif;">LoadRemoteAppVersion不用修改</span>
func.BeginInvoke(null, null);
}
private void BtnLoadBinInformation(object sender, EventArgs e)
{
LoadRemoteAppVersionDelegate func = new LoadRemoteAppVersionDelegate(LoadRemoteAppVersion);//<span style="font-family: Arial, Helvetica, sans-serif;">LoadRemoteAppVersion不用修改</span>
func.BeginInvoke(null, null);
}
希望本文所述對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
.net 通過 WebAPI 調(diào)用nsfwjs 進行視頻鑒別功能
這篇文章主要介紹了.net 通過 WebAPI 調(diào)用 nsfwjs 進行視頻鑒別,文末給大家提到了FFMPEG獲取視頻關(guān)鍵幀并保存成jpg圖像的相關(guān)知識,需要的朋友可以參考下2021-09-09
C#實現(xiàn)基于加減按鈕形式控制系統(tǒng)音量及靜音的方法
這篇文章主要介紹了C#實現(xiàn)基于加減按鈕形式控制系統(tǒng)音量及靜音的方法,涉及C#引用user32.dll動態(tài)鏈接庫操作系統(tǒng)音量的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
C#實現(xiàn)的Windows剪貼板監(jiān)視器功能實例【附demo源碼下載】
這篇文章主要介紹了C#實現(xiàn)的Windows剪貼板監(jiān)視器功能,結(jié)合實例形式分析了C#實現(xiàn)剪貼板監(jiān)視功能所涉及的相關(guān)Windows API函數(shù)與使用技巧,需要的朋友可以參考下2016-08-08

