C# 每15秒执行一次方法,直到它工作为止

C# 每15秒执行一次方法,直到它工作为止,c#,C#,我想要一种方法,每15秒检查一次是否连接了特定设备/是否使用了该设备的驱动程序 比如: Every 15 seconds { if (Device is connected) { do sth.; exit loop; } } 到目前为止,我已经能够创建一个似乎有效的计时器: { System.Timers.Timer timer = new System.Timers.Timer(); timer.Interval = 15000; timer.Elapse

我想要一种方法,每15秒检查一次是否连接了特定设备/是否使用了该设备的驱动程序

比如:

Every 15 seconds 
{
if (Device is connected)
  {
   do sth.;
   exit loop;
  }      
}
到目前为止,我已经能够创建一个似乎有效的计时器:

{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 15000;
timer.Elapsed += CheckConnection;
timer.Start();
}

不幸的是,一旦我的CheckConnection方法为true/找到连接的设备,我就不知道如何退出/中止计时器。

下面是一个示例应用程序,它展示了如何使用它来实现所需的行为

假设您的
设备
类如下所示:

公共类设备
{
私有int-connectionAttents=0;
当连接成功=7时的私有常量int;
public bool CheckConnection()=>ConnectionAttents==当连接成功时;
public void Connect()=>connectionAttents++;
}
  • 每次调用
    Connect
    方法时,它都会增加
    connectionTests
  • 当尝试次数计数器等于预定义值时,
    CheckConnection
    将返回
    true
    (这模拟成功的连接),否则
    false
重试策略设置如下所示:

var retry=Policy.HandleResult(result=>!result.CheckConnection())
.WaitAndRetryForever(=>TimeSpan.FromSeconds(15),
onRetry:(结果,持续时间)=>Console.WriteLine(“重试已启动”);
var device=新设备();
重试。执行(()=>
{
device.Connect();
返回装置;
});
  • 在给定设备未连接之前(
    !result.CheckConnection())
    它将永远重试(
    WaitAndRetryForever
    )连接方法
  • 在每次重试之间,它会等待15秒(
    TimeSpan.FromSeconds(15)
  • 此处使用的
    onRetry
    委托仅用于演示目的<代码>重试是可选的
因此,如果我有以下简单的控制台应用程序:

类程序
{
静态void Main(字符串[]参数)
{
var retry=Policy.HandleResult(结果=>!result.CheckConnection())
.WaitAndRetryForever(=>TimeSpan.FromSeconds(15),
onRetry:(结果,持续时间)=>Console.WriteLine(“重试已启动”);
var device=新设备();
重试。执行(()=>
{
device.Connect();
返回装置;
});
Console.WriteLine(“设备已连接”);
}
}
然后将以下行打印到输出:

Retry has been initiated
Retry has been initiated
Retry has been initiated
Retry has been initiated
Retry has been initiated
Retry has been initiated
Device has been connected

下面是一个示例应用程序,它显示了如何使用来实现所需的行为

假设您的
设备
类如下所示:

公共类设备
{
私有int-connectionAttents=0;
当连接成功=7时的私有常量int;
public bool CheckConnection()=>ConnectionAttents==当连接成功时;
public void Connect()=>connectionAttents++;
}
  • 每次调用
    Connect
    方法时,它都会增加
    connectionTests
  • 当尝试次数计数器等于预定义值时,
    CheckConnection
    将返回
    true
    (这模拟成功的连接),否则
    false
重试策略设置如下所示:

var retry=Policy.HandleResult(result=>!result.CheckConnection())
.WaitAndRetryForever(=>TimeSpan.FromSeconds(15),
onRetry:(结果,持续时间)=>Console.WriteLine(“重试已启动”);
var device=新设备();
重试。执行(()=>
{
device.Connect();
返回装置;
});
  • 在给定设备未连接之前(
    !result.CheckConnection())
    它将永远重试(
    WaitAndRetryForever
    )连接方法
  • 在每次重试之间,它会等待15秒(
    TimeSpan.FromSeconds(15)
  • 此处使用的
    onRetry
    委托仅用于演示目的<代码>重试是可选的
因此,如果我有以下简单的控制台应用程序:

类程序
{
静态void Main(字符串[]参数)
{
var retry=Policy.HandleResult(结果=>!result.CheckConnection())
.WaitAndRetryForever(=>TimeSpan.FromSeconds(15),
onRetry:(结果,持续时间)=>Console.WriteLine(“重试已启动”);
var device=新设备();
重试。执行(()=>
{
device.Connect();
返回装置;
});
Console.WriteLine(“设备已连接”);
}
}
然后将以下行打印到输出:

Retry has been initiated
Retry has been initiated
Retry has been initiated
Retry has been initiated
Retry has been initiated
Retry has been initiated
Device has been connected

停止计时?只需将
启用
设置为
错误
?如果
CheckConnection
根据是否成功返回
true
false
,则可以使用此
appeased
委托:
timer.appeased+=(s,e)=>{If(CheckConnection())timer.Stop();}查看method@GM你考虑过使用吗?停止计时器?只需将
启用
设置为
错误
?如果
CheckConnection
根据是否成功返回
true
false
,则可以使用此
appeased
委托:
timer.appeased+=(s,e)=>{If(CheckConnection())timer.Stop();}查看method@GM您是否考虑过使用?第三方组件来完成常规
可以完成的任务?真的吗?@Dennis我的假设是,为了更好地集中精力,所提供的问题范围会大大缩小。对于这个简单的用例来说,这个解决方案的确浪费了很多精力,但我怀疑,如果像这样的工具变得方便的话,确切的问题要复杂得多。第三方组件可以做常规
可以做的事吗?“真的吗?”丹尼斯我的假设是