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);
    }
  • 相关阅读:
    嵌入式整体框架——总结
    DSP Bios记忆
    三遥
    usb设备 配置 接口 端点
    ARM, MIPS, Power PC的比较
    STM32 IAP
    FSMC 总结
    BCD码与十进制的相互转换
    读 “cortexM3” 权威指南 小记(一)
    crc校验码的16 32位 查表法 算法记载
  • 原文地址:https://www.cnblogs.com/mskycn/p/5100109.html
Copyright © 2011-2022 走看看