System.Threading.Timer没有触发,global.aspx

时间:2011-08-04 19:21:41

标签: vb.net multithreading global-asax

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

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

我的代码来自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

结束班

的Global.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>

1 个答案:

答案 0 :(得分:0)

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