網(wǎng)站首頁 編程語言 正文
一、工具類代碼
public class TaskHelper
{
#region 多線程操作
/// <summary>
/// 功能描述:多線程執(zhí)行方法,方法無參數(shù),無返回值
/// </summary>
/// <param name="func">方法,如果方法中調(diào)用了控件,請使用 ThreadInvokerControl(() => { 您的操作})進(jìn)行包括</param>
/// <param name="callback">執(zhí)行完成回調(diào),參數(shù)為object,如果錯(cuò)誤返回的是Exception,否則為null,如果為空則默認(rèn)調(diào)用基類回調(diào)方法</param>
/// <param name="enableControl">調(diào)用線程時(shí)禁用的控件</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>
/// 功能描述:線程默認(rèn)回調(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">所在窗體,默認(rèn)使用當(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 禁用控件時(shí)不改變空間顏色
[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è)置多個(gè)控件的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;
});
});
原文鏈接:https://www.cnblogs.com/wml-it/p/15607362.html
相關(guān)推薦
- 2022-05-20 Spring注入bean的常用的六種方式
- 2022-09-20 Python中類的mro與繼承關(guān)系詳解_python
- 2022-08-08 redis如何實(shí)現(xiàn)保存對象_Redis
- 2021-12-17 Windows下Flutter+Idea環(huán)境搭建及配置_Android
- 2022-01-01 element對穿梭框?qū)涌诜祷氐臄?shù)據(jù)其他字段進(jìn)行校驗(yàn)多個(gè)校驗(yàn)
- 2022-06-06 CSS3動(dòng)畫:wifi 焦點(diǎn)擴(kuò)散漸變
- 2022-09-08 go語言中的協(xié)程詳解_Golang
- 2022-06-29 python中py文件與pyc文件相互轉(zhuǎn)換的方法實(shí)例_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支