【问题标题】:Thread in Global.asax does not work after publishing the web siteGlobal.asax 中的线程在发布网站后不起作用
【发布时间】:2012-01-20 16:03:11
【问题描述】:

我正在开发一个使用 Global.asax 实现作业调度程序的 Web 应用程序。我已经使用线程在特定的时间内开始工作。

如果我通过 Visual Studio 运行应用程序,它可以正常工作。但是当我将它发布并部署到服务器时,线程不起作用。

请帮我解决这个问题。有没有其他选择。

请在下面找到示例代码,

protected void Application_Start()
    {
        Thread thread = new Thread(new ThreadStart(ThreadFunc));
        thread.IsBackground = true;
        thread.Name = "ThreadFunc";
        thread.Start();
    }

    protected void ThreadFunc()
    {
        System.Timers.Timer t = new System.Timers.Timer();
        t.Elapsed += new System.Timers.ElapsedEventHandler(TimerWorker);
        t.Interval = 10000;
        t.Enabled = true;
        t.AutoReset = true;
        t.Start();
    }

    protected void TimerWorker(object sender, System.Timers.ElapsedEventArgs e)
    {
        //work args
    }

谢谢,

维杰

【问题讨论】:

  • @Ravi - 请查找已编辑的示例代码。
  • 看起来不错。可能是IIS问题??为什么你不能使用服务而不是线程?作为@MK。 anwserd 在他的链接中。

标签: asp.net publish global-asax web-deployment


【解决方案1】:

我认为在 global.asax 中生成线程不是一个好主意。请参阅此question 以获得更好的解决方案。

【讨论】:

  • 现在我写了一个windows服务来执行调度任务。
【解决方案2】:

如果您的网站在一段时间内没有任何活动,则会自动关闭。

为了规避这个问题,谷歌“保持 asp.net 网站活着”

但是你会发现一堆像 hacks 一样的胶带。我会改变架构。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多