所以我有一个计时器,每次运行时都会更改图片框中的图像。我生成了一个随机数,取决于它将停在该图像上的数字。所以最初计时器第一次运行时没有失败,它会在没有问题的情况下停在正确的图像上。它第二次通过它会出现故障并且出错,第三次它会在1秒左右停止并且第四次冻结。我不明白的是,定时器如何在第一次完美工作并停止(我认为会重置定时器)然后第二次按下按钮再次激活定时器并且它搞砸了?。
这是我的一些代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
namespace WheelOfFortune
{
public partial class f : Form
{
private static Random r = new Random();
private static int spin = 0;
private static int t1 = 0;
private static int t2 = 0;
private static int t3 = 0;
private static int rn = 0;
private static System.Timers.Timer st = new System.Timers.Timer();
public f()
{
InitializeComponent();
}
private void Wheel_Click(object sender, EventArgs e)
{
}
private void Spin_Click(object sender, EventArgs e)
{
if (spin == 0) {
t1 = 0;
t2 = 0;
t3 = 0;
spin = 1;
rn = r.Next(1, 53);
Console.WriteLine("" + rn);
st.Stop();
st.Elapsed += new ElapsedEventHandler(OnTimedEvent);
st.Interval = 15;
st.Start();
}
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
t1 = t1 + 6;
if (t1 == 1)
{
Wheel.Image = Properties.Resources.Wheel_Test_0001;
}
else if (t1 == 2)
{
Wheel.Image = Properties.Resources.Wheel_Test_0002;
}
else if (t1 == 3)
{
Wheel.Image = Properties.Resources.Wheel_Test_0003;
}
else if (t1 == 4)
{
Wheel.Image = Properties.Resources.Wheel_Test_0004;
}
else if (t1 == 5)
{
Wheel.Image = Properties.Resources.Wheel_Test_0005;
}
else if (t1 == 6)
{
Wheel.Image = Properties.Resources.Wheel_Test_0006;
}
else if (t1 == 7)
{
Wheel.Image = Properties.Resources.Wheel_Test_0007;
}
else if (t1 == 8)
{
Wheel.Image = Properties.Resources.Wheel_Test_0008;
}
else if (t1 == 9)
{
Wheel.Image = Properties.Resources.Wheel_Test_0009;
}
else if (t1 == 10)
{
Wheel.Image = Properties.Resources.Wheel_Test_0010;
}
else if (t1 == 11)
{
Wheel.Image = Properties.Resources.Wheel_Test_0011;
}
else if (t1 == 12)
{
Wheel.Image = Properties.Resources.Wheel_Test_0012;
if(rn == 2)
{
t2 = t2 + 1;
if (t2 == 1)
{
t3 = t3 + 1;
if (t3 == 4)
{
spin = 0;
t3 = 0;
t1 = 0;
st.Stop();
}
t2 = 0;
}
}
}
此后代码反复重复,因为有624个图像要经过,因为在开始时一次跳过6个然后将逐渐减慢。 (我还没有实现减速部分。)
我似乎无法看到为什么计时器不会重置并再次执行相同操作的原因,我错过了什么?
答案 0 :(得分:2)
您是否知道可以使用资源管理器来减少代码。
ResourceManager _resourceManager= Resources.ResourceManager;
...
计时器事件
string _imageName=SomeLeftPaddFunction(4,"0",t1);//function to always pad to 4 0's
Wheel.Image = (Bitmap)_resourceManager.GetObject("Wheel_Test_"+_imageName);
缓存图片
public class ImageCacheItem
{
int Index{get;set;}
Bitmap Image{get;set;}
}
...
List<ImageCacheItem> _imageCache=new List<ImageCacheItem>();
....
计时器事件
Wheel.Image=_imageCache.Where(p=>p.Index==t1).FirstOrDefault();
if(Wheel.Image==null)
{
string _imageName=SomeLeftPaddFunction(4,"0",t1);//function to always pad to 4 0's
_imageCache.Add(new {Index=t1,Image=(Bitmap)_resourceManager.GetObject("Wheel_Test_"+_imageName);
Wheel.Image=_imageCache.Where(p=>p.Index==t1).First();
}