C#多線程異步執(zhí)行和跨線程訪問控件Helper
更新時間:2022年04月24日 08:36:35 作者:農(nóng)碼一生
這篇文章介紹了C#多線程異步執(zhí)行和跨線程訪問控件Helper,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
一、工具類代碼
public class TaskHelper
{
#region 多線程操作
/// <summary>
/// 功能描述:多線程執(zhí)行方法,方法無參數(shù),無返回值
/// </summary>
/// <param name="func">方法,如果方法中調(diào)用了控件,請使用 ThreadInvokerControl(() => { 您的操作})進行包括</param>
/// <param name="callback">執(zhí)行完成回調(diào),參數(shù)為object,如果錯誤返回的是Exception,否則為null,如果為空則默認調(diào)用基類回調(diào)方法</param>
/// <param name="enableControl">調(diào)用線程時禁用的控件</param>
public static void TaskRun(
Form frm,
Func<Task> func,
Action<object> callback = null,
Control[] enableControl = null)
{
if (enableControl != null)
{
SetControlEnableds(enableControl, false);
}
Task.Factory.StartNew(() =>
{
try
{
Task task = func();
if (task.Exception != null && task.Exception.InnerException != null)
throw task.Exception.InnerException;
callback?.Invoke(null);
}
catch (Exception ex)
{
if (callback != null)
callback(ex);
else
ThreadBaseCallBack(frm, ex);
}
finally
{
if (enableControl != null && frm != null)
ThreadInvokerControl(frm, () => { SetControlEnableds(enableControl, true); });
}
});
}
/// <summary>
/// 功能描述:線程默認回調(diào)方法
/// </summary>
public static void ThreadBaseCallBack(Form frm, Exception ex)
{
if (frm != null)
{
ThreadInvokerControl(frm, () =>
{
try
{
Exception lastEx = ex.GetOriginalException();
MessageBox.Show(lastEx.Message);
}
catch
{
}
});
}
}
/// <summary>
/// 功能描述:委托調(diào)用,用于夸線程訪問控件
/// </summary>
/// <param name="action">action</param>
/// <param name="f">所在窗體,默認使用當(dāng)前窗體</param>
public static void ThreadInvokerControl(Form frm, Action action)
{
if (frm != null)
{
if (frm.InvokeRequired)
{
frm.BeginInvoke(action);
}
else
{
action();
}
}
}
#endregion
#region 提示層
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private static void ShowProcessPanel(Control parent, string strMessage)
{
if (parent.InvokeRequired)
{
parent.BeginInvoke(new MethodInvoker(delegate
{
ShowProcessPanel(parent, strMessage);
}));
}
else
{
parent.VisibleChanged -= new EventHandler(parent_VisibleChanged);
parent.VisibleChanged += new EventHandler(parent_VisibleChanged);
parent.FindForm().FormClosing -= ControlHelper_FormClosing;
parent.FindForm().FormClosing += ControlHelper_FormClosing;
Control control = null;
lock (parent)
{
control = HaveProcessPanelControl(parent);
if (control == null)
{
control = CreateProgressPanel();
parent.Controls.Add(control);
}
}
FWaiting fWaiting = control.Tag as FWaiting;
fWaiting.Message = strMessage;
fWaiting.Show();
}
}
private static void ControlHelper_FormClosing(object sender, FormClosingEventArgs e)
{
Control control = sender as Control;
control.FindForm().FormClosing -= ControlHelper_FormClosing;
CloseWaiting(control);
}
private static void parent_VisibleChanged(object sender, EventArgs e)
{
Control control = sender as Control;
control.VisibleChanged -= new EventHandler(parent_VisibleChanged);
if (!control.Visible)
{
CloseWaiting(control);
}
}
private static void CloseWaiting(Control control)
{
Control[] array = control.Controls.Find("myProgressPanelext", false);
if (array.Length > 0)
{
Control myProgress = array[0];
if (myProgress.Tag != null && myProgress.Tag is FWaiting)
{
FWaiting fWaiting = myProgress as FWaiting;
if (fWaiting != null && !fWaiting.IsDisposed && fWaiting.Visible)
{
fWaiting.Hide();
}
}
}
}
private static void CloseProcessPanel(Control parent)
{
if (parent.InvokeRequired)
{
parent.BeginInvoke(new MethodInvoker(delegate
{
CloseProcessPanel(parent);
}));
}
else if (parent != null)
{
Control control = HaveProcessPanelControl(parent);
if (control != null)
{
Form frm = control.Tag as Form;
if (frm != null && !frm.IsDisposed && frm.Visible)
{
if (frm.InvokeRequired)
{
frm.BeginInvoke(new MethodInvoker(delegate
{
frm.Hide();
}));
}
else
{
frm.Hide();
}
}
}
}
}
private static Control HaveProcessPanelControl(Control parent)
{
Control[] array = parent.Controls.Find("myProgressPanelext", false);
Control result;
if (array.Length > 0)
{
result = array[0];
}
else
{
result = null;
}
return result;
}
private static Control CreateProgressPanel()
{
return new Label
{
Name = "myProgressPanelext",
Visible = false,
Tag = new FWaiting
{
TopMost = true,
}
};
}
#endregion
#region 禁用控件時不改變空間顏色
[System.Runtime.InteropServices.DllImport("user32.dll ")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int wndproc);
[System.Runtime.InteropServices.DllImport("user32.dll ")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
private const int GWL_STYLE = -16;
private const int WS_DISABLED = 0x8000000;
/// <summary>
/// 功能描述:設(shè)置控件的Enable屬性,控件不改顏色
/// </summary>
/// <param name="c">c</param>
/// <param name="enabled">enabled</param>
private static void SetControlEnabled(Control c, bool enabled)
{
if (enabled)
{
SetWindowLong(c.Handle, GWL_STYLE, (~WS_DISABLED) & GetWindowLong(c.Handle, GWL_STYLE));
}
else
{
SetWindowLong(c.Handle, GWL_STYLE, WS_DISABLED + GetWindowLong(c.Handle, GWL_STYLE));
}
}
/// <summary>
/// 功能描述:設(shè)置多個控件的Enable屬性,控件不改顏色
/// </summary>
/// <param name="cs">cs</param>
/// <param name="enabled">enabled</param>
private static void SetControlEnableds(Control[] cs, bool enabled)
{
foreach (var c in cs)
{
SetControlEnabled(c, enabled);
}
}
#endregion
}
二、調(diào)用代碼
TaskHelper.TaskRun(this, async () =>
{
TaskHelper.ThreadInvokerControl(this, () =>
{
//夸線程訪問控件的
this.btnStart.Enabled = true;
this.btnStart.BackColor = Color.Gainsboro;
});
});到此這篇關(guān)于C#多線程異步執(zhí)行和跨線程訪問控件Helper的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#/VB.NET實現(xiàn)在 Word 中插入水印?
這篇文章主要介紹了C#/VB.NET實現(xiàn)在 Word 中插入水印,水印是指在 Word 文檔的背景中以淡色或灰色顯示的文本或圖像。文章圍繞主題展開介紹,需要的朋友可以參考一下2022-08-08
C#中Invoke和BeginInvoke區(qū)別小結(jié)
有時候,我們不得不跨線程調(diào)用主界面的控件來進行操作,所以為了方便的解決問題,.net為我們提供了Invoke?與beginInvoke,那么Invoke和BeginInvoke區(qū)別在哪,本文就來詳細的介紹一下2023-08-08
Unity3D實現(xiàn)物體旋轉(zhuǎn)縮放移動效果
這篇文章主要為大家詳細介紹了Unity3D實現(xiàn)物體旋轉(zhuǎn)縮放移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02
C# Lambda表達式及Lambda表達式樹的創(chuàng)建過程
這篇文章主要介紹了C# Lambda表達式及Lambda表達式樹的創(chuàng)建過程,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02

