/// <summary>
/// 调用函数信息
/// </summary>
public class CallFunction
{
/// <summary>
/// 执行函数信息
/// </summary>
private readonly FunctionInfo _function = null; /// <summary>
/// 重试总数
/// </summary>
private int retryCount; public CallFunction(FunctionInfo functionInfo)
{
if (functionInfo == null)
{
throw new Exception("functionInfo为null");
} if (functionInfo.Func == null)
{
throw new Exception("functionInfo.Func为null");
}
_function = functionInfo;
retryCount = functionInfo.RetryCount;
} /// <summary>
/// 执行 信息
/// </summary>
/// <returns></returns>
public FunctionResult Invoke()
{
int timeOutCount = ; //超时次数
var lisExceptions = new List<RetryException>(); //异常集合
FunctionResult functionResult = new FunctionResult(); //函数返回结果
do
{
try
{
AutoResetEvent autoResetEvent = new AutoResetEvent(false);
Thread thread = new Thread(() =>
{
try
{
functionResult.Result = _function.Func.Invoke();
}
catch (RetryException retryException) //重试异常,需要外面自定义
{
if (retryException.Exception != null)
{
functionResult.Exception = retryException.Exception;
}
functionResult.IsRetryException = true;
}
catch (Exception exception)
{
functionResult.Result = null;
functionResult.Exception = exception;
}
finally
{
try
{
autoResetEvent.Set();
}
catch
{
// ignored
}
}
}) { IsBackground = true, Priority = ThreadPriority.Highest };
thread.Start();
bool autoReset = autoResetEvent.WaitOne(TimeSpan.FromSeconds(_function.TimeOut)); //线程等
try
{
//thread.Abort();
}
catch
{
// ignored
}
try
{
autoResetEvent.Close();
autoResetEvent.Dispose();
}
catch
{
// ignored
}
if (functionResult.IsRetryException)
{
Thread.Sleep(); //执行失败在睡眠 1 毫秒
functionResult.IsRetryException = false;
throw new RetryException() { Exception = functionResult.Exception }; //重试异常
}
if (!autoReset) //
{
timeOutCount++; //超时次数
_function.RetryCount--;
Thread.Sleep(); //执行失败在睡眠 1 毫秒
}
else
{
return functionResult;
}
}
catch (RetryException retryException) //重试异常,需要外面自定义
{
_function.RetryCount--;
lisExceptions.Add(retryException);
}
catch (Exception ex) //Exception 异常
{
functionResult.Result = null;
functionResult.Exception = ex;
return functionResult;
}
} while (_function.RetryCount > );
functionResult.Result = null;
functionResult.Exception =
new Exception("执行函数失败,超时次数:" + timeOutCount + "重试次数:" + (retryCount - _function.RetryCount),
lisExceptions.Count > ? lisExceptions[lisExceptions.Count - ].Exception : null);
return functionResult;
} ///// <summary>
///// 执行 信息
///// </summary>
///// <returns></returns>
//public FunctionResult Invoke()
//{
// int timeOutCount = 0; //超时次数
// var lisExceptions = new List<RetryException>(); //异常集合
// FunctionResult functionResult = new FunctionResult(); //函数返回结果
// do
// {
// try
// {
// IAsyncResult iAsyncResult = _function.Func.BeginInvoke(null, null);
// if (!iAsyncResult.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(_function.TimeOut))) //阻塞当前线程
// {
// try
// {
// if (iAsyncResult.IsCompleted)
// {
// _function.Func.EndInvoke(iAsyncResult);
// }
// else
// {
// _function.Func.EndInvoke(iAsyncResult);
// }
// }
// catch (Exception ex)
// {
// iAsyncResult.AsyncWaitHandle.Close();
// iAsyncResult.AsyncWaitHandle.Dispose();
// }
// timeOutCount++; //超时次数
// //超时重新连接
// _function.RetryCount--;
// }
// else
// {
// functionResult.Result = _function.Func.EndInvoke(iAsyncResult);
// return functionResult;
// }
// }
// catch (RetryException retryException) //重试异常,需要外面自定义
// {
// _function.RetryCount--;
// lisExceptions.Add(retryException);
// }
// catch (Exception ex) //Exception 异常
// {
// functionResult.Result = null;
// functionResult.Exception = ex;
// return functionResult;
// }
// } while (_function.RetryCount > 0);
// functionResult.Result = null;
// functionResult.Exception =
// new Exception("执行函数失败,超时次数:" + timeOutCount + "重试次数:" + (retryCount - _function.RetryCount),
// lisExceptions.Count > 0 ? lisExceptions[lisExceptions.Count - 1].Exception : null);
// return functionResult;
//} } /// <summary>
/// 函数对象信息
/// </summary>
public class FunctionInfo
{
private int _timeout = ;
/// <summary>
/// 超时时间 以秒为单位,默认是20S
/// </summary>
public int TimeOut
{
get { return _timeout; }
set { _timeout = value; }
}
/// <summary>
/// 重试次数 默认 3次
/// </summary>
private int _retryCount = ; /// <summary>
/// 重试次数 默认 3次
/// </summary>
public int RetryCount
{
get { return _retryCount; }
set { _retryCount = value; }
}
/// <summary>
/// 没参数但可以返回的委托
/// </summary>
public Func<dynamic> Func { get; set; }
} /// <summary>
/// 函数执行结果
/// </summary>
public class FunctionResult
{
/// <summary>
///异常
/// </summary>
public Exception Exception { get; set; }
/// <summary>
/// 返回结果
/// </summary>
public dynamic Result
{
get;
set; }
/// <summary>
/// 是否重试
/// </summary>
public bool IsRetryException { get; set; }
}

调用方式:

 //可设置超时时间TimeOut(默认20s)及失败重试次数RetryCount(默认3次)
CallFunction cf = new CallFunction(new FunctionInfo()
{
Func = () => Test(),
RetryCount = ,
TimeOut =
});
FunctionResult r = cf.Invoke();

c# 自定义公共类CallFunction-调用函数信息帮助类的更多相关文章

  1. [C#] 常用工具类——应用程序属性信息访问类

    using System; using System.Collections.Generic; using System.Text; using System.Reflection; namespac ...

  2. c++学习笔记之基础---类内声明函数后在类外定义的一种方法

    在C++的“类”中经常遇到这样的函数, 返回值类型名 类名::函数成员名(参数表){ 函数体.} 双冒号的作用 ::域名解析符!返回值类型名 类名::函数成员名(参数表) { 函数体. } 这个是在类 ...

  3. VB6/VBA中跟踪鼠标移出窗体控件事件(类模块成员函数指针CHooker类应用)

    一.关于起因 前几天发了一篇博文,是关于获取VB类模块成员函数指针的内容(http://www.cnblogs.com/alexywt/p/5880993.html):今天我就发一下我的应用实例. V ...

  4. 在JBPM的Handle类中调用Spring管理的类

    我们在使用JBPM定义流程的时候经常要在流程定义文件中加入一个继承xxxHandler的类来实现我们的业务逻辑判断或者其他的需求,在这个类中一般都是用Spring的Application来获取,而这种 ...

  5. 编写一个带有main函数的类,调用上面的汽车类,实例化奔驰、大众、丰田等不同品牌和型号,模拟开车过程:启动、加速、转弯、刹车、息火,实时显示速度。

    //程序入口    public static void main(String[] args) {        // TODO Auto-generated method stub         ...

  6. unittest框架,调用函数类 和 调用函数外的 方法

  7. C++之友元机制(友元函数和友元类)

    一.为什么引入友元机制? 总的来说就是为了让非成员函数即普通函数或其他类可以访问类的私有成员,这确实破坏了类的封装性和数据的隐蔽性,但为什么要这么做呢? (c++ primer:尽管友元被授予从外部访 ...

  8. C++的友元类和友元函数实例

    #include <math.h> #include<iostream> using namespace std; class Point { public: Point(do ...

  9. c++友元函数与友元类

    友元函数和友元类的需要: 类具有封装和信息隐藏的特性.只有类的成员函数才能访问类的私有成员,程序中的其他函数是无法访问私有成员的.非成员函数可以访问类中的公有成员,但是如果将数据成员都定义为公有的,这 ...

随机推荐

  1. Objective-C基础笔记(8)Foundation经常使用类NSString

    一.创建字符串的方法 void stringCreate(){ //方法1 NSString *str1 = @"A String!"; //方法2 NSString *str2 ...

  2. php实现把数组排成最小的数(核心是排序)(看别人的代码其实也没那么难)(把php代码也看一下)(implode("",$numbers);)(usort)

    php实现把数组排成最小的数(核心是排序)(看别人的代码其实也没那么难)(把php代码也看一下)(implode("",$numbers);)(usort) 一.总结 核心是排序 ...

  3. php实现表示数值的字符串(is_numeric($s))

    php实现表示数值的字符串(is_numeric($s)) 一.总结 is_numeric($s) 二.php实现表示数值的字符串 题目描述 请实现一个函数用来判断字符串是否表示数值(包括整数和小数) ...

  4. [TypeScript] Catch unsafe use of "this" in TypeScript functions

    this is probably the most tricky thing to use in JavaScript and therefore TypeScript. Fortunately th ...

  5. Qt 无标题无边框程序的拖动和改变大小

    最近做项目遇到的问题,总结下. 有时候我们觉得系统的标题栏和按钮太丑太呆板,想做自己的标题栏以及最大化.最小化.关闭,菜单按钮,我们就需要 setWindowFlags(Qt::FramelessWi ...

  6. GANs(生成对抗网络)初步

    Image Completion with Deep Learning in TensorFlow 1. 基本思路 首先定义一个简单的.常见的概率分布,将其表示为 pz,不妨将其作为 [-1, 1] ...

  7. 【14.36%】【codeforces 614C】Peter and Snow Blower

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. JAVA SE回顾及思考(1)——面向对象的特点

    学习Java已经三年了,现在开始做Android开发,虽说还在用Java语言但本人现在才真真的意识到无论学什么基础才是最重要的,可能一些刚接触Java或者Android的朋友现在还体会不到基础的重要性 ...

  9. css3中的制作动画小总结

    系列教程 CSS3属性中有关于制作动画的三个属性:Transform,Transition,Animation: Transform 在CSS3中transform主要包括以下几种:旋转rotate. ...

  10. windows mysql5.7 InnoDB 通过frm与ibd对数据进行恢复

    参考:https://www.jianshu.com/p/50a2e13cd5cf 安装MySQL Utilities 下载地址:https://dev.mysql.com/downloads/uti ...