开发者

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?

Startin开发者_运维百科g 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...


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.)


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜