i have a background worker with a sleep method inside a method. (winforms)
here is the code that is executed:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
int[] makeSelfMoves = new int[4];
foreach (KeyValuePair<int, int[]> item in replay)// count should be more than 2
{
makeSelfMoves = replay[item.Key];// the Dictionary contains all the chess moves, rowStart,columnStart,rowEnd,columnEnd
codeFile.ExecuteAll(makeSelfMoves[0], makeSelfMoves[1], makeSelfMoves[2], makeSelfMoves[3]);// this method executes the code.
PrintPieces(codeFile.PieceState());
System.Threading.Thread.Sleep(1000);
}
//PrintPieces(codeFile.FirstTimeLoad());
//PrintPieces(codeFile.PieceState());
}
it is being activated by an outside method.
public void ReplayGame()
{
Class2.replayIsOn = true;
replay=serializeMeh.giveBackDictionary();
if (backgroundWorker1.IsBusy != true)
{
// Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync();
}
i tested the code many times with a messagebox, it works. but when i put it inside a that backgroundworker, what happens, is synchronization problems, if i activated the method ReplayGame once, it works, fine, but the more i activate it, the more synchronization problems there are. for example, the replay may stagger, lag in the middle of the game and then continue... or freeze!!