Add timeout param.
This commit is contained in:
@@ -354,7 +354,7 @@ namespace Modbus.Net
|
|||||||
{
|
{
|
||||||
if (Machine.IsConnected)
|
if (Machine.IsConnected)
|
||||||
{
|
{
|
||||||
var ans = await task.Invoke(Machine, _tasks, task.Params);
|
var ans = await task.Invoke(Machine, _tasks, task.Params, task.TimeoutTime);
|
||||||
task.Return?.Invoke(ans);
|
task.Return?.Invoke(ans);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -411,13 +411,15 @@ namespace Modbus.Net
|
|||||||
/// <param name="getDataType">返回值的键类型</param>
|
/// <param name="getDataType">返回值的键类型</param>
|
||||||
/// <param name="getCycle">循环间隔(毫秒)</param>
|
/// <param name="getCycle">循环间隔(毫秒)</param>
|
||||||
/// <param name="sleepCycle">设备离线时的循环间隔(毫秒)</param>
|
/// <param name="sleepCycle">设备离线时的循环间隔(毫秒)</param>
|
||||||
public TaskItemGetData(Action<DataReturnDef> returnFunc, MachineGetDataType getDataType, int getCycle, int sleepCycle)
|
/// <param name="timeout">任务的超时时间</param>
|
||||||
|
public TaskItemGetData(Action<DataReturnDef> returnFunc, MachineGetDataType getDataType, int getCycle, int sleepCycle, int timeout = 100000)
|
||||||
{
|
{
|
||||||
Name = "GetDatas";
|
Name = "GetDatas";
|
||||||
Invoke = async (machine, tasks, parameters) =>
|
TimeoutTime = timeout;
|
||||||
|
Invoke = async (machine, tasks, parameters, timeoutTime) =>
|
||||||
{
|
{
|
||||||
var cts = new CancellationTokenSource();
|
var cts = new CancellationTokenSource();
|
||||||
cts.CancelAfter(TimeSpan.FromSeconds(100000));
|
cts.CancelAfter(TimeSpan.FromMilliseconds(timeoutTime));
|
||||||
var ans =
|
var ans =
|
||||||
await tasks.StartNew(
|
await tasks.StartNew(
|
||||||
async () => await machine.InvokeMachineMethod<IMachineMethodData,
|
async () => await machine.InvokeMachineMethod<IMachineMethodData,
|
||||||
@@ -447,13 +449,15 @@ namespace Modbus.Net
|
|||||||
/// <param name="values">写入的值</param>
|
/// <param name="values">写入的值</param>
|
||||||
/// <param name="setDataType">写入值的键类型</param>
|
/// <param name="setDataType">写入值的键类型</param>
|
||||||
/// <param name="returnFunc">返回值的处理函数</param>
|
/// <param name="returnFunc">返回值的处理函数</param>
|
||||||
public TaskItemSetData(Dictionary<string, double> values, MachineSetDataType setDataType, Action<bool> returnFunc = null)
|
/// <param name="timeout">任务的超时时间</param>
|
||||||
|
public TaskItemSetData(Dictionary<string, double> values, MachineSetDataType setDataType, Action<bool> returnFunc = null, int timeout = 100000)
|
||||||
{
|
{
|
||||||
Name = "SetDatas";
|
Name = "SetDatas";
|
||||||
Invoke = Invoke = async (machine, tasks, parameters) =>
|
TimeoutTime = timeout;
|
||||||
|
Invoke = Invoke = async (machine, tasks, parameters, timeoutTime) =>
|
||||||
{
|
{
|
||||||
var cts = new CancellationTokenSource();
|
var cts = new CancellationTokenSource();
|
||||||
cts.CancelAfter(TimeSpan.FromSeconds(100000));
|
cts.CancelAfter(TimeSpan.FromMilliseconds(timeoutTime));
|
||||||
var ans =
|
var ans =
|
||||||
await tasks.StartNew(
|
await tasks.StartNew(
|
||||||
async () => await machine.InvokeMachineMethod<IMachineMethodData,
|
async () => await machine.InvokeMachineMethod<IMachineMethodData,
|
||||||
@@ -482,6 +486,11 @@ namespace Modbus.Net
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public int TimerTime { get; set; }
|
public int TimerTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 超时时间
|
||||||
|
/// </summary>
|
||||||
|
public int TimeoutTime { get; set; } = 100000;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 离线定时器
|
/// 离线定时器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -495,7 +504,7 @@ namespace Modbus.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 执行的任务
|
/// 执行的任务
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Func<IMachinePropertyWithoutKey, TaskFactory, object[], Task<TInterType>> Invoke { get; set; }
|
public Func<IMachinePropertyWithoutKey, TaskFactory, object[], int, Task<TInterType>> Invoke { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 检测设备的在线状态
|
/// 检测设备的在线状态
|
||||||
@@ -566,7 +575,7 @@ namespace Modbus.Net
|
|||||||
Timer = new Timer(async state =>
|
Timer = new Timer(async state =>
|
||||||
{
|
{
|
||||||
if (!DetectConnected()) TimerChangeToDisconnect();
|
if (!DetectConnected()) TimerChangeToDisconnect();
|
||||||
var ans = await Invoke(GetMachine(), GetTaskFactory(), Params);
|
var ans = await Invoke(GetMachine(), GetTaskFactory(), Params, TimeoutTime);
|
||||||
Return?.Invoke(ans);
|
Return?.Invoke(ans);
|
||||||
}, null, 0, TimerTime);
|
}, null, 0, TimerTime);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user