Raskaroth
Raskaroth

Reputation: 659

c# FileSystemWatcher and a Timer

I have this strange, yet maybe very simple problem..

I have a timer1 in my program that should start when I for example click a button. which it does..

However when I use the FileSystemWatch it does not trigger timer1 for some reason I can't seem to figure out.. is there something special that prevents the timer from being triggered?

Starting time works here:

private void toolStripMenuItem2_Click(object sender, EventArgs e)
    {
        timer1.Start();
    }

but here it does not..

        private void fsw_SS_Created(object sender, FileSystemEventArgs e)
    { 
        fsw_SS.EnableRaisingEvents = false;
        timer1.Start(); 
    }

Intellisense of Visual Studio does not show any problems, neither can i seem to find a solution out there.

timer settings are as followed: Interval of 5000ms, and a timer_tick event (setup properly).

My problem is either 1) the timer doesn't start or 2) it doesn't tick..

What is wrong here, as I said before using a manual button or tool strip menue item it starts fine...

Upvotes: 0

Views: 1081

Answers (2)

James
James

Reputation: 82136

I don't think it's a good idea to use a WinForms timer in this particular scenario as @Raymond has pointed out you are dealing with different threads and may encounter unexpected behaviour. You should consider using System.Threading.Timer instead.

Upvotes: 0

Raymond Chen
Raymond Chen

Reputation: 45172

Observe that the events are raised on a background thread, which means that you are accessing the timer from the wrong thread. (You never said what timer1 is, but I'm guessing it has thread affinity.)

Upvotes: 1

Related Questions