首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FluentScheduler不工作

FluentScheduler不工作
EN

Stack Overflow用户
提问于 2016-03-28 16:12:16
回答 1查看 2.3K关注 0票数 1

我尝试在windows服务中使用.Net中的TopShelf和FluentScheduler每10秒触发一次事件,但我并不是每10秒触发一次事件。我正在分享我的实现,请指导我。

代码语言:javascript
复制
class Program
    {        
        static void Main(string[] args)
        {
            HostFactory.Run(x =>
            {
                x.Service<IWindowsService>(s =>
                {
                    s.ConstructUsing(name => new WindowsService(new SchedulerRegistry(new Worker())));
                    s.WhenStarted(tc => tc.Start());
                    s.WhenStopped(tc => tc.Stop());
                });

                x.RunAsLocalSystem();

                x.SetDescription("Test");
                x.SetDisplayName("Test Service");
                x.SetServiceName("Testservice");

                x.StartAutomatically();

                x.EnableServiceRecovery(s =>
                {
                    s.RestartService(1);
                    s.RestartService(2);
                });
            });
        }
    }

    public class SchedulerRegistry : Registry
    {
        public SchedulerRegistry(Worker worker)
        {
            Schedule(() =>
            {
                try
                {
                    worker.Run();
                }
                catch (Exception ex)
                {

                    throw;
                }
            }).NonReentrant().ToRunNow().AndEvery(10).Seconds();
        }
    }

    public interface IWindowsService
    {
        void Start();
        void Stop();
    }

    public class WindowsService : IWindowsService
    {
        public WindowsService(SchedulerRegistry registry)
        {
            JobManager.Initialize(registry);
        }

        public void Start()
        {
            Console.WriteLine("Service started");
        }

        public void Stop()
        {
            Console.WriteLine("Service stopped");
        }
    }

    public class Worker
    {
        public void Run()
        {
            CheckUrl();
        }

        public static void CheckUrl()
        {
            HttpWebResponse response = null;

            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://google.com");
                request.Method = "GET";

                response = (HttpWebResponse)request.GetResponse();

                if (response.StatusCode == HttpStatusCode.OK)
                {
                }
                else
                {
                }
            }
            catch (WebException e)
            {
                response.Close();
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }
        }

    }
EN

回答 1

Stack Overflow用户

发布于 2016-04-02 23:33:57

通过执行以下操作,我能够让它运行:

代码语言:javascript
复制
public class WindowsService : IWindowsService
{
    private SchedulerRegistry registry;

    public WindowsService(SchedulerRegistry schedulerRegistry)
    {
        this.registry = schedulerRegistry;  
    }

    public void Start()
    {
        Console.WriteLine("Service started");

        JobManager.Initialize(this.registry);
    }

    public void Stop()
    {
        Console.WriteLine("Service stopped");
    }
}

基本上,我所做的就是更改JobManager.Initialize(this.registry)的调用位置。我不认为你想让它运行,直到你的服务启动,所以我认为这是有意义的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36258450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档