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);
    }
  • 相关阅读:
    Python爬虫实验报告之Big_Homework2_Douyu
    Python_dict
    Common sequence manipulation functions
    python基于opencv库的人脸识别总结
    使用cwrsync同步windows文件到linux
    搭建mosquitto
    docker搭建mqtt
    docker部署gofastdfs
    ap配置
    冒泡排序
  • 原文地址:https://www.cnblogs.com/mskycn/p/5100109.html
Copyright © 2011-2022 走看看