mike
mike

Reputation: 100

C# Timer getting stopped after some time

I am using this code for a C# service, "SomeFuction" should run after every 6 minutes, but after some 30 minutes its getting stopped.

What I'm missing here? I want it to run till my service is running, please help :(

var timer = new System.Threading.Timer((e) =>
        {
            try
            { SomeFunction(arg1, arg2, arg3); }
            catch
            { return;}

        }, null, 0, 360000);

Upvotes: 1

Views: 1264

Answers (1)

Manuel Zametter
Manuel Zametter

Reputation: 309

Could it be that the Timer gets disposed by the GarbageCollector? It seems like you only declare the variable holding the Timer-Reference in a local scope.

To prevent GarbageCollection, make sure that the Timer-Reference gets stored in an Object that exists for the entire lifetime of the Service.

See this post for a similar issue: Why does System.Threading.Timer stop on its own?

Upvotes: 4

Related Questions