/// <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. php课程 6-24 字符串函数有哪些(复习)

    php课程 6-24 字符串函数有哪些(复习) 一.总结 一句话总结: 二.php课程 6-24 字符串函数有哪些(复习) 上次复习:--------------------------------- ...

  2. 通过rinetd实现port转发来訪问内网的服务

    一.   问题描写叙述 通过外网来訪问内网的服务 二.   环境要求 须要有一台能够外网訪问的机器做port映射.通过数据包转发来实现外部訪问阿里云的内网服务 三.   操作方法 做port映射的方案 ...

  3. 【物理】概念的理解 —— Phase(相位)

    Phase is the position of a point in time (an instant) on a waveform cycle. 相位指的是波形周期中点在某一时刻的位置.Phase ...

  4. 今天犯了个小错误:_dataArray.count>1 和_dataArray.count>0搞混淆了

    _dataArray.count>1 和_dataArray.count>0搞混淆了:当数据为一条时,条件不成立.应该_dataArray.count>=1  或者>0   ( ...

  5. 【codeforces 777B】Game of Credit Cards

    [题目链接]:http://codeforces.com/contest/777/problem/B [题意] 等价题意: 两个人都有n个数字, 然后两个人的数字进行比较; 数字小的那个人得到一个嘲讽 ...

  6. oracle数据库零散知识01

    1,rownum 是一个虚列,使用时必须包括1才能使用,rownum = 1,rownum < 10;  rownum = 2是不可以的: 2,if case loop 要加end结束,end ...

  7. java中的方法返回值使用泛型,实现灵活的返回值类型

    痛点:      使用Mybatis框架的时候,想封装一个底层JDBC控制器,用于提供和Mybatis交互的增删改查接口(公用的接口),但由于公用的查询方法可能是用户自定义的任意一个和表对应的java ...

  8. noip刷题记录 20170818

    天天爱跑步 lca + 树上差分 #include<iostream> #include<cstdio> #include<cstdlib> #include< ...

  9. Android Studio 2.3.1导出jar文件不能生成release解决办法

    升级了AS之后,在项目中的时候,有个需求需要把通过AS导出一个模块,需要以jar的形式导出来,研究了一下,按照网上的描述操作了一遍,不知道是AS版本问题还是自己操作问题,发现使用 ./gradlew ...

  10. C# TimeSpan 时间计算

    原文:C# TimeSpan 时间计算 本文告诉大家简单的方法进行时间计算. 实际上使用 TimeSpan 可以做到让代码比较好懂,而代码很简单. 例如我使用下面的代码表示 5 秒 const int ...