简体   繁体   中英

I can not create thread in the window service

I am at an impasse. Just make a service which does something every 10 sec for example. At first I have used System.Timers.Timer for it. and all was ok on my PC. But I had faced with problem on Windows Server 2003 server of my customer. System.Timers.Timer timer1_Elapsed not firing and I have changed this timer for System.Threading.Timer and I see the same picture. It works on my PC and doesn't work on customer's server. After I drop all both these timers and have used a BackgroundWorked for this task Code:

public partial class XXXService : ServiceBase
{
    public XXXService()
    {
        InitializeComponent();



        if (!System.Diagnostics.EventLog.SourceExists("XXXSource1"))
            System.Diagnostics.EventLog.CreateEventSource("XXXSource1", "XXXLog");
        EventXXXLog.Source = "XXXSource1";
        EventXXXLog.Log = "XXXLog";
    }

    private ManualResetEvent threadSleepEvent = new ManualResetEvent(false);
    private BackgroundWorker fBg = new System.ComponentModel.BackgroundWorker();

    protected override void OnStart(string[] args)
    {
        EventXXXLog.WriteEntry("XXX Service has started.");

        //fTimer.Enabled = false;
        //fTimer.Interval = 1000;
        //fTimer.Elapsed += new System.Timers.ElapsedEventHandler(fTimer_Elapsed);
        //fTimer.Start();

        //fTimer = new System.Threading.Timer(new System.Threading.TimerCallback(fTimer_Elapsed), null, 1000, 5000);

        fBg.DoWork += new System.ComponentModel.DoWorkEventHandler(fBg_DoWork);

        EventXXXLog.WriteEntry("XXX Service has started. test 2");
        fBg.RunWorkerAsync();
        EventXXXLog.WriteEntry("XXX Service has started. test 3");
    }

    void fBg_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {
        //EventXXXLog.WriteEntry("test 4");
        lock (EventXXXLog)
        {
            EventXXXLog.WriteEntry("test 4");
        }
    }
}

All the same. All is ok on my PC, but it doesn't work on Server 2003 comp. I am seeing a test 1, test 2 and test 3 log messages and doesn't see 'test 4'. It seems that the BackgroundWorker doesn't execute fBg_DoWork.

What is the cause? Any ideas?

Are u getting log before

lock (EventEDILog)

Because if EventEDILog is being lock by another thread and never unlock then your BackgroundWorker will be blocked for the EventEDILog to get unlocked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM