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++友元函数与友元类
友元函数和友元类的需要: 类具有封装和信息隐藏的特性.只有类的成员函数才能访问类的私有成员,程序中的其他函数是无法访问私有成员的.非成员函数可以访问类中的公有成员,但是如果将数据成员都定义为公有的,这 ...
随机推荐
- Linux命令具体解释(2) – mv
文件位置: mv: /bin/mv /usr/share/man/man1/mv.1.gz 名称: mv - move (rename) files 使用方法: mv [ ...
- 【u208】修复公路
Time Limit: 1 second Memory Limit: 128 MB [问题描述] A地区在地震过后,连接所有村庄的公路都造成了损坏而无法通车.政府派人修复这些公路. 给出A地区的村庄数 ...
- Android自定义控件View(一)
虽然Android API给我们提供了众多控件View来使用,但是鉴于Android的开发性,自然少不了根据需求自定义控件View了.比如说QQ头像是圆形的,但是纵观整个Android控件也找不到一个 ...
- android获取和展示音乐的频谱
做了个音乐播放器 就一直想做个加一个音乐频谱的展示界面 觉的这是一个好玩的东西,可以将耳边动听的声音形象化,仿佛眼前可以看到声音一样. 但是我在文档的开发者指南里没有讲任何有关音乐频谱的东西,最后还是 ...
- asp.net webform中的ext.net使用
ext.net是对ext.js进行封装的net控件库,能够砸webform 和mvc中使用,从今天器我会对这一年多的ext.net开发进行一些对应的总结. 首先针对ext.net进行引用: <% ...
- POJ 2104 - 主席树 / 询问莫队+权值分块
传送门 题目大意应该都清楚. 今天看到一篇博客用分块+莫对做了这道题,直接惊呆了. 首先常规地离散化后将询问分块,对于某一询问,将莫队指针移动到指定区间,移动的同时处理权值分块的数字出现次数(单独.整 ...
- Sqlplus的一般操作
Sqlplus一些必要操作指令 1, 登陆sys 用户,需要加上 as sysdba Connect sys as sysdba; Input your password; Connected; 2, ...
- selenium firefox 提取qq空间相册链接
环境: selenium-java 3.9.1 firefox 57.0 geckodriver 0.19.1 1.大概的思路就是模拟用户点击行为,关于滚动条的问题,我是模拟下拉箭头,否则只能每个相册 ...
- maven Java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
如果你可以确认你的maven Dependencies中已经导入了如下的jar包,那么你就要检查下Deployment Assembly 选中项目 alt+enter,然后查看maven依赖有没有被添 ...
- 弄App Store提示和技巧推荐
众所周知上苹果的主页推荐是对产品最佳(高曝光率+零广告费)推广,然而苹果却对选择的方式和规则讳莫如深. 下面是搜集的一些获得推荐的开发人员的经验. 1. 产品要新颖.且质量上乘.这个质量包括非常多细节 ...