Timer in C # with a different thread

   static Timer _timer;

    static void Main(string[] args)
    {
       _timer = new Timer(1000);
       _timer.Enabled = true;
       _timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

      for (int i = 0; i < 10000; i++)
      {
        string strXMLComperator = @"D:\randomFiles\rand" + i + ".txt";

        if (!File.Exists(strXMLComperator))
        {
          StreamWriter sWriter = new StreamWriter(strXMLComperator, false, Encoding.UTF8);
          sWriter.Write("<?xml version=\"1.0\" encoding=\"utf-8\"?><catalog>dasd</catalog>");
          sWriter.Flush();
          sWriter.Close();
          sWriter.Dispose();
        }
      }
    }   



     private static void OnTimedEvent(object source, ElapsedEventArgs e)
     {
       //some code here
     }

I want to know if the Main () method will add files when OnTimedEvent is working, or will it stop working while timer_event ends

+3
source share
1 answer

Well, you didn’t say which class Timeryou are using, but provided that you are not using the Windows Forms timer, then yes: the method OnTimedEventwill be called on another thread in the Main thread (the thread of the pool thread, in fact), so they will be launched simultaneously.

(Note that contrary to the title of the question, this is not another process - just a different thread.)

+4
source

Source: https://habr.com/ru/post/1766697/


All Articles