An application with a thread and a timer.

Joined
Jul 14, 2016
Messages
6
Programming Experience
10+
I'm trying to write a simple console program.
There is a class that runs in a thread and I want to call a method in that class from a timer event.

For the timer to access the thread I needed to make the the thread class a member of the class as well as the timer...
Then things wouldn't compile anymore...

Help?
Brian.
 

Attachments

  • Program.cs.zip
    694 bytes · Views: 54
That doesn't really make sense. Classes - or instances of them - don't run in a thread. Class instances just sit in memory. It's their methods that get executed and it's quite possible to have multiple methods of the same object executing simultaneously on different threads. The concept of an object belonging to a thread really only exists because Windows GUI applications create a control handle on a particular thread and then any changes that use that handle must be executed on that same thread. Even then though, it's possible to execute a method of a control on other than the UI thread as long as it doesn't touch the handle.

Apart from that, Timers and multi-threading in a Console app doesn't really sound like a good idea to begin with. Console apps are basically sequential so having something else happening when the main program could terminate is generally a bad idea. What is it that you are actually trying to achieve for which you think that this is a good idea? Most like there's a better way to do it.
 
I have an event driven thread that needs to be interrupted and told to clear some unused objects from memory.
It's working perfectly but I haven't added the timer... Maybe instead of waiting for a key in Main I can sleep wake up and tell the threaded class to clean up?
Source code was provided...
 
Last edited:
I'm not interested in source code. I'm interested in your explanation of what you're trying to achieve, not how you're trying to achieve it. Code that doesn't work is never the best way to determine what someone is trying to do. If you'd rather wait for someone else who does want to read your code then that's your prerogative.
 
I don't know how better to explain what it is i'm doing.. I provided an explanation and maybe 25 lines of code as an example.
I feel you when you say don't use console.. but this is a headless unix server running on top of Mono and .NET
The 'real' server is running perfectly well at the moment but I need to add a timer and want to make sure I'm doing the threading properly.
TIA.
 
I am going to go out on a limb here...

You cannot access non-static methods or non-static members from static methods. Therefore, Main() is static as well as the HandleTimer().

These:

        public MyServer server { get; set; }
        public System.Timers.Timer timer { get; set; }

are non-static.


Additionally the HandleTimer() does not return anything, and it is expecting a 'Task' return.

Furthermore, this: timer = new Timer(1000); is ambiguous. The compiler is confused. Do you want System.Timers.Timer or System.Threading.Timer. You need to explicitly state timer = new System.Timers.Timer(1000);. This assumes that this is what you want, considering that timer = new System.Threading.Timer(1000); would not compile, because the System.Threading.Timer does not contain a constructor with a single integral parameter.

The ambiguaty is due to the fact that you are using both System.Timers and System.Threading.



*Edit

Thought of this this morning. You could use an alias to solve the collision of System.Timers and System.Threading.

Ex.
using System;using System.Threading;
using System.Threading.Tasks;
using System.Timers;


namespace TImerTestWithThread
{
    using TimerFromSysTimer = System.Timers.Timer;


    class MainClass
    {
        public static TimerFromSysTimer timer { get; set; }


        public static void Main(string[] args)
        {
            timer = new TimerFromSysTimer(1000);
        }
    }
}


This may make your code more confusing, but it is a legitimate option.
 
Last edited:
Back
Top Bottom