开发者

Timer in C# with 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, Elap开发者_运维知识库sedEventArgs e)
     {
       //some code here
     }

i want to know will Main() Method add files when OnTimedEvent works or it will stop working while timer_event finish


Well, you haven't said which Timer class you're using, but assuming you're not using the Windows Forms timer, then yes: the OnTimedEvent method will be called in a different thread to the Main thread (a thread-pool thread, actually), so they'll run concurrently.

(Note that contrary to your question title, this isn't a different process - just a different thread.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜