如题“System.Timers.Timer 嵌套 System.Windows.Forms.Timer的问题”,最近在项目中在类uc_Map中启用了System.Timers.Timer,并在Timer的Timer_Elapsed方法中需要启动或停止GMapMarkerDirection markerPlane类中的System.Windows.Forms.Timer,但是并没用在uc_Map启动/停止成功,经排查,需要在启动或停止markerPlane中的方法时需要加委托Invoke,如下:
uc_Map中的Timer_Elapsed方法:
this.Invoke(new Action(delegate{if (isWarnData){ markerPlane.StartFlash(); }elsemarkerPlane.StopFlash();}));
markerPlane类中的方法:
/// <summary>/// 开始闪烁/// </summary>public void StartFlash(){if (bIsFlash == false){flashTimer.Start();bIsFlash = true;}}/// <summary>/// 停止闪烁/// </summary>public void StopFlash(){if (bIsFlash == true){flashTimer.Stop();if (FlashPen != null){FlashPen.Dispose();FlashPen = null;}if (this.Overlay.Control.InvokeRequired){this.Overlay.Control.Invoke(new Action(delegate{this.Overlay.Control.Refresh();}));}elsethis.Overlay.Control.Refresh();bIsFlash = false;}}
这样就能正常启动/停止第二个Timer