2
나는 작업이있는 Windows 서비스를 가지고 있으며 이러한 작업은 병렬 foreach를 실행하여 일부 이미지를 처리합니다.작업 .net4 및 windows 서비스 중지
지금해야 할 일은 이미 수행중인 항목에 대해 작업이 처리를 완료하도록 허용하는 서비스 온 스타트 방법을 갖지만 새 항목을 시작할 수 없도록 설정하는 것입니다. 다음은
내가 가진 것을 보여 내 코드의 일부 조각이 있습니다if (recordsToProcess != null && recordsToProcess.Any())
{
if (!_cancellationTokenSource.Token.IsCancellationRequested)
{
Parallel.ForEach(recordsToProcess, new ParallelOptions { MaxDegreeOfParallelism = MaxParallelThreadSetting }, currentRecord => _Processor.Process(currentRecord, _supportedFileTypes));
}
Task.WaitAll(); //Recently added not sure if needed
}
protected override void OnStop()
{
//Sets the cancel flag to stop the processing
_cancellationTokenSource.Cancel();
//Allows existing threads to complete
_logHandler.LogInfoMessage("Waiting On Cancel");
Task.WaitAll();
//Stop service and tidy up resources
_logHandler.LogInfoMessage("Stopping service");
base.OnStop();
}
당신은 내가 처리 할 새 항목을 추가 중지하고 볼 수있는 것처럼하지만, 난 여전히 모두가 끝나기 전에 서비스가 중지되어 있는지입니다 .
어떤 아이디어 ??
건배
좋아요,하지만이 여전히 정지 문제에 대한 서비스와 함께 나를 떠나요 ?? – Morn
@Morn 루프가 정지했을 때 서비스에 신호를 보내려면 'ManualResetEvent'또는 이와 유사한 것을 사용하고 싶을 것입니다 ... –