Windows服务Thread.isBackground = true过早终止应用程序

时间:2014-10-21 06:35:52

标签: c# multithreading

我正在尝试创建一个Windows服务,它定期从数据库中查询数据并根据数据执行任务。 我使用了线程,以下代码在运行后立即终止。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace MESUDeleteService
{
    public partial class MESDeleteService : ServiceBase
    {
        AutoResetEvent StopRequest = new AutoResetEvent(false);
        private Thread _thread;

        public MESDeleteService()
        {
            InitializeComponent();
        }

        public void startConsole()
        {
            OnStart(null);
        }

        protected override void OnStart(string[] args)
        {
            MESUDeleteService.Core.Logger.Writelog.Info("Deletion Service Start");
            _thread = new Thread(WorkerThreadFunc);
            _thread.IsBackground = true;
            _thread.Start();
        }

        public void WorkerThreadFunc()
        {
            for (; ; )
            {
                if (StopRequest.WaitOne(10000)) return;
                try
                {
                    SNDX.DataAccess.UnitOfWork uow = new SNDX.DataAccess.UnitOfWork();

                    var deleteOrderCollection = uow.DeleteSettingsRepository
                        .Get()
                        .Where(m => m.STATUS == "ACTIVE" && m.NEXTRUN != null)
                        .Where(n => n.NEXTRUN.Value.ToString("{0:yyyyMMddHHmm}") == DateTime.Now.ToString("{0:yyyyMMddHHmm}"))
                        .ToList<SNDX.DataAccess.SND_DELETE_SETTINGS>();

                    if (deleteOrderCollection.Count > 0)
                    {
                        MESUDeleteService.Core.Logger.Writelog.Info("There'r new Delete Orders");

                        //process each delete orders
                        foreach (SNDX.DataAccess.SND_DELETE_SETTINGS x in deleteOrderCollection)
                        {
                            MESUDeleteService.Core.Logger.Writelog.Info("Delete Type : " + x.SETTINGS);
                            MESUDeleteService.Core.DeleteOrder deleteOrder = new MESUDeleteService.Core.DeleteOrder();
                            deleteOrder.Settings = x.SETTINGS;
                            deleteOrder.SearchPattern = x.SEARCHPATTERN;
                            deleteOrder.NetworkPath = x.NETWORKPATH;
                            deleteOrder.EmailList = x.EMAILLIST;
                            deleteOrder.ID = x.ID;
                            deleteOrder.LastRun = DateTime.Now;
                            deleteOrder.WeekRun = x.WEEKRUN;
                            deleteOrder.ComputerName = x.COMPUTERNAME;
                            deleteOrder.DomainName = x.DOMAINNAME;
                            deleteOrder.ADAccount = x.ADACCOUNT;
                            deleteOrder.ADPassword = x.ADPASSWORD;

                            //by having them in a thread.                            
                            Thread t = new Thread(new ThreadStart(deleteOrder.ThreadStart)) { IsBackground = true };
                            t.IsBackground = true;
                            MESUDeleteService.Core.Logger.Writelog.Info("Delete Thread Starting Type");
                            t.Start();
                        }
                    }
                    uow.Dispose();
                }
                catch (Exception ex)
                {
                    MESUDeleteService.Core.Logger.Writelog.Error(ex.Message);
                }
            }
        }

        protected override void OnStop()
        {
            StopRequest.Set();
            _thread.Join();
        }
    }
 }

但是,如果我删除 isBackground = true ,它将正常运行,但会消耗大量的CPU能力。

使用 isBackground = true ,服务/应用终止@

 SNDX.DataAccess.UnitOfWork uow = new SNDX.DataAccess.UnitOfWork();

如何优化此代码?

0 个答案:

没有答案