System.Threading.Timer callback is never called

docaholic

My System.Threading.Timer (which has a callback) never fires reliably. This is part of my programming assignment where I input the amount of time the timer is supposed to run from a textbox.

The timer is declared like this:

System.Threading.Timer timer = new System.Threading.Timer(WorkerObject.callback, null, delay, Timeout.Infinite);

And the delay is the simply an int describing the delay for the callback to fire the first time (it is only supposed to fire once).

The callback method is like this:

 public static void callback(Object stateinfo)
 {
     stop = true;
 }

And all that does is set a flag to true which stops a loop (which is being run by a thread on a ThreadPool, in effect, stopping the thread).

The loop looks like this:

while (!stop)
{
    currentTextbox.Invoke(new Action(delegate()
    {
        currentTextbox.AppendText((counter++) + Environment.NewLine);
        currentTextbox.Update();
     }));
}

My problem is that the stop variable is always false for any delay over 5000 milliseconds. Is there a way to "force" the callback to always fire?

Lasse V. Karlsen

You need to hold on to the reference to the timer.

Most likely the timer object is being garbage collected, which will run its finalizer, stopping the timer.

So hold on to the reference for as long as you need the timer to be alive.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pass System.Threading.Timer object reference to its callback function

From Dev

System.Threading.Timer Calling an Implicit Duplicate of a Callback Function?

From Dev

MongoClient callback never called

From Dev

Overlap callback never called

From Dev

acquireToken callback never called

From Dev

Threading Timer doesn't callback

From Dev

HttpWebRequest BeginGetRequestStream callback never called

From Dev

Wait that the Threading.Timer callback function is ending

From Dev

Why is my timer Observable never called?

From Java

System.Timers.Timer vs System.Threading.Timer

From Dev

SetWindowsHookEx succeed but callback function is never called

From Dev

google api client callback is never called

From Dev

caolon async - callback never gets called

From Dev

ajax complete callback function is never called

From Dev

Project tango Never called the OnXYZijAvailable Callback

From Dev

My callback method is never called using an interface

From Dev

Asynchronous Callback in NSOperation inside of NSOperationQueue is never called

From Dev

Http callback is never called, Vue Laravel

From Dev

C# Winforms System.Threading.Timer

From Dev

System.Threading.Timer low level implementation

From Dev

System.Threading.Timer stops the thread?

From Dev

System.Threading callback does not seem that accurate

From Dev

System.Threading.Timer vs System.Threading.Thread.Sleep resolution - .NET Timer not using system clock resolution

From Dev

System.Threading.Timer vs System.Threading.Thread.Sleep resolution - .NET Timer not using system clock resolution

From Dev

Why is the OnError callback never called when throwing from the given subscriber?

From Dev

Callback never called in child_process.exec with redis

From Dev

Callback function never called after Mongoose query is executed

From Dev

CoreAudio AudioQueue callback function never called, no errors reported

From Dev

chrome.tabs.create - callback function never called

Related Related

  1. 1

    Pass System.Threading.Timer object reference to its callback function

  2. 2

    System.Threading.Timer Calling an Implicit Duplicate of a Callback Function?

  3. 3

    MongoClient callback never called

  4. 4

    Overlap callback never called

  5. 5

    acquireToken callback never called

  6. 6

    Threading Timer doesn't callback

  7. 7

    HttpWebRequest BeginGetRequestStream callback never called

  8. 8

    Wait that the Threading.Timer callback function is ending

  9. 9

    Why is my timer Observable never called?

  10. 10

    System.Timers.Timer vs System.Threading.Timer

  11. 11

    SetWindowsHookEx succeed but callback function is never called

  12. 12

    google api client callback is never called

  13. 13

    caolon async - callback never gets called

  14. 14

    ajax complete callback function is never called

  15. 15

    Project tango Never called the OnXYZijAvailable Callback

  16. 16

    My callback method is never called using an interface

  17. 17

    Asynchronous Callback in NSOperation inside of NSOperationQueue is never called

  18. 18

    Http callback is never called, Vue Laravel

  19. 19

    C# Winforms System.Threading.Timer

  20. 20

    System.Threading.Timer low level implementation

  21. 21

    System.Threading.Timer stops the thread?

  22. 22

    System.Threading callback does not seem that accurate

  23. 23

    System.Threading.Timer vs System.Threading.Thread.Sleep resolution - .NET Timer not using system clock resolution

  24. 24

    System.Threading.Timer vs System.Threading.Thread.Sleep resolution - .NET Timer not using system clock resolution

  25. 25

    Why is the OnError callback never called when throwing from the given subscriber?

  26. 26

    Callback never called in child_process.exec with redis

  27. 27

    Callback function never called after Mongoose query is executed

  28. 28

    CoreAudio AudioQueue callback function never called, no errors reported

  29. 29

    chrome.tabs.create - callback function never called

HotTag

Archive