简体   繁体   English

我无法在窗口服务中创建线程

[英]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. 只需提供一项每10秒执行一次操作的服务即可。 At first I have used System.Timers.Timer for it. 首先,我使用了System.Timers.Timer。 and all was ok on my PC. 在我的电脑上一切正常。 But I had faced with problem on Windows Server 2003 server of my customer. 但是我在客户的Windows Server 2003服务器上遇到了问题。 System.Timers.Timer timer1_Elapsed not firing and I have changed this timer for System.Threading.Timer and I see the same picture. System.Timers.Timer timer1_Elapsed没有触发,我为System.Threading.Timer更改了此计时器,我看到的是同一张图片。 It works on my PC and doesn't work on customer's server. 它可以在我的PC上运行,而不能在客户的服务器上运行。 After I drop all both these timers and have used a BackgroundWorked for this task Code: 在删除所有这两个计时器并为该任务代码使用BackgroundWorked之后:

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. 在我的PC上一切正常,但在Server 2003 comp上不起作用。 I am seeing a test 1, test 2 and test 3 log messages and doesn't see 'test 4'. 我看到测试1,测试2和测试3的日志消息,但没有看到“测试4”。 It seems that the BackgroundWorker doesn't execute fBg_DoWork. 似乎BackgroundWorker不执行fBg_DoWork。

What is the cause? 原因是什么? Any ideas? 有任何想法吗?

Are u getting log before 你之前有日志吗

lock (EventEDILog) 锁(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. 因为如果EventEDILog被另一个线程锁定且从不解锁,则您的BackgroundWorker将被阻止,以便EventEDILog解锁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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