2

I am working on a program where I need to activate a timer from a different thread. I've shortened the code to this example below.

static class Program
{
    public static System.Windows.Forms.Timer TimerVideo = new System.Windows.Forms.Timer();

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        TimerVideo.Interval = 1000;
        TimerVideo.Tick += new EventHandler(TimerVideo_Tick);
        new Thread(() =>
        {
            test RunTimer = new test();
            RunTimer.StartTimer();

        }).Start();

    Application.Run();
    }

    private static void TimerVideo_Tick(object sender, EventArgs e)
    {
        Console.WriteLine("Running");
    }
}

class test
{
    public void StartTimer()
    {
        Program.TimerVideo.Start();
    }
}

Even if I have to code the timer in the class that would be fine with me, but I did try that and it didn't work either, namely the timer did not fire and nothing was printed out to the console.

Ohad Schneider
  • 36,600
  • 15
  • 168
  • 198
Rachel Dockter
  • 946
  • 1
  • 8
  • 21
  • 3
    Are you sure you don't want to use a `System.Timers.Timer` instead of a `System.Windows.Forms.Timer`? – itsme86 Jul 11 '16 at 17:41
  • What error are you getting? – Dean Fenster Jul 11 '16 at 17:43
  • 2
    You haven't asked a question here. "It didn't work" is nothing that anyone can use to diagnose your problem. Say what the problem is, and then ask a question about it please. You may wish to read http://stackoverflow.com/help/how-to-ask – Eric Lippert Jul 11 '16 at 17:43
  • Before immediately voting to close, I think it's fair to wait for a response to Eric's and itsme86's questions. @Rachel - In addition to their questions, is this a console application or WinForms - it looks like a WinForm app but you said there's no form in your other comment. Could you please clarify why there's no form? – keyboardP Jul 11 '16 at 17:47
  • long story short its a hidden program so no forms but i want to use some of the forms features such as the timers, textboxes and message boxs. so its a win form but all the code is on the program.cs – Rachel Dockter Jul 11 '16 at 17:48
  • also i did eric, i said i cant start a timer from a different thread, meaning the question was effectivily "how do i start a timer from a different thread" – Rachel Dockter Jul 11 '16 at 17:49
  • The program design is very suspect. That 's probably what makes this question so odd. – sstan Jul 11 '16 at 17:49
  • @RachelDockter - This sounds like a bad approach. If there is no form to be seen, how will the textboxes be used? (you can still create messageboxes in Console apps, [for example](http://stackoverflow.com/questions/29326042/show-message-box-in-net-console-application)? – keyboardP Jul 11 '16 at 17:53
  • for example i call a class that collects information, i declare a textbox at the top then return the contents when its done. I just find it more easier to work with than a string – Rachel Dockter Jul 11 '16 at 17:55
  • As "mean" as Eric's comment may seem, the more specific you specify a question, the more accurate the response and the more you understand the issue and, because of that, the answer. – C.Evenhuis Jul 11 '16 at 17:56
  • @Eli Arbel: I wasn't questioning the lack of an MCVE. I'm just saying that the way she's using controls from the `WinForms` namespace without actually creating a form is very odd and probably the wrong way of doing things. And I think that the weirdness of it threw us off somewhat. And after reading a later comment from OP where she says that she prefers to use a `textbox` over a `string` kind of confirms what I suspected. – sstan Jul 11 '16 at 18:07
  • 1
    @EliArbel: "The timer doesn't fire and no exception is thrown" is a clear description of a problem. "The following exception is thrown" would be another. "The timer fires but only in 30% of the times I run it under the following conditions" would be another. There are an infinite number of ways that something could "not work" and it is by no means obvious which one is intended. Don't make the people you're asking for help have to do your work to help you. – Eric Lippert Jul 11 '16 at 18:20
  • 1
    @RachelDockter: "I can't start a timer from a different thread" is a statement of fact, not a question, and that statement does not imply the question. If I told you a fact, say, "I can't speak Japanese" what question does that imply? Does it imply the question "how can I learn to speak Japanese?" or "Where can I hire a Japanese translator?" or "are there any programs that can translate Japanese?" or "Is this documentation available in English?" Or... there are infinitely many questions that could be the consequence of a statement. **Ask the question**. Don't make us guess. – Eric Lippert Jul 11 '16 at 18:22

2 Answers2

7

System.Windows.Forms.Timer is meant to be run from a UI thread. If you want a timer that fires on a different thread, you can use System.Timers.Timer, e.g.:

TimerVideo = new System.Timers.Timer();
TimerVideo.Interval = 1000;
TimerVideo.Elapsed += (o, e) => Console.WriteLine("Running");
TimerVideo.Start();

See also Timers in .NET documentation.

Eli Arbel
  • 22,391
  • 3
  • 45
  • 71
  • See also Comparing the Timer Classes in the .NET Framework Class Library ([CHM File](http://download.microsoft.com/download/3/a/7/3a7fa450-1f33-41f7-9e6d-3aa95b5a6aea/MSDNMagazineFebruary2004en-us.chm) or [HTML File](https://web.archive.org/web/20150329101415/https://msdn.microsoft.com/en-us/magazine/cc164015.aspx)) from MSDN Magazine. – Brian Jul 11 '16 at 18:30
  • If you are happy with an answer, please consider [accepting](http://stackoverflow.com/help/accepted-answer) it..! – TaW Jul 11 '16 at 19:26
-2

Since the timer was created in other thread, you need to call it's methods using Invoke.

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/zyzhdc6b(v=vs.110).aspx

  • i cant invoke as there is no forms – Rachel Dockter Jul 11 '16 at 17:40
  • @RachelDockter: What do you mean "there is no forms"? Your code says that you are creating a forms timer. That thing is a thread-affinitized control. If that's not what you want then don't create a forms timer in the first place. – Eric Lippert Jul 11 '16 at 17:42