/// <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. [RxJS] Flatten a higher order observable with concatAll in RxJS

    Besides switch and mergeAll, RxJS also provides concatAll as a flattening operator. In this lesson w ...

  2. [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 ...

  3. Android 延时执行的几种方法

    开启新线程 new Thread(new Runnable(){ public void run(){ Thread.sleep(XXXX); handler.sendMessage(); //告诉主 ...

  4. leveldb学习:skiplist

    leveldb中的memtable仅仅是一个封装类,它的底层实现是一个跳表. 跳表是一种基于随机数的平衡数据结构.其它的平衡数据结构还有红黑树.AVL树.但跳表的原理比它们简单非常多.跳表有点像链表, ...

  5. XMPP之安装mySQL--Mac OS(一)

    come from:http://www.cnblogs.com/xiaodao/archive/2013/04/04/2999426.html 一.安装 到MySQL官网上http://dev.my ...

  6. TextView中实现跑马灯的最简单方法

    几行代码实现跑马灯效果,效果如下: 因为很简单,所以就直接贴代码喽 <TextView android:id="@+id/item1_title_message" andro ...

  7. 【BZOJ 1032】 [JSOI2007]祖码Zuma

    [题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1032 [题意] [题解] /* 设f[i][j]表示从第i个珠子开始的j个珠子被消除; ...

  8. Windows消息:WM_USER与WM_APP的区别

    Windows消息范围及意义 #define WM_USER 0x0400 #define WM_APP 0x8000 0到WM_USER-1 Messages reserved for use by ...

  9. 项目中碰到的ExceptionInInitializerError异常

    背景 之前在集成第三方即时通信系统-融云的时候,我直接clone它的服务端源码,然后导入我的项目,我在测试它连接融云服务器案例时,发现一直不成功,始终报一个 ExceptionInInitialize ...

  10. 从华为产品学到什么是devops

    根据Gartner 2015 I&O Automation 报告,DevOps处于技术发展的最高点,实践受到高度关注,到底devops魔力在哪里? 从devops实践看主要是打破开发人员和运营 ...