List:自定义对象如何从列表中删除自身?

发布于 2024-11-17 00:07:08 字数 552 浏览 1 评论 0原文

我创建了一个类,它实现了带有 elapse 事件的 System.Timers.Timer 。


private void ElapseEvent(object source, ElapsedEventArgs e){
//read from serial port device
}

在主程序中,我有一个 private List; _计时器

我动态启动计时器并将其添加到计时器列表中。


 var timer1= new MyTimerClass();
 timer1.Execute("Device A");
_timers.Add(timer1);

 var timer2= new MyTimerClass();
 timer2.Execute("Device B");
_timers.Add(timer2);

ElapseEvent遇到错误并且串口断开连接时,我想从列表中删除计时器。我怎么可能这么做呢?

I created a class that implements a System.Timers.Timer with an elapse event.


private void ElapseEvent(object source, ElapsedEventArgs e){
//read from serial port device
}

In the main program I have a private List<MyTimerClass> _timers.

I dynamically start the timer and add it to the timer list.


 var timer1= new MyTimerClass();
 timer1.Execute("Device A");
_timers.Add(timer1);

 var timer2= new MyTimerClass();
 timer2.Execute("Device B");
_timers.Add(timer2);

When the ElapseEvent encounters an error and the serial port was disconnected, I want to remove the timer from the List. How can I possibly do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

葵雨 2024-11-24 00:07:08

您可以在包含计时器的类中添加 OnError 方法。为了让父类知道发生了错误。类似于以下内容:

在 MyTimerClass

public MyTimerClass(Action<MyTimerClass, Exception> onError)
{
    this.onError = onError;
}

private void ElapseEvent(object source, ElapsedEventArgs e){
try{
//read from serial port device
}
catch(Exception ex) // It would be better if you know the exceptions you want to handle
{
  if(this.onError != null)
  {
   this.onError(this, ex);
  }
}
}

在父类中:

var timer1= new MyTimerClass(this.OnTimerError);
 timer1.Execute("Device A");
_timers.Add(timer1);

 var timer2= new MyTimerClass(this.OnTimerError);
 timer2.Execute("Device B");
_timers.Add(timer2);



public void OnTimerError(MyTimerClass timer, Exception error)
{
 //Log the exception
  _timers.Remove(timer);
}

You can add a OnError Method at the class that contains the timers. In order to let the parent class know that an error occurs. Something like the following:

At MyTimerClass

public MyTimerClass(Action<MyTimerClass, Exception> onError)
{
    this.onError = onError;
}

private void ElapseEvent(object source, ElapsedEventArgs e){
try{
//read from serial port device
}
catch(Exception ex) // It would be better if you know the exceptions you want to handle
{
  if(this.onError != null)
  {
   this.onError(this, ex);
  }
}
}

At the parent class:

var timer1= new MyTimerClass(this.OnTimerError);
 timer1.Execute("Device A");
_timers.Add(timer1);

 var timer2= new MyTimerClass(this.OnTimerError);
 timer2.Execute("Device B");
_timers.Add(timer2);



public void OnTimerError(MyTimerClass timer, Exception error)
{
 //Log the exception
  _timers.Remove(timer);
}
dawn曙光 2024-11-24 00:07:08

如果您的 _timers 是静态变量,则在同一类中创建静态事件处理程序。当计时器的 ElapseEvent 中引发错误时,调用引用其自身的事件处理程序,并让静态事件处理程序从列表中删除出错的计时器。

If your _timers is a static variable, then create a static event handler in the same class. When the error is thrown in a timer's ElapseEvent, call the event handler with reference to itself, and have the static event handler remove the error'ed timer from the list.

清眉祭 2024-11-24 00:07:08

您应该查看命令模式;听起来这对你来说是完美的。

You should check out the Command Pattern; sounds like it would be perfect for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文