public void ReplayGame()
{
if (Class2.replayIsOn)
{
if (!backgroundWorker1.IsBusy)
{
backgroundWorker1.RunWorkerAsync();
}
}
}
Я хочу отменить / остановить backgroundwoker1, как только функция завершится .. событие backgroundworker запускается на несколько секунд и останавливается .. когда оно останавливается, я хочу, чтобы оно закончилось !! ..
Как я могу достичь этой задачи /?без получения недопустимых исключений операций, которые я получаю сейчас
ОБНОВЛЕНИЕ:
public void ReplayGame()
{
if (Class2.replayIsOn)
{
replay = serializeMeh.giveBackDictionary();
backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.RunWorkerAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int[] makeSelfMoves = new int[4];
if (!backgroundWorker1.CancellationPending)
{
lock (replay)
{
foreach (KeyValuePair<int, int[]> item in replay)// count should be more than 2
{
if (backgroundWorker1.CancellationPending)
{
break;
}
makeSelfMoves = replay[item.Key];
codeFile.ExecuteAll(makeSelfMoves[0], makeSelfMoves[1], makeSelfMoves[2], makeSelfMoves[3]);
PrintPieces(codeFile.PieceState()); Invalid operation exception is thrown here when chess pieces are drawn on the screen ... Two threads enter the loop simultaneously..:(...Invalid operation exception
System.Threading.Thread.Sleep(1000);
}
Class2.counter = serializeMeh.serializedCounter;
Class2.replayIsOn = false;
Game.WasntSerialized = true;
}
}
backgroundWorker1.CancelAsync();
}