在CS模式开发中一般我们需要用到大量的线程来处理比较耗时的操作,以防止界面假死带来不好的体验效果,下面我将我定义的线程基类给大家参考下,如有问题欢迎指正。
基类代码
1 #region 方法有返回值 2 /// <summary> 3 /// 功能描述:多线程执行方法,方法有返回值 4 /// 作 者:huangzh 5 /// 创建日期:2017-03-29 17:44:26 6 /// 任务编号:MES 7 /// </summary> 8 /// <param name="func">方法,参数为object[],返回值为object,如果方法中调用了控件,请使用 ThreadInvokerControl(() => { 您的操作})进行包括</param> 9 /// <param name="objParams">方法参数</param> 10 /// <param name="callback">执行完成回调,参数使用func返回的值,如果错误返回的是Exception,如果为空则默认调用基类回调方法</param> 11 /// <param name="enableControl">调用线程时禁用的控件</param> 12 /// <param name="blnShowSplashScreen">是否显示提示层</param> 13 /// <param name="enableControl">调用线程时,禁用的控件</param> 14 /// <param name="strMsg">提示层提示内容,仅当blnShowSplashScreen=true时生效</param> 15 protected void ThreadRun(Func<List<string>, object> func, List<string> objParams, Action<object> callback, Control[] enableControl = null, bool blnShowSplashScreen = true, string strMsg = null) 16 { 17 if (blnShowSplashScreen) 18 { 19 splashScreenManager1.ShowWaitForm(); 20 splashScreenManager1.SetWaitFormCaption("提示"); 21 if (string.IsNullOrEmpty(strMsg)) 22 strMsg = "处理正在进行中,请稍候..."; 23 splashScreenManager1.SetWaitFormDescription(strMsg); 24 } 25 if (enableControl != null) 26 { 27 SetControlEnableds(enableControl, false); 28 } 29 Thread th = new Thread(delegate() 30 { 31 try 32 { 33 object obj = func(objParams); 34 if (callback != null) 35 callback(obj); 36 } 37 catch (Exception ex) 38 { 39 if (callback != null) 40 callback(ex); 41 else 42 ThreadBaseCallBack(ex); 43 } 44 finally 45 { 46 if (blnShowSplashScreen) 47 { 48 ThreadInvokerControl(() => { splashScreenManager1.CloseWaitForm(); }); 49 } 50 if (enableControl != null) 51 { 52 ThreadInvokerControl(() => { SetControlEnableds(enableControl, true); }); 53 } 54 } 55 }); 56 th.IsBackground = true; 57 th.Start(); 58 } 59 /// <summary> 60 /// 功能描述:多线程执行方法,方法有返回值 61 /// 作 者:huangzh 62 /// 创建日期:2017-03-29 17:44:26 63 /// 任务编号:MES 64 /// </summary> 65 /// <param name="func">方法,参数为object[],返回值为object,如果方法中调用了控件,请使用 ThreadInvokerControl(() => { 您的操作})进行包括</param> 66 /// <param name="objParams">方法参数</param> 67 /// <param name="callback">执行完成回调,参数使用func返回的值,如果错误返回的是Exception,如果为空则默认调用基类回调方法</param> 68 /// <param name="enableControl">调用线程时禁用的控件</param> 69 /// <param name="blnShowSplashScreen">是否显示提示层</param> 70 /// <param name="strMsg">提示层提示内容,仅当blnShowSplashScreen=true时生效</param> 71 protected void ThreadRun(Func<List<object>, object> func, List<object> objParams, Action<object> callback, Control[] enableControl = null, bool blnShowSplashScreen = true, string strMsg = null) 72 { 73 if (blnShowSplashScreen) 74 { 75 splashScreenManager1.ShowWaitForm(); 76 splashScreenManager1.SetWaitFormCaption("提示"); 77 if (string.IsNullOrEmpty(strMsg)) 78 strMsg = "处理正在进行中,请稍候..."; 79 splashScreenManager1.SetWaitFormDescription(strMsg); 80 } 81 if (enableControl != null) 82 { 83 SetControlEnableds(enableControl, false); 84 } 85 Thread th = new Thread(delegate() 86 { 87 try 88 { 89 object obj = func(objParams); 90 if (callback != null) 91 callback(obj); 92 } 93 catch (Exception ex) 94 { 95 if (callback != null) 96 callback(ex); 97 else 98 ThreadBaseCallBack(ex); 99 } 100 finally 101 { 102 if (blnShowSplashScreen) 103 { 104 ThreadInvokerControl(() => { splashScreenManager1.CloseWaitForm(); }); 105 } 106 if (enableControl != null) 107 { 108 ThreadInvokerControl(() => { SetControlEnableds(enableControl, true); }); 109 } 110 } 111 }); 112 th.IsBackground = true; 113 th.Start(); 114 } 115 116 /// <summary> 117 /// 功能描述:多线程执行方法,方法有返回值 118 /// 作 者:huangzh 119 /// 创建日期:2017-03-29 17:44:26 120 /// 任务编号:MES 121 /// </summary> 122 /// <param name="func">方法,参数为ictionary<string,object>,返回值为object,如果方法中调用了控件,请使用 ThreadInvokerControl(() => { 您的操作})进行包括</param> 123 /// <param name="objParams">方法参数</param> 124 /// <param name="callback">执行完成回调,参数使用func返回的值,如果错误返回的是Exception,如果为空则默认调用基类回调方法</param> 125 /// <param name="enableControl">调用线程时禁用的控件</param> 126 /// <param name="blnShowSplashScreen">是否显示提示层</param> 127 /// <param name="strMsg">提示层提示内容,仅当blnShowSplashScreen=true时生效</param> 128 protected void ThreadRun(Func<Dictionary<string, object>, object> func, Dictionary<string, object> objParams, Action<object> callback, Control[] enableControl = null, bool blnShowSplashScreen = true, string strMsg = null) 129 { 130 if (blnShowSplashScreen) 131 { 132 splashScreenManager1.ShowWaitForm(); 133 splashScreenManager1.SetWaitFormCaption("提示"); 134 if (string.IsNullOrEmpty(strMsg)) 135 strMsg = "处理正在进行中,请稍候..."; 136 splashScreenManager1.SetWaitFormDescription(strMsg); 137 } 138 if (enableControl != null) 139 { 140 SetControlEnableds(enableControl, false); 141 } 142 Thread th = new Thread(delegate() 143 { 144 try 145 { 146 object obj = func(objParams); 147 if (callback != null) 148 callback(obj); 149 } 150 catch (Exception ex) 151 { 152 if (callback != null) 153 callback(ex); 154 else 155 ThreadBaseCallBack(ex); 156 } 157 finally 158 { 159 if (blnShowSplashScreen) 160 { 161 ThreadInvokerControl(() => { splashScreenManager1.CloseWaitForm(); }); 162 } 163 if (enableControl != null) 164 { 165 ThreadInvokerControl(() => { SetControlEnableds(enableControl, true); }); 166 } 167 } 168 }); 169 th.IsBackground = true; 170 th.Start(); 171 } 172 173 /// <summary> 174 /// 功能描述:多线程执行方法,方法无参数,有返回值 175 /// 作 者:huangzh 176 /// 创建日期:2017-03-29 17:44:26 177 /// 任务编号:MES 178 /// </summary> 179 /// <param name="func">方法,返回值为object,如果方法中调用了控件,请使用 ThreadInvokerControl(() => { 您的操作})进行包括</param> 180 /// <param name="objParams">方法参数</param> 181 /// <param name="callback">执行完成回调,参数使用func返回的值,如果错误返回的是Exception,如果为空则默认调用基类回调方法</param> 182 /// <param name="enableControl">调用线程时禁用的控件</param> 183 /// <param name="blnShowSplashScreen">是否显示提示层</param> 184 /// <param name="strMsg">提示层提示内容,仅当blnShowSplashScreen=true时生效</param> 185 protected void ThreadRun(Func<object> func, Action<object> callback, Control[] enableControl = null, bool blnShowSplashScreen = true, string strMsg = null) 186 { 187 if (blnShowSplashScreen) 188 { 189 splashScreenManager1.ShowWaitForm(); 190 splashScreenManager1.SetWaitFormCaption("提示"); 191 if (string.IsNullOrEmpty(strMsg)) 192 strMsg = "处理正在进行中,请稍候..."; 193 splashScreenManager1.SetWaitFormDescription(strMsg); 194 } 195 if (enableControl != null) 196 { 197 SetControlEnableds(enableControl, false); 198 } 199 Thread th = new Thread(delegate() 200 { 201 try 202 { 203 object obj = func(); 204 if (callback != null) 205 callback(obj); 206 } 207 catch (Exception ex) 208 { 209 if (callback != null) 210 callback(ex); 211 else 212 ThreadBaseCallBack(ex); 213 } 214 finally 215 { 216 if (blnShowSplashScreen) 217 { 218 ThreadInvokerControl(() => { splashScreenManager1.CloseWaitForm(); }); 219 } 220 if (enableControl != null) 221 { 222 ThreadInvokerControl(() => { SetControlEnableds(enableControl, true); }); 223 } 224 } 225 }); 226 th.IsBackground = true; 227 th.Start(); 228 } 229 #endregion 230 231 #region 方法无返回值 232 /// <summary> 233 /// 功能描述:多线程执行方法,方法无返回值 234 /// 作 者:huangzh 235 /// 创建日期:2017-03-29 17:44:26 236 /// 任务编号:MES 237 /// </summary> 238 /// <param name="func">方法,参数为object[],如果方法中调用了控件,请使用 ThreadInvokerControl(() => { 您的操作})进行包括</param> 239 /// <param name="objParams">方法参数</param> 240 /// <param name="callback">执行完成回调,参数为object,如果错误返回的是Exception,否则为null,如果为空则默认调用基类回调方法</param> 241 /// <param name="enableControl">调用线程时禁用的控件</param> 242 /// <param name="blnShowSplashScreen">是否显示提示层</param> 243 /// <param name="strMsg">提示层提示内容,仅当blnShowSplashScreen=true时生效</param> 244 protected void ThreadRunExt(Action<List<string>> func, List<string> objParams, Action<object> callback, Control[] enableControl = null, bool blnShowSplashScreen = true, string strMsg = null) 245 { 246 if (blnShowSplashScreen) 247 { 248 splashScreenManager1.ShowWaitForm(); 249 splashScreenManager1.SetWaitFormCaption("提示"); 250 if (string.IsNullOrEmpty(strMsg)) 251 strMsg = "处理正在进行中,请稍候..."; 252 splashScreenManager1.SetWaitFormDescription(strMsg); 253 } 254 if (enableControl != null) 255 { 256 SetControlEnableds(enableControl, false); 257 } 258 Thread th = new Thread(delegate() 259 { 260 try 261 { 262 func(objParams); 263 if (callback != null) 264 callback(null); 265 } 266 catch (Exception ex) 267 { 268 if (callback != null) 269 callback(ex); 270 else 271 ThreadBaseCallBack(ex); 272 } 273 finally 274 { 275 if (blnShowSplashScreen) 276 { 277 ThreadInvokerControl(() => { splashScreenManager1.CloseWaitForm(); }); 278 } 279 if (enableControl != null) 280 { 281 ThreadInvokerControl(() => { SetControlEnableds(enableControl, true); }); 282 } 283 } 284 }); 285 th.IsBackground = true; 286 th.Start(); 287 } 288 /// <summary> 289 /// 功能描述:多线程执行方法,方法无返回值 290 /// 作 者:huangzh 291 /// 创建日期:2017-03-29 17:44:26 292 /// 任务编号:MES 293 /// </summary> 294 /// <param name="func">方法,参数为object[],如果方法中调用了控件,请使用 ThreadInvokerControl(() => { 您的操作})进行包括</param> 295 /// <param name="objParams">方法参数</param> 296 /// <param name="callback">执行完成回调,参数为object,如果错误返回的是Exception,否则为null,如果为空则默认调用基类回调方法</param> 297 /// <param name="enableControl">调用线程时禁用的控件</param> 298 /// <param name="blnShowSplashScreen">是否显示提示层</param> 299 /// <param name="strMsg">提示层提示内容,仅当blnShowSplashScreen=true时生效</param> 300 protected void ThreadRunExt(Action<List<object>> func, List<object> objParams, Action<object> callback, Control[] enableControl = null, bool blnShowSplashScreen = true, string strMsg = null) 301 { 302 if (blnShowSplashScreen) 303 { 304 splashScreenManager1.ShowWaitForm(); 305 splashScreenManager1.SetWaitFormCaption("提示"); 306 if (string.IsNullOrEmpty(strMsg)) 307 strMsg = "处理正在进行中,请稍候..."; 308 splashScreenManager1.SetWaitFormDescription(strMsg); 309 } 310 if (enableControl != null) 311 { 312 SetControlEnableds(enableControl, false); 313 } 314 Thread th = new Thread(delegate() 315 { 316 try 317 { 318 func(objParams); 319 if (callback != null) 320 callback(null); 321 } 322 catch (Exception ex) 323 { 324 if (callback != null) 325 callback(ex); 326 else 327 ThreadBaseCallBack(ex); 328 } 329 finally 330 { 331 if (blnShowSplashScreen) 332 { 333 ThreadInvokerControl(() => { splashScreenManager1.CloseWaitForm(); }); 334 } 335 if (enableControl != null) 336 { 337 ThreadInvokerControl(() => { SetControlEnableds(enableControl, true); }); 338 } 339 } 340 }); 341 th.IsBackground = true; 342 th.Start(); 343 } 344 /// <summary> 345 /// 功能描述:多线程执行方法,方法无返回值 346 /// 作 者:huangzh 347 /// 创建日期:2017-03-29 17:44:26 348 /// 任务编号:MES 349 /// </summary> 350 /// <param name="func">方法,参数为ictionary<string,object>,如果方法中调用了控件,请使用 ThreadInvokerControl(() => { 您的操作})进行包括</param> 351 /// <param name="objParams">方法参数</param> 352 /// <param name="callback">执行完成回调,参数为object,如果错误返回的是Exception,否则为null,如果为空则默认调用基类回调方法</param> 353 /// <param name="enableControl">调用线程时禁用的控件</param> 354 /// <param name="blnShowSplashScreen">是否显示提示层</param> 355 /// <param name="strMsg">提示层提示内容,仅当blnShowSplashScreen=true时生效</param> 356 protected void ThreadRunExt(Action<Dictionary<string, object>> func, Dictionary<string, object> objParams, Action<object> callback, Control[] enableControl = null, bool blnShowSplashScreen = true, string strMsg = null) 357 { 358 if (blnShowSplashScreen) 359 { 360 splashScreenManager1.ShowWaitForm(); 361 splashScreenManager1.SetWaitFormCaption("提示"); 362 if (string.IsNullOrEmpty(strMsg)) 363 strMsg = "处理正在进行中,请稍候..."; 364 splashScreenManager1.SetWaitFormDescription(strMsg); 365 } 366 if (enableControl != null) 367 { 368 SetControlEnableds(enableControl, false); 369 } 370 Thread th = new Thread(delegate() 371 { 372 try 373 { 374 func(objParams); 375 if (callback != null) 376 callback(null); 377 } 378 catch (Exception ex) 379 { 380 if (callback != null) 381 callback(ex); 382 else 383 ThreadBaseCallBack(ex); 384 } 385 finally 386 { 387 if (blnShowSplashScreen) 388 { 389 ThreadInvokerControl(() => { splashScreenManager1.CloseWaitForm(); }); 390 } 391 if (enableControl != null) 392 { 393 ThreadInvokerControl(() => { SetControlEnableds(enableControl, true); }); 394 } 395 } 396 }); 397 th.IsBackground = true; 398 th.Start(); 399 } 400 401 /// <summary> 402 /// 功能描述:多线程执行方法,方法无参数无返回值 403 /// 作 者:huangzh 404 /// 创建日期:2017-03-29 17:44:26 405 /// 任务编号:MES 406 /// </summary> 407 /// <param name="func">方法,如果方法中调用了控件,请使用 ThreadInvokerControl(() => { 您的操作})进行包括</param> 408 /// <param name="objParams">方法参数</param> 409 /// <param name="callback">执行完成回调,参数为object,如果错误返回的是Exception,否则为null,如果为空则默认调用基类回调方法</param> 410 /// <param name="enableControl">调用线程时禁用的控件</param> 411 /// <param name="blnShowSplashScreen">是否显示提示层</param> 412 /// <param name="strMsg">提示层提示内容,仅当blnShowSplashScreen=true时生效</param> 413 protected void ThreadRunExt(Action func, Action<object> callback, Control[] enableControl = null, bool blnShowSplashScreen = true, string strMsg = null) 414 { 415 if (blnShowSplashScreen) 416 { 417 splashScreenManager1.ShowWaitForm(); 418 splashScreenManager1.SetWaitFormCaption("提示"); 419 if (string.IsNullOrEmpty(strMsg)) 420 strMsg = "处理正在进行中,请稍候..."; 421 splashScreenManager1.SetWaitFormDescription(strMsg); 422 } 423 if (enableControl != null) 424 { 425 SetControlEnableds(enableControl, false); 426 } 427 Thread th = new Thread(delegate() 428 { 429 try 430 { 431 func(); 432 if (callback != null) 433 callback(null); 434 } 435 catch (Exception ex) 436 { 437 if (callback != null) 438 callback(ex); 439 else 440 ThreadBaseCallBack(ex); 441 } 442 finally 443 { 444 if (blnShowSplashScreen) 445 { 446 ThreadInvokerControl(() => { splashScreenManager1.CloseWaitForm(); }); 447 } 448 if (enableControl != null) 449 { 450 ThreadInvokerControl(() => { SetControlEnableds(enableControl, true); }); 451 } 452 } 453 }); 454 th.IsBackground = true; 455 th.Start(); 456 } 457 #endregion 458 459 /// <summary> 460 /// 功能描述:委托调用,用于夸线程访问控件 461 /// 作 者:huangzh 462 /// 创建日期:2017-03-29 17:58:53 463 /// 任务编号:MES 464 /// </summary> 465 /// <param name="action">action</param> 466 /// <param name="f">所在窗体,默认使用当前窗体</param> 467 protected void ThreadInvokerControl(Action action, Form frm = null) 468 { 469 if (frm == null) 470 frm = this; 471 frm.BeginInvoke(action); 472 } 473 474 /// <summary> 475 /// 功能描述:线程默认回调方法 476 /// 作 者:huangzh 477 /// 创建日期:2017-03-29 19:31:19 478 /// 任务编号:MES 479 /// </summary> 480 /// <param name="obj">obj</param> 481 private void ThreadBaseCallBack(object obj) 482 { 483 if (obj is Exception) 484 { 485 ThreadInvokerControl(() => { throw obj as Exception; }); 486 } 487 } 488 489 490 #region 禁用控件时不改变空间颜色 491 [System.Runtime.InteropServices.DllImport("user32.dll ")] 492 private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int wndproc); 493 [System.Runtime.InteropServices.DllImport("user32.dll ")] 494 private static extern int GetWindowLong(IntPtr hWnd, int nIndex); 495 496 private const int GWL_STYLE = -16; 497 private const int WS_DISABLED = 0x8000000; 498 499 /// <summary> 500 /// 功能描述:设置控件的Enable属性,控件不改颜色 501 /// 作 者:huangzh 502 /// 创建日期:2017-03-30 09:01:45 503 /// 任务编号:MES 504 /// </summary> 505 /// <param name="c">c</param> 506 /// <param name="enabled">enabled</param> 507 public void SetControlEnabled(Control c, bool enabled) 508 { 509 if (enabled) 510 { 511 SetWindowLong(c.Handle, GWL_STYLE, (~WS_DISABLED) & GetWindowLong(c.Handle, GWL_STYLE)); 512 } 513 else 514 { 515 SetWindowLong(c.Handle, GWL_STYLE, WS_DISABLED + GetWindowLong(c.Handle, GWL_STYLE)); 516 } 517 } 518 /// <summary> 519 /// 功能描述:设置多个控件的Enable属性,控件不改颜色 520 /// 作 者:huangzh 521 /// 创建日期:2017-03-30 09:07:12 522 /// 任务编号:MES 523 /// </summary> 524 /// <param name="cs">cs</param> 525 /// <param name="enabled">enabled</param> 526 public void SetControlEnableds(Control[] cs, bool enabled) 527 { 528 foreach (var c in cs) 529 { 530 SetControlEnabled(c, enabled); 531 } 532 } 533 #endregion
再看使用方法
1 Dictionary<string, object> para = new Dictionary<string, object>(); 2 para.Add("strConfig", "1"); 3 para.Add("strTypeValue", "2"); 4 5 ThreadRunExt(GetSource, para, null, new Control[] { this.xtabMain });
1 private void GetSource(Dictionary<string, object> para) 2 {.....}