1

I'm trying to use the timer class as stated in this question:

What is the best way to implement a "timer"?

using System.Timers;

_

 System.Timers.Timer aTimer = new System.Timers.Timer();
 aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
 aTimer.Interval = 1;
 aTimer.Enabled = true;
 aTimer.Start();
 void OnTimedEvent(object source, ElapsedEventArgs e)
 {
 Text += "hello world";
 }

but it not only doesn't work, it also gives an error message in Microsoft Visual Studio when I close the application.

enter image description here

The target process was terminated with code 0 by evaluating the function 'System.AppDomain.IsAppXModel'.

If the problem happens regularly consider disabling the Tools -> Options, "Debugging -> General -> Enable property evaluation and other implicit function calls" or change the code to disable evaluation of this method. See help for information on doing this.

karel
  • 5,489
  • 46
  • 45
  • 50
Thiago
  • 332
  • 1
  • 3
  • 16
  • 5
    I don't know what `Text` is but my guess is that you need to synchronize it with the UI thread: [If you use the System.Timers.Timer class with a user interface element, such as a form or control, without placing the timer on that user interface element, assign the form or control that contains the Timer to the SynchronizingObject property, so that the event is marshaled to the user interface thread.](https://learn.microsoft.com/en-us/dotnet/api/system.timers.timer?view=netcore-3.1#remarks) – Crowcoder Jun 27 '20 at 12:38
  • 4
    The code you linked is for a Console app. In a WinForms app, you may want to try the *dedicated* `System.Windows.Forms.Timer` instead. Its Tick event is raised in the UI Thread. You could translate the exception message, though. It doesn't look like a simple cross-thread exception. – Jimi Jun 27 '20 at 14:12

0 Answers0