C# ThreadTimer object to callback
In my wpf application, I used the ThreadTime as bleow.
TimerC开发者_开发问答allback tcb = new TimerCallback(TimerTask);
tmrdetectchanges = new Timer(tcb, "", 100, tmrinterval);
private void TimerTask(object data)
{
DetectChanges();
}
I just passed " " to TimerTask Parameter as I'm not using it. Is there any impact on my program? In my case , do i need to use this param? Please advise. Thanks.
From MSDN:
public delegate void TimerCallback(Object state)
state
Type: System.Object
An object containing application-specific information relevant to the method invoked by this delegate, or null.
Just pass null instead.
there is 'almost' no impact on your program. There's some but it's so minimal that you probably wouldn't even notice it. When you pass in "", it allocates a new string object on the heap so at some point garbage collector will have to find it and free it, which I'm sure it will relatively fast.
Since you are not using the user data param, just pass in null. Perfectly valid and saves those few CPU cycles (both for the call and for the garbage collection)
EDIT: As others have corrected me, there really almost pretty much no impact whatsoever passing a string instead of a null. However you will still save few CPU cycles if you pass in null. Passing in "" does generate one extra assembly instruction (at least on the calling side) that in your case would serve no purpose
Also note I only verified that in debug build and release could be optimized so there's no difference at all. I'm sure someone will correct me if that's the case :)
精彩评论