.NETコアタイマープログラム

使用してMicrosoft.Extensions.DependencyInjectionを。
使用してMicrosoft.Extensions.Hostingを。
使用してMicrosoft.Extensions.Loggingを。
使用してクォーツ。
使用してQuartz.Implを。
使用してシステムを。
使用してSystem.Collections.Specializedを。
使用してSystem.IOを。
使用してSystem.Textのを。
使用してSystem.Threading.Tasksを。

名前空間Star.Service.News 
{ 
    クラスプログラム
    { 
        静的な 無効メイン(文字列[] argsを)
        { 
            Console.WriteLineを(" タイムジョブ開始" ); 
            RunProgram()GetAwaiter()のgetResult();。。
            Console.WriteLineを(" Hello Worldの!" ); 
            Console.Read(); 
        } 

        プライベート 静的 非同期タスクRunProgram()
        { 
            しようと
            { 
                // からSchedulerインスタンスをつかみます工場   
                NameValueCollectionの小道具= 新しいNameValueCollectionの
                { 
                    { " quartz.serializer.type "" バイナリ" } 
                }。
                工場StdSchedulerFactory = 新新StdSchedulerFactory(小道具); 
                ISchedulerスケジューラは = のawait ; factory.GetScheduler()

                // タスクスケジューラを開始   
                インクルードが待つscheduler.Startを(); 

                // カスタムタイミングタスクジョブクラスを定義 
                IJobDetail仕事= JobBuilder.Create <TimedBackgroundService > ()
                    .WithIdentity(" ジョブというJOB1 "" 名前のGROUP1 " 
                    .build(); 
                ISimpleTriggerトリガー = (ISimpleTrigger)TriggerBuilder.Create()
                    .WithIdentity(" TRIGGER1 "// 名前にタスクに   
                    .StartAt(DateTime.Now)//は、タスクの開始時刻を設定   
                    .ForJob(" JOB1というジョブ"" GROUP1 "//は、タスクグループを指定する   
                    .WithSimpleSchedule(X =>をX 
                    .WithIntervalInSeconds(180// 1秒の時間のサイクルタイム
                    .RepeatForever())
                    .build(); 


                // ミッション待つ   
                ザが待つscheduler.ScheduleJob(ジョブ、トリガ)。

                // 起こっているのかを示すためにいくつかの睡眠が  
                 // 待つTask.Delay(TimeSpan.FromMilliseconds(2000));  
            }
             キャッチ(SchedulerException SE)
            { 
                のawait Console.Error.WriteLineAsync(se.ToString())。
            } 
        } 
    } 
}
使用してクォーツ。
使用してStar.Helpersを。
使用してシステムを。
使用したSystem.Diagnosticsを。
使用してSystem.Threading.Tasksを。

名前空間Star.Service.News 
{ 
    パブリック クラスTimedBackgroundService:IJobは
    { 
        公共のタスクが実行(IJobExecutionContextコンテキスト)
        { 
            // Console.Out.WriteLineAsyncを( "HelloJobからのご挨拶!")を返します。
            JobKeyキー= context.JobDetail.Key。
            プロセスPROC = NULL ; 

            文字列 EXENAME = メイン

            試す
            { 
                文字列 destFilePath = " C://chromedriver.exe " ;
                文字列 sourceFilePath = CommonHelper.GetPhysicalPath($ " newspicker \\ chromedriver.exe " ); 

                もし(!FileHelper.IsExistFile(destFilePath))
                { 
                    FileHelper.CopyTo(sourceFilePath、destFilePath)。
                } 

                VAR myProcess = Process.GetProcesses()。
                foreachの(プロセスMyProcess myProcess)
                { 
                    // 查找是否正在运行
                    場合(MyProcess.ProcessName.CompareTo(EXENAME)== 0 
                        || MyProcess.ProcessName.CompareTo(" クロム")== 0 
                        || MyProcess.ProcessName.CompareTo(" chromedriver ")== 0 
                    { 
                        MyProcess.Kill()。// 杀死当前程序
                    } 
                } 
            } 
            キャッチ(例外)
            { 

            } 

            してみてください
            { 
                文字列 fileurl = CommonHelper.GetPhysicalPath($ ".EXE EXENAME} { " );
                 // 外部プログラム起動 
                procは= Process.Start(fileurl)を、

                Proc.WaitForExit(120 * 1000 ); 
                Proc.CloseMainWindow(); // メインウィンドウを閉じるために、プロセスによってメッセージに近い送信ユーザインタフェース処理有する

                context.MergedJobDataMap.Put(" RunResult "、ApiResult.Success($ " - {Proc.ExitTime).Milliseconds(Proc.StartTime}ニュース収集プログラム実行時" ;))
            } 
            キャッチ(例外EX)を
            { 
                procの?.WaitForExit(60 *1000年); 
                PROC.CloseMainWindow(); 
                PROC.Close(); 

                context.MergedJobDataMap.Put(" RunResult "、ApiResult.Fail(ApiEnum.Error、$ " {{key.Name key.Group}}:{。 } ex.Message " )); 
            } 
            最後に
            { 
                Proc.Close(); // このリソースに関連付けられたすべてのコンポーネントが解放
            }
             戻りTask.CompletedTaskを; 
        } 
    } 
}

 

おすすめ

転載: www.cnblogs.com/smile-live/p/11314420.html