2017-10-17 55 views
0

我知道網站上的很多示例都是用C#編寫的,但是我想調用Quartz的.NET函數/方法,並且使用VB.Net。如何調用Quartz的.NET函數/方法

許多例子說明如何打開文件/應用/ EXE等是外部到.NET ...但我想打電話給我如何能做到這一點的東西內部(VB函數/方法)

請?可能嗎?

請參閱下文。

'define the job and tie it to our HelloJob class 
    IJobDetail job = JobBuilder.Create<HelloJob>() 
     .WithIdentity(myMethod(), "group1") 
     .Build(); 



Sub myMethod() 
    MsgBox("YES") 
End Sub 
+0

您不調用Quart的方法,但Quartz調度程序調用您的方法。創建一個實現IJob的類。在那個類中添加一個方法Execute()並將你的類傳遞給Quart.Net。安排工作,運行Quartz,它會調用你的Execute()方法。 – derloopkat

+0

非常感謝!請你能給我一個VB.Net的例子。即使是C#,如果你不能寫VB.Net代碼。 – Jane

+0

我已經將一個C#代碼示例翻譯成VB.Net並正常工作。只需要將NuGet包中的Quartz.Net添加到VB.Net控制檯項目並使用該代碼即可。 – derloopkat

回答

0

基本上,你創建一個類(實現IJob),寫一個方法Execute,把你的代碼中有。請查看this tutorial,它解釋瞭如何運行調度程序並通過作業。我已經將完整的示例代碼轉換爲VB.Net並進行了測試。

Imports System.Threading 
Imports Quartz 
Imports Quartz.Impl 
Imports Quartz.Job 

Module Module1 

    Sub Main() 
     Try 
      Common.Logging.LogManager.Adapter = New Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter() _ 
       With {.Level = Common.Logging.LogLevel.Info} 

      ' Grab the Scheduler instance from the Factory 
      Dim scheduler As IScheduler = StdSchedulerFactory.GetDefaultScheduler() 

      ' and start it off 
      scheduler.Start() 

      ' define the job and tie it to our HelloJob class 
      Dim job As IJobDetail = JobBuilder.Create(Of HelloJob)().WithIdentity("job1", "group1").Build() 

      ' Trigger the job to run now, and then repeat every 10 seconds 
      Dim trigger As ITrigger = TriggerBuilder.Create().WithIdentity("trigger1", "group1").StartNow().WithSimpleSchedule(Function(x) x.WithIntervalInSeconds(10).RepeatForever()).Build() 

      ' Tell quartz to schedule the job using our trigger 
      scheduler.ScheduleJob(job, trigger) 

      ' some sleep to show what's happening 
      Thread.Sleep(TimeSpan.FromSeconds(60)) 

      ' and last shut down the scheduler when you are ready to close your program 
      scheduler.Shutdown() 
     Catch se As SchedulerException 
      Console.WriteLine(se) 
     End Try 

     Console.WriteLine("Press any key to close the application") 
     Console.ReadKey() 
    End Sub 

    Public Class HelloJob 
     Implements IJob 
     Public Sub Execute(context As IJobExecutionContext) Implements IJob.Execute 
      Console.WriteLine("Greetings from HelloJob!") 
     End Sub 
    End Class 
End Module 

接下來的事情就是學習如何定義的Cron表達式。例如每週一或每月的第一天運行該作業。然後,可以將您的控制檯應用程序部署爲Windows服務,該服務在啓動時自動運行,並在到期時調用Execute方法。