zoukankan      html  css  js  c++  java
  • ThreadHelper

    public class ThreadHelper
    {
    public static void RunSafeThread(Control ctrl, MethodInvoker invoker)
    {
    if(ctrl != null && !ctrl.IsDisposed)
    {
    try
    {
    if (ctrl.InvokeRequired)
    {
    ctrl.Invoke(invoker);
    }
    else
    {
    invoker();
    }
    }
    catch { }
    }
    }
    
    public static Thread Threading<TReturn>(ThreadDelegate<TReturn> method, ThreadCallback<TReturn> callback)
    {
    Thread thread = new Thread(() =>
    {
    IAsyncResult result = method.BeginInvoke(null, null);
    TReturn returnValue = method.EndInvoke(result);
    if(callback != null)
    {
    callback(returnValue);
    }
    });
    thread.Start();
    return thread;
    }
    
    public static Thread Threading<TReturn>(ThreadDelegateParams<TReturn> method, object[] parameters, ThreadCallback<TReturn> callback)
    {
    Thread thread = new Thread(() =>
    {
    IAsyncResult result = method.BeginInvoke(ref parameters, null, null);
    TReturn returnValue = method.EndInvoke(ref parameters, result);
    if (callback != null)
    {
    callback(returnValue);
    }
    });
    thread.Start();
    return thread;
    }
    
    public static Thread Threading<TReturn>(ThreadDelegateParams<TReturn> method, object[] parameters, ThreadCallbackParams<TReturn> callback)
    {
    Thread thread = new Thread(() =>
    {
    IAsyncResult result = method.BeginInvoke(ref parameters, null, null);
    TReturn returnValue = method.EndInvoke(ref parameters, result);
    if (callback != null)
    {
    callback(returnValue, parameters);
    }
    });
    thread.Start();
    return thread;
    }
    
    public delegate TReturn ThreadCallback<TReturn>(TReturn returnValue);
    public delegate TReturn ThreadCallbackParams<TReturn>(TReturn returnValue, object[] threadDelegateParams);
    public delegate TReturn ThreadDelegate<TReturn>();
    public delegate TReturn ThreadDelegateParams<TReturn>(ref object[] parameters);
    }
  • 相关阅读:
    未登录词识别方法研究公布硕士论文最新进展五(2007.4.26)
    公布硕士论文最新进展一(2007.3.6)
    对于切分歧义以及识别未登录词的随想公布硕士论文最新进展四(2007.4.13)
    一则杂感一次医院项目的感悟
    公布硕士论文最新进展三(2007.3.26)
    毕业典礼7.1
    家乡风光
    还在深圳!
    经历摩托罗拉
    又回来了,南京!
  • 原文地址:https://www.cnblogs.com/mskycn/p/5100109.html
Copyright © 2011-2022 走看看