开发者

Why doesn't System.Threading.Timer callback fire?

I'm not using a Windows Form Timer--I'm using a System.Threading.Timer. No exceptions are raised when instantiating the timer, and to my understanding, System.Threading.Timer's do not need to be explicitly started.

I've tried using a System.Timers.Timer (See comments in code) and changing the signature of the callback with no luck. Inside the callback method, I stubbed in some EventLog writes, and not even the first writes to the event log. All I see in the Event Log is MyService.OnStart fired followed by MyService started (these are both from the OnStart event). Why aren't the timer callback events firing?

public partial class MyService : ServiceBase
{

    private static System.Threading.Timer timer;

    public MyService()
    {
        InitializeComponent();
        if (!System.Diagnostics.EventLog.SourceExists("MyService"))
        {
            System.Diagnostics.EventLog.CreateEventSource(
                "MyService", "MyServiceLog");
        }
        eventLog1.Source = "MyService";
        eventLog1.Log = "MyServiceLog";
    }

    protected override void OnStart(string[] args)
    {
        eventLog1.WriteEntry("MyService.OnStart event fired");

        // setup timer to poll and execute an event
        //Timer timer = new Timer(new TimerCallback(CheckCa开发者_开发百科lls), null, 0, 10000);
        try
        {
            timer = new System.Threading.Timer(new System.Threading.TimerCallback(CheckCalls), null, 0, 10000);
        }
        catch (Exception ex)
        {
            eventLog1.WriteEntry(ex.Message + " Stacktrace: " + ex.StackTrace);
        }

        eventLog1.WriteEntry("MyServicestarted");

        GC.KeepAlive(timer);
    }
}

and the callback method, CheckCalls:

private static void CheckCalls(object objectState)
    {
        EventLog eventLog1 = new EventLog("MyServiceLog", "DAVIDWIN7", "MyService");
            eventLog1.WriteEntry("MyService is polling");
    }

Arg--For some reason when you Build->Clean Solution, then Build->Build Solution, Visual Studio does not rebuild your setup project. I believe my code above works fine, but I had tried many different fixes without explicitly right-clicking the setup project and selecting Build.

I thought Clean Solution would force all projects to be rebuilt on next build???


it's got to be an error with the timer thread writing to the event log.

I got it working on my computer but I replaced all the eventlog with Console.writeline because I didn't want to mess with my event log permissions.


Did you check if the account under which the service is running has access to the eventlog?


Please can you go and download this test application which mirrors exactly the scenario you are talking about:

https://docs.google.com/leaf?id=0Bw_NnV9fhgmgMThjNDgzOTgtODNiOC00NDE1LWEyMTYtNzVhOTMyNzlmZjZk&hl=en&authkey=CMuupNkC

I've just created this. It's a service that effectively uses the same code as you. The only difference is that I have used the toolbox to drag and drop an eventlog on to the service designer - setting the log to 'Application' and the source to 'Service1'.

Open a VS command prompt at the project folder, go to bin\debug and run installutil windowsservice1.exe - specifying some credentials (so you're installing the dev service directly from your bin output folder).

Start the service and monitor the application log - it will write every ten seconds as you expect to see from your code.

Assuming this works on your machine(tm) like it does on my machine(tm) then I would suggest that the problem is the event logging itself, or, horror of horrors - your service isn't running from your build output folder (most of the devs in my team insist on running their dev services from a different folder and then swear when they forget to actually update the binaries when they start debugging it!).

As I suggested in my comment, you really need to be able to attach the debugger to this and breakpoint the callback to verify that it's not getting fired. I never rely on the eventlog for diagnostics messages simply because the log might be full, or permissions might prevent you etc etc - it might be a windows platform component, but it's not as reliable as I would like.

The debugger, however, rarely lies (unless the deployed binaries are out of date ;) ). Compile a debug build of your project and make sure it's the one that is installed in the services list, start it up and then attach directly to the service exe (there's no point trying to attach to [service].vshost.exe - VS cannot auto-attach or host windows services as the only way they can run is via svchost.exe shelling them).

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜