Timer in winform with an iteration
foreach (KeyValuePair<int, int[]> item in replay)// count should be more than 2
{
makeSelfMoves = replay[item.Key];
codeFile.ExecuteAll(makeSelfMoves[0], makeSelfMoves[1], makeSelfMoves[2], makeSelfMoves[3]);
PrintPieces(codeFile.PieceState());
// MessageBox.Show("rowStart: " + makeSelfMoves[0] + ". rowEnd: " + makeSelfMoves[2] + ". columnStart: " + makeSelfMoves[1] + ". columnEnd: " + makeSelfMoves[3] + "____a is: " + a);
}
i want to execute this whole iteration, that i responsible for a game replay, in 1 second intervals. i put a timer in my form and set it to 1 second (this should make the pieces to be moved at 1 second intervals). i made an event, and before the loop i put the statement, timer1.enabled=true. The iteration will reach the end in a quick manner.. h开发者_如何转开发ow do you use a timer to set iteration to execute each second using the timer?
public void ReplayGame()
{
Class2.replayIsOn = true;
replay=serializeMeh.giveBackDictionary();
int[] makeSelfMoves=new int[4];
//Timer t = new Timer();
//t.Interval = 1000;
//t.Tick += timer1_Tick;
//t.Enabled = true;
//t.Start();
if (backgroundWorker1.IsBusy != true)
{
// Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync();
}
//foreach (KeyValuePair<int, int[]> item in replay)// count should be more than 2
//{
// makeSelfMoves = replay[item.Key];
// codeFile.ExecuteAll(makeSelfMoves[0], makeSelfMoves[1], makeSelfMoves[2], makeSelfMoves[3]);
// PrintPieces(codeFile.PieceState());
// MessageBox.Show("rowStart: " + makeSelfMoves[0] + ". rowEnd: " + makeSelfMoves[2] + ". columnStart: " + makeSelfMoves[1] + ". columnEnd: " + makeSelfMoves[3] );
//}
}
The above method is activated if i want a replay.
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];// delivers an array of a single move location startrow,startcolumn,endrow,endcolun
codeFile.ExecuteAll(makeSelfMoves[0], makeSelfMoves[1], makeSelfMoves[2], makeSelfMoves[3]);
PrintPieces(codeFile.PieceState());// prints the code on the board
System.Threading.Thread.Sleep(1000);
}
}
it does the work, but the problem that i had is after the loop finishes, the pieces that existed disappear. is it because of the background worker, cause without it , i have no pieces disappear at the end of the process. The second time that i activate the method, it happens
Run it in a background thread and put Thread.Sleep(1000) in the loop.
This way it will be time based and not freeze your app.
We need to see more of your code, but it's pretty simple:
using System.Windows.Forms;
SomeMethod(...)
{
Timer t = new Timer();
t.Interval = 1000;
t.Tick += t_Tick;
t.Enabled = true;
t.Start();
}
void t_Tick(...)
{
foreach (KeyValuePair<int, int[]> item in replay)// count should be more than 2
{
makeSelfMoves = replay[item.Key];
codeFile.ExecuteAll(makeSelfMoves[0], makeSelfMoves[1], makeSelfMoves[2], makeSelfMoves[3]);
PrintPieces(codeFile.PieceState());
// MessageBox.Show("rowStart: " + makeSelfMoves[0] + ". rowEnd: " + makeSelfMoves[2] + ". columnStart: " + makeSelfMoves[1] + ". columnEnd: " + makeSelfMoves[3] + "____a is: " + a);
}
}
Personally, I would put this into a seperate thread and use Thread.sleep(). In combination with accessing GUI-elements, I would suggest a BackgroundWorker instead (see MSDN, easy to implement).
If you insist in using a timer, I would suggest you use the suggestion of 'Ed S.'. The timer could also be placed on your form, instead of creating it in code of course. Just what you prefer.
精彩评论