How to prevent my Timer from GC collecting before its callback excuted?

后端 未结 6 655
情深已故
情深已故 2021-01-26 06:51

I need to create a bunch of timer as local variable to do something like:

void Foo()
{
    Timer t = new Timer(myTimerCallback,null,1000,Timeout.Infinite);
}
         


        
6条回答
  •  遥遥无期
    2021-01-26 07:16

    Your best bet is to store them in a member variable and then dispose of them when you no longer need them.

提交回复
热议问题