טיימר שרץ ברקע ב ASP NET

tenen

New member
טיימר שרץ ברקע ב ASP NET

אני רוצה להפעיל את הטיימר ברמת האפליקציה אבל כיצד? מה שאני יודע נכון לעכשיו זה ליצור את הטיימר בתוך global.asax
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) Dim turns As New System.Timers.Timer turns.Start() End Sub​
מה שאני לא יודע, זה היכן עלי לכתוב את הקוד שאני רוצה שירוץ בכל פעם שעוברות 5 השניות. לעזרתכם אודה, טל :).
 

tenen

New member
הכל הבנתי חוץ משתי שורות :)

ואלו בדיוק השורות שלא הבנתי שחיפשתי קטעי קוד לפני ששאלתי איך אני מתרגם את השורה הזו
Clock.Tick+=new EventHandler(myClass.myTimedProcedure);​
והשורה הזו
class myClass { public static void myTimedProcedure()​
לקוד VB NET?
 

tenen

New member
זה הקןד שיש לי בינתיים,

ומשום מה זה לא עובד, ואני מחפש את הטעות.
Public Class myClass1 Public Sub myTimedProcedure() MsgBox("hi") End Sub End Class Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs on application startup Dim Clock As New System.Timers.Timer() Clock.Interval = 1000 Clock.Start() Dim hi As New System.Timers.ElapsedEventHandler(myClass1.myTimedProcedure) End Sub​
אני מקבל הודעה שמה ששמתי בסוגריים בשורה האחרונה לא מתאים, ואני מחפש איך לגרום לזה להתאים :)
 

Justin Angel

New member
קה פרובלמה מואי צ'יקיטה?

לא קישרת את ה-Delegate שיצרת לאירוע בפועל, רק יצרת Delegate. אני ממליץ בחום שתעקוב אחרי הדוגמאות מ-MSDN: Timer Class
Imports System Imports System.Timers Public Class Timer1 Public Shared Sub Main() ' Normally, the timer is declared at the class level, so ' that it doesn't go out of scope when the method ends. ' In this example, the timer is needed only while Main ' is executing. However, KeepAlive must be used at the ' end of Main, to prevent the JIT compiler from allowing ' aggressive garbage collection to occur before Main ' ends. Dim aTimer As New System.Timers.Timer() ' Hook up the Elapsed event for the timer. AddHandler aTimer.Elapsed, AddressOf OnTimedEvent ' Set the Interval to 2 seconds (2000 milliseconds). aTimer.Interval = 2000 aTimer.Enabled = True Console.WriteLine("Press the Enter key to exit the program.") Console.ReadLine() ' Keep the timer alive until the end of Main. GC.KeepAlive(aTimer) End Sub ' Specify what you want to happen when the Elapsed event is ' raised. Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs) Console.WriteLine("Hello World!") End Sub End Class​
 

tenen

New member
תודה, לזה הצלחתי לעשות הסבה :)

ולהכניס את זה ל global.asax :) הייתי צריך להוציא הכל מהקלאס ולהכניס לתוך aplication start ואת הפרוצדורה להפוך מprivate ל public אבל העיקר שהצלחתי בסוף. באמת צריך להשתמש יותר ב msdn, ושוב, תודה :).
 
למעלה