c# 自定义公共类CallFunction-调用函数信息帮助类
/// <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-调用函数信息帮助类的更多相关文章
- [C#] 常用工具类——应用程序属性信息访问类
using System; using System.Collections.Generic; using System.Text; using System.Reflection; namespac ...
- c++学习笔记之基础---类内声明函数后在类外定义的一种方法
在C++的“类”中经常遇到这样的函数, 返回值类型名 类名::函数成员名(参数表){ 函数体.} 双冒号的作用 ::域名解析符!返回值类型名 类名::函数成员名(参数表) { 函数体. } 这个是在类 ...
- VB6/VBA中跟踪鼠标移出窗体控件事件(类模块成员函数指针CHooker类应用)
一.关于起因 前几天发了一篇博文,是关于获取VB类模块成员函数指针的内容(http://www.cnblogs.com/alexywt/p/5880993.html):今天我就发一下我的应用实例. V ...
- 在JBPM的Handle类中调用Spring管理的类
我们在使用JBPM定义流程的时候经常要在流程定义文件中加入一个继承xxxHandler的类来实现我们的业务逻辑判断或者其他的需求,在这个类中一般都是用Spring的Application来获取,而这种 ...
- 编写一个带有main函数的类,调用上面的汽车类,实例化奔驰、大众、丰田等不同品牌和型号,模拟开车过程:启动、加速、转弯、刹车、息火,实时显示速度。
//程序入口 public static void main(String[] args) { // TODO Auto-generated method stub ...
- unittest框架,调用函数类 和 调用函数外的 方法
- C++之友元机制(友元函数和友元类)
一.为什么引入友元机制? 总的来说就是为了让非成员函数即普通函数或其他类可以访问类的私有成员,这确实破坏了类的封装性和数据的隐蔽性,但为什么要这么做呢? (c++ primer:尽管友元被授予从外部访 ...
- C++的友元类和友元函数实例
#include <math.h> #include<iostream> using namespace std; class Point { public: Point(do ...
- c++友元函数与友元类
友元函数和友元类的需要: 类具有封装和信息隐藏的特性.只有类的成员函数才能访问类的私有成员,程序中的其他函数是无法访问私有成员的.非成员函数可以访问类中的公有成员,但是如果将数据成员都定义为公有的,这 ...
随机推荐
- ios开发日期的NSDate,NSCalendar分类
#import <Foundation/Foundation.h> @interface NSDate (XMGExtension) /** */ // @property (nonato ...
- QT学习记录之控件布局
作者:朱金灿 来源:http://blog.csdn.net/clever101 想到控件布局就会想到Windows编程中要实现对话框上的控件的合理布局是一件多么艰难的事情.对此QT提出了一个很方便的 ...
- 【u006】海战
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 在峰会期间,武装部队得处于高度戒备.警察将监视每一条大街,军队将保卫建筑物,领空将布满了F-2003飞 ...
- web项目的WEB-INF目录
WEB-INF是Java的WEB应用的安全目录.所谓安全就是客户端无法访问,只有服务端可以访问的目录. 如果想在页面中直接访问其中的文件,必须通过web.xml文件对要访问的文件进行相应映射才能访问. ...
- AndroidClipSquare安卓实现方形头像裁剪
安卓实现方形头像裁剪 实现思路.界面可见区域为2层View 最顶层的View是显示层,主要绘制半透明边框区域和白色裁剪区域,代码比較easy. 第二层继承ImageView,使用ImageView的M ...
- 开源:通用的日志分析工具(LogViewer)
工具介绍 本工具最早是制作出来查看我的 FTL(Fast Trace Log) 二进制日志文件的, 后来因为去做Java后台,经常看 SpringBoot, Tomcat 等的日志, 就简单重构了一下 ...
- storm 经常使用类
弄 <dependency> <groupId>org.apache.storm</groupId> <artifactId>storm-core< ...
- MyReport报表引擎2.2.0.0新功能
分组功能添加分组头,分组尾设计支持,支持按字段分组,排序 分组效果 排序效果 新增分组行号函数,用于分组内部独立行号显示 分组行号效果 新增平均函数,用于求平均值统计 支持四则优先运算(用中括号表示, ...
- XMPP开发adiumclient登陆
我写在前面client它已经实现了登陆,我用下面的adium要登录落实的朋友加入,而自己写的client在聊天帐号. 第一次登录时adium工欲善其事,必先例如,下面的配置 保存后.你会发现自己的账号 ...
- C#List实现行转列
List实现行转列的通用方案 最近在做报表统计方面的需求,涉及到行转列报表.根据以往经验使用SQL可以比较容易完成,这次决定挑战一下直接通过代码方式完成行转列.期间遇到几个问题和用到的新知识这里整理记 ...