【问题标题】:System.Threading.Timer not firing, global.aspxSystem.Threading.Timer 未触发,global.aspx
【发布时间】:2011-08-04 19:21:40
【问题描述】:

我是新手,但我无法让这段代码正常工作。 FileSweeper 应该启动一个在 Web 服务器上触发 fileCopy 的 timmer。 fileSweeoer 由 global.asax 触发。 FileCopy 然后将复制文件。但是 FC.copy 永远不会触发。任何帮助/解释都会有所帮助!

如果这是一个运行在网络服务器上的类,会有什么不同吗?

我的代码来自http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx

Imports Microsoft.VisualBasic
Imports System
Imports System.Threading

Public Class fileSweeper

Dim stateTimer As Timer

<MTAThread()> _
Sub Main()
    Dim FC As New fileCopy
    Dim tcb As TimerCallback = AddressOf FC.Copy

    stateTimer = New Timer(tcb, "", 20000, 200000)
    GC.KeepAlive(stateTimer)
End Sub

结束类

全球.asax:

<%@ Application Language="VB" %>

<script runat="server">

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

End Sub

Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
    ' Code that runs on application shutdown
End Sub

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    ' Code that runs when an unhandled error occurs
End Sub

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
    ' Code that runs when a new session is started
    Dim FS As New fileSweeper
    FS.Main()
End Sub

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
    ' Code that runs when a session ends. 
    ' Note: The Session_End event is raised only when the sessionstate mode
    ' is set to InProc in the Web.config file. If session mode is set to StateServer 
    ' or SQLServer, the event is not raised.
End Sub

</script>

【问题讨论】:

  • 你在哪里启用计时器?
  • @Micheal Eakins 你是什么意思,你能澄清一下吗? THX
  • 这是一个网站吗?如果有,您是否有 Web 表单?必须启动 Session_Start 事件。我已经测试了您的代码,如果 Session_Start 事件触发,那么您的代码确实可以工作。

标签: vb.net multithreading global-asax


【解决方案1】:

您可能会在这里得到意想不到的结果,因为您可能会在 Web 服务器上获得多个会话。您可能需要考虑创建一个控制台应用程序并使用 Windows 任务计划程序。

【讨论】:

    猜你喜欢
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多