C# 放慢foreach的速度,以便我可以显示所有GIF
我想减慢foreach循环的速度,这样我就可以显示gif了,完成这个循环大约需要2秒钟,但是foreach循环非常快,除了最后一个循环,我看不到gifC# 放慢foreach的速度,以便我可以显示所有GIF,c#,foreach,C#,Foreach,我想减慢foreach循环的速度,这样我就可以显示gif了,完成这个循环大约需要2秒钟,但是foreach循环非常快,除了最后一个循环,我看不到gif foreach (string s in words) { path = Dictionary.wordchecker(s);//Checks words for Animation pictureBox1.Image = Image.FromFile(Directory.GetCu
foreach (string s in words)
{
path = Dictionary.wordchecker(s);//Checks words for Animation
pictureBox1.Image = Image.FromFile(Directory.GetCurrentDirectory() + "\\images\\" + path + ".gif");
textBox1.Text = s;
}
根据要求,我更新了解决方案以使用windows窗体, 假设您有一个字符串列表,可以这样做:
foreach (string s in words)
{
path = Dictionary.wordchecker(s);//Checks words for Animation
pictureBox1.Image = Image.FromFile(Directory.GetCurrentDirectory() + "\\images\\" + path + ".gif");
textBox1.Text = s;
}
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
List<string> words = new List<string>();
int cur = 0;
public static int Main()
{
//2000 is two seconds you can adjust to the amount you need
timer.Interval = 2000;
timer.Tick += timerTick;
timer.Start();
}
private void timerTick(object sender, object e)
{
if (cur < words.Count)
{
path = Dictionary.wordchecker(words[cur]);//Checks words for Animation
pictureBox1.Image = Image.FromFile(Directory.GetCurrentDirectory() + "\\images\\" + path + ".gif");
textBox1.Text = s;
cur++;
}
else
{
timer.Stop();
}
}
System.Windows.Forms.Timer Timer=new System.Windows.Forms.Timer();
列表单词=新列表();
int cur=0;
公共静态int Main()
{
//2000是两秒钟,你可以根据需要调整
时间间隔=2000;
timer.Tick+=timerTick;
timer.Start();
}
私有void timerTick(对象发送方,对象e)
{
if(cur
根据要求,我更新了解决方案以使用windows窗体,
假设您有一个字符串列表,可以这样做:
foreach (string s in words)
{
path = Dictionary.wordchecker(s);//Checks words for Animation
pictureBox1.Image = Image.FromFile(Directory.GetCurrentDirectory() + "\\images\\" + path + ".gif");
textBox1.Text = s;
}
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
List<string> words = new List<string>();
int cur = 0;
public static int Main()
{
//2000 is two seconds you can adjust to the amount you need
timer.Interval = 2000;
timer.Tick += timerTick;
timer.Start();
}
private void timerTick(object sender, object e)
{
if (cur < words.Count)
{
path = Dictionary.wordchecker(words[cur]);//Checks words for Animation
pictureBox1.Image = Image.FromFile(Directory.GetCurrentDirectory() + "\\images\\" + path + ".gif");
textBox1.Text = s;
cur++;
}
else
{
timer.Stop();
}
}
System.Windows.Forms.Timer Timer=new System.Windows.Forms.Timer();
列表单词=新列表();
int cur=0;
公共静态int Main()
{
//2000是两秒钟,你可以根据需要调整
时间间隔=2000;
timer.Tick+=timerTick;
timer.Start();
}
私有void timerTick(对象发送方,对象e)
{
if(cur
Task.Delay()
也许吧?如果没有更多信息,我们无法真正了解。您使用什么来显示GIF?请显示更多代码,突出显示图像的显示方式。您应该使用计时器,这样您可以确定间隔。此外,您没有阻塞gui线程“减慢foreach循环”并不是您真正想要的,因为它会占用您的gui线程并导致应用程序挂起。您真正想做的是设置一个定时器,它将每隔一段时间触发一次,允许您切换正在显示的图像。Task.Delay()
也许?如果没有更多信息,我们无法真正了解。您使用什么来显示GIF?请显示更多代码,突出显示图像的显示方式。您应该使用计时器,这样您可以确定间隔。此外,您没有阻塞gui线程“减慢foreach循环”并不是您真正想要的,因为它会占用您的gui线程并导致应用程序挂起。你真正想要的是设置一个定时器,它会每隔一段时间触发一次,允许你切换正在显示的图像。当你到达字符串列表的末尾时呢?这将在第一次通过列表后引发异常。是的,编辑了谢谢,在原始帖子中我确实忘记启动计时器了,它现在应该可以工作了:)是的,这应该可以解决问题,OP也可以做一个“环绕”就像我在回答中所说的,如果他想连续循环浏览图像,或者如果他只想像你在这里一样显示每幅图像一次,他可以停止计时器。是的,因为他没有说明他是否在使用Windows窗体或WPF或创建UWP应用程序,我再次编辑了这篇文章,以记录这一事实和所需的更改表单应用程序当您到达字符串列表的末尾时呢?这将在第一次通过列表后引发异常。是的,编辑了谢谢,在原始帖子中我确实忘记启动计时器了,它现在应该可以工作了:)是的,这应该可以解决问题,OP也可以做一个“环绕”就像我在回答中所说的,如果他想连续循环浏览图像,或者如果他只想像你在这里一样显示每幅图像一次,他可以停止计时器。是的,因为他没有说明他是否在使用Windows窗体或WPF或创建UWP应用程序,我再次编辑了这篇文章,以记录这一事实和所需的更改表格申请