如何使用TIMER减慢FOR的速度 [英] How do I use the TIMER for slowing down a FOR
本文介绍了如何使用TIMER减慢FOR的速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我的问题是我有这个方法而且我不知道如何使用Timer类来减慢FOR。目前我正在使用Thread.Sleep,但我真的很讨厌它,因为它使界面死,直到脚本完成。
如何更改脚本以这种方式使用Timer类而不是Thread.Sleep,以便我可以在脚本仍在运行时使用UI?
Hello all,
My problem is that I have this method and I don't know how to use the Timer class to slow down the FOR. At the moment I am using the Thread.Sleep but I really hate it because it makes the interface "dead" until the script is done.
How can I change the script in such a way to use the Timer Class instead of the Thread.Sleep so that I can use the UI while the script is still running?
private void ProcessCsvFile(string pFileContent)
{
//set the delimiters for string seperation
string[] stringSeparators = new string[] { "\r\n" };
//now the file content gets split into an array where each line of the .csv-file gets into on element of the array
string[] dataLines = pFileContent.Split(stringSeparators, StringSplitOptions.None);
// X = counter for how many orders have been sent
int x = 0;
//Check if there is a Session ID
string CookieID = textBox1.Text;
if (string.IsNullOrEmpty(CookieID))
{
SystemSounds.Beep.Play();
MessageBox.Show("Please provide a session ID!");
}
else
{
//loop over all of the lines
for (int i = 0; i < dataLines.Length; i++)
{
//dont process the first line, since its the description line
if (i != 0 && dataLines[i] != "")
{
x++;
//split line by ;
string[] csvColumns = dataLines[i].Split(',');
label3.Text = x.ToString();
label3.Refresh();
// Send order based on ID, Count and Price
Send_BuyOrder(csvColumns[0], csvColumns[1], csvColumns[2]);
// Write in the data grid
dataGridView1.Rows.Add(csvColumns[0], csvColumns[1], csvColumns[2], "Done");
dataGridView1.Refresh();
// Sleep for 4seconds
if (x < dataLines.Length -1)
{
Thread.Sleep(4300);
}
}
}
SystemSounds.Beep.Play();
MessageBox.Show(x + " orders have been set!");
}
}
推荐答案
我同意使用线程而非计时器。
与Sleep不同,Join阻塞调用线程,直到线程终止,同时继续执行标准COM和SendMessage抽取。
http://msdn.microsoft.com/ en-us / library / 95hbf2ta(v = vs.110).aspx [ ^ ]
所以这是一个简单的小实验,用于在线程中包装Sleep然后加入:
I agree with using a Thread rather than a Timer.
Unlike Sleep, Join "Blocks the calling thread until a thread terminates, while continuing to perform standard COM and SendMessage pumping."
http://msdn.microsoft.com/en-us/library/95hbf2ta(v=vs.110).aspx[^]
So here's a simple little experiment in wrapping a Sleep in a Thread and then Joining on it:
public static void
SleepWalk
(
int millisecondsTimeout
)
{
System.Threading.Thread t = new System.Threading.Thread
(
delegate() { System.Threading.Thread.Sleep ( millisecondsTimeout ) ; }
)
{
IsBackground = true
,
Priority = System.Threading.ThreadPriority.Lowest
} ;
t.Start() ;
t.Join() ;
return ;
}
好像有效。
我也同意你可能需要重新考虑你的设计。
It seems to work.
I also agree that maybe you need to rethink your design.
为了在繁忙的过程中保持UI响应,你可以使用Application.DoEvents方法
http ://msdn.microsoft.com/en-us/library/system.windows.forms.application.doevents(v = vs.110).aspx [ ^ ]
To keep the UI responsive during busy processes you can use the Application.DoEvents method
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.doevents(v=vs.110).aspx[^]
这篇关于如何使用TIMER减慢FOR的速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文