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);
    }
  • 相关阅读:
    顺序前缀改为随机性前缀 反转时间戳 反转年月日
    后台组件的治理思路
    干货 | 高耦合场景下,Trip.com如何做支付设计与落地
    每天响应数亿次请求,腾讯云如何提供高可用API服务?
    系统管理及操作命令
    远程连接及系统管理
    linux系统部署安装过程
    day 1 硬件组成概念及介绍笔记
    day 4
    day 3
  • 原文地址:https://www.cnblogs.com/mskycn/p/5100109.html
Copyright © 2011-2022 走看看