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);
    }
  • 相关阅读:
    Reflector 插件
    Tips for ILMerge
    WaitAll for multiple handles on a STA thread is not supported 解决方案
    MSI: UAC return 0x800704C7
    SET与SETX的区别
    年在Copyright中的含义
    gacutil : 添加.NET 4.0 assembly 到GAC失败
    LicenseContext.GetSavedLicenseKey 需要 FileIOPermission
    Linq学习之linq基础知识
    SQL Server 2008如何导出带数据的脚本文件
  • 原文地址:https://www.cnblogs.com/mskycn/p/5100109.html
Copyright © 2011-2022 走看看