최선의 선택, 응용 프로그램에서 가능하면 협력을 취소 할 수 있습니다.
실행할 코드가 더 이상 없을 때 스레드가 자동으로 멈 춥니 다. 따라서 사용자가 응용 프로그램을 닫으면 스레드를 중지해야 함을 나타내는 플래그를 설정합니다. 스레드가 플래그가 설정되어 있으면 때때로 확인해야하며, 그렇다면 서버에서 레코드를 가져 오는 것을 중지하고 돌아 오십시오.
그렇지 않으면, 당신은 스레드가 될 수있다 예를
static bool isCancellationRequested = false;
static object gate = new object();
// request cancellation
lock (gate)
{
isCancellationRequested = true;
}
// thread
for (int i = 0; i < 100000; i++)
{
// simulating work
Thread.SpinWait(5000000);
lock (gate)
{
if (isCancellationRequested)
{
// perform cleanup if necessary
//...
// terminate the operation
break;
}
}
}
출처
2010-08-22 15:46:39
dtb
가능한 중복 [? 폼의 closing 이벤트에 BackgroundWorker에 중지하는 방법 (http://stackoverflow.com/questions/1731384/how-to-stop- backgroundworker-on-forms-closing-event) –
나는 당신의 링크 sugestion을 보았다 ... 나를 위해 일을하지 않았다. – fishhead