2010-11-22 94 views
0

我有一個應用程序,我需要在每天的某個時間自動化,例如下午6點,它將運行某種方法(即檢查數據庫的關鍵術語,然後訪問api搜索這些條款)。但是,還有另一個進程一直在運行,訪問流api,所以當搜索完成時,它會中斷流並交出新條件。 現在我想添加.exe文件到Windows任務調度器,但不知道這是否會工作。流方法無限期運行,每天下午6點運行另一個進程。我想過使用system.threading.task TaskFactory,但是當我包含它時,它將taskfactory顯示爲undefined。 (我有.NET框架4.0),如果啓動使用任務計劃爲18:00我的代碼邏輯如下:在vb.net中的調度任務

 While True 
      'should i run this method as background 
      dim gStream as StreamProcess 
      gStream.module1() 


      If DateTime.Now.TimeOfDay.ToString = searchTime Then 
       addKeywordSearchRules = getSTerms.getNewTerms(addKeywordSearchRules) 
       ruleManip.ruleChanges(ruleMethods.Method.ADD, addKeywordSearchRules) 
      End If 

      Dim gSearch As SearchProcess 
      if not addKeywordSearchRules.count = 0 then 
       gSearch.module1() 
      end if 

     End While 

這是否邏輯是否合理?任何其他想法>

+0

如果您使用的是VS2008,那就是爲什麼您沒有看到TaskFactory - 您需要VS2010來定位.NET 4. – 2010-11-22 17:43:54

+0

謝謝我會忘記任務工廠,然後......但就程序邏輯而言我可以繼續嗎 – vbNewbie 2010-11-22 17:55:02

回答

1

另一種方法是使用system.windows.forms.timer對象。

  1. 將您的形式
  2. 「已啓用」屬性設置爲True
  3. 計時器對象的「間隔」屬性設置爲24小時,即(24 * 60 * 60 * 1000,因爲單位是毫秒)
  4. 在Timer對象的「tick」事件中,將其設置爲每24小時運行一次要執行的任何函數。

即像做以下,假設您的Timer對象被稱爲定時器1:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 
    'run your desired function here 
End Sub 

希望有所幫助。

編輯:下面是示例代碼,以使在一個控制檯應用程序這項工作...

Imports System.Windows.Forms 
Module Module1 
    Public WithEvents timer As New System.Windows.Forms.Timer 
    Sub Main() 
     timer.Enabled = True 
     timer.Interval = 500 'replace 500 milliseconds with 24 hours' 
     MsgBox("pause") 
    End Sub 

    Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer.Tick 
     'run your desired method here' 
    End Sub 

End Module 

的細微差別是:

  1. 你必須明確地導入 System.Windows。表格
  2. 在您的模塊中,您必須 將定時器聲明爲WithEvents,以便您的模塊得到「Tick」事件的通知

您可以先導入system.windows.forms,方法是先雙擊項目瀏覽器中的「我的項目」,然後轉到「參考」,然後單擊「添加」,然後選擇「.Net」選項卡並選擇System.windows.forms。我希望那些是正確的英文名字,因爲我使用不同語言的視覺工作室。