版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第C#多线程异步执行和跨线程访问控件Helper////summary
///paramname="func"方法,如果方法中调用了控件,请使用ThreadInvokerControl(()={您的操作})进行包括/param
///paramname="callback"执行完成回调,参数为object,如果错误返回的是Exception,否则为null,如果为空则默认调用基类回调方法/param
///paramname="enableControl"调用线程时禁用的控件/param
publicstaticvoidTaskRun(
Formfrm,
FuncTaskfunc,
Actionobjectcallback=null,
Control[]enableControl=null)
if(enableControl!=null)
SetControlEnableds(enableControl,false);
Task.Factory.StartNew(()=
Tasktask=func();
if(task.Exception!=nulltask.Exception.InnerException!=null)
throwtask.Exception.InnerException;
callback.Invoke(null);
catch(Exceptionex)
if(callback!=null)
callback(ex);
else
ThreadBaseCallBack(frm,ex);
finally
if(enableControl!=nullfrm!=null)
ThreadInvokerControl(frm,()={SetControlEnableds(enableControl,true);});
///summary
///功能描述:线程默认回调方法
////summary
publicstaticvoidThreadBaseCallBack(Formfrm,Exceptionex)
if(frm!=null)
ThreadInvokerControl(frm,()=
ExceptionlastEx=ex.GetOriginalException();
MessageBox.Show(lastEx.Message);
catch
///summary
///功能描述:委托调用,用于夸线程访问控件
////summary
///paramname="action"action/param
///paramname="f"所在窗体,默认使用当前窗体/param
publicstaticvoidThreadInvokerControl(Formfrm,Actionaction)
if(frm!=null)
if(frm.InvokeRequired)
frm.BeginInvoke(action);
else
action();
#endregion
#region提示层
[DllImport("user32.dll")]
privatestaticexternboolSetForegroundWindow(IntPtrhWnd);
privatestaticvoidShowProcessPanel(Controlparent,stringstrMessage)
if(parent.InvokeRequired)
parent.BeginInvoke(newMethodInvoker(delegate
ShowProcessPanel(parent,strMessage);
}));
else
parent.VisibleChanged-=newEventHandler(parent_VisibleChanged);
parent.VisibleChanged+=newEventHandler(parent_VisibleChanged);
parent.FindForm().FormClosing-=ControlHelper_FormClosing;
parent.FindForm().FormClosing+=ControlHelper_FormClosing;
Controlcontrol=null;
lock(parent)
control=HaveProcessPanelControl(parent);
if(control==null)
control=CreateProgressPanel();
parent.Controls.Add(control);
FWaitingfWaiting=control.TagasFWaiting;
fWaiting.Message=strMessage;
fWaiting.Show();
privatestaticvoidControlHelper_FormClosing(objectsender,FormClosingEventArgse)
Controlcontrol=senderasControl;
control.FindForm().FormClosing-=ControlHelper_FormClosing;
CloseWaiting(control);
privatestaticvoidparent_VisibleChanged(objectsender,EventArgse)
Controlcontrol=senderasControl;
control.VisibleChanged-=newEventHandler(parent_VisibleChanged);
if(!control.Visible)
CloseWaiting(control);
privatestaticvoidCloseWaiting(Controlcontrol)
Control[]array=control.Controls.Find("myProgressPanelext",false);
if(array.Length0)
ControlmyProgress=array[0];
if(myProgress.Tag!=nullmyProgress.TagisFWaiting)
FWaitingfWaiting=myProgressasFWaiting;
if(fWaiting!=null!fWaiting.IsDisposedfWaiting.Visible)
fWaiting.Hide();
privatestaticvoidCloseProcessPanel(Controlparent)
if(parent.InvokeRequired)
parent.BeginInvoke(newMethodInvoker(delegate
CloseProcessPanel(parent);
}));
elseif(parent!=null)
Controlcontrol=HaveProcessPanelControl(parent);
if(control!=null)
Formfrm=control.TagasForm;
if(frm!=null!frm.IsDisposedfrm.Visible)
if(frm.InvokeRequired)
frm.BeginInvoke(newMethodInvoker(delegate
frm.Hide();
}));
else
frm.Hide();
privatestaticControlHaveProcessPanelControl(Controlparent)
Control[]array=parent.Controls.Find("myProgressPanelext",false);
Controlresult;
if(array.Length0)
result=array[0];
else
result=null;
returnresult;
privatestaticControlCreateProgressPanel()
returnnewLabel
Name="myProgressPanelext",
Visible=false,
Tag=newFWaiting
TopMost=true,
#endregion
#region禁用控件时不改变空间颜色
[System.Runtime.InteropServices.DllImport("user32.dll")]
privatestaticexternintSetWindowLong(IntPtrhWnd,intnIndex,intwndproc);
[System.Runtime.InteropServices.DllImport("user32.dll")]
privatestaticexternintGetWindowLong(IntPtrhWnd,intnIndex);
privateconstintGWL_STYLE=-16;
privateconstintWS_DISABLED=0x8000000;
///summary
///功能描述:设置控件的Enable属性,控件不改颜色
////summary
///paramname="c"c/param
///paramname="enabled"enabled/param
privatestaticvoidSetControlEnabled(Controlc,boolenabled)
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
///功能描述:设置多个控件的Enable属性,控件不改颜色
////summary
///paramname="cs"cs/param
///paramname="enabled"enabled/param
privatestaticvoidSetControlEnableds(Control[]cs,boolenabled)
foreach(varcincs)
SetControlEnabled(c,enabled);
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年河南省登封市高三生物上册期末考试真题及答案
- 河北昌黎荒佃庄 2026 毛皮产业监管岗公务员招录理论考卷 招聘 5 人
- 宁波慈溪周巷 2026 制造业服务岗公务员招录理论考卷 招聘 8 人
- 2026年银行对公客户经理真题及参考答案
- 云南保山隆阳 2026 文旅综合岗公务员招录测评试题 招聘 3 人
- 工业园区应急队伍联训联演方案
- 2025年特种设备作业人员考试能力提高训练试题库及答案
- 重庆巴南 2026 年汽车零部件配套操作工入职理论试题 招聘 66 人
- 银川兴庆区 2026 年行政区后勤服务岗入职测评试卷 招聘 29 人
- 大型沥青基球形活性炭活化炉改造项目环境影响评价报告
- 2026年物业管理服务人员知识培训考试题库附答案
- 排水管道工程现场检测报告
- 2026年碳排放管理师考试题库及参考答案解析
- 钛铁矿开发项目可行性研究报告
- 空调安装工程施工方案及方法
- 第3章人文地理学的研究方法-人文地理学.课件-课件
- 儿童暴发性心肌炎诊治核心2026
- 2026高处安装、维护、拆除作业题库及答案
- 采煤工作面处理顶板冒落安全技术措施培训
- 物业中控外包合同
- AQ 3026-2026《化工企业设备检修作业安全规范》解读课件
评论
0/150
提交评论