当前位置:编程学习 > C#/ASP.NET >>

WPF写的一个随机摇号小程序使用定时器的问题

功能都实现了,现在就是摇号出号的间隔时长不一致,定时器那有问题,最终效果是每摇号3秒出一个号然后停1秒再接着摇,红色标记就是点击开始按钮的事件和那4个定时器,还有能不能简化代码,用线程来实现。该项目是WPF应用程序,直接可用,请帮忙改一下,100分送上,初学者,谢谢各位~
前台代码:
<Window x:Class="WpfRandom.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="随机摇号器" Height="350" Width="525">
    <Grid>
        <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="30" TextWrapping="Wrap"  FontSize="16" VerticalAlignment="Top" Width="85" Margin="166,56,0,0" LostFocus="textBox1_LostFocus"/>
        <TextBlock HorizontalAlignment="Left" Margin="30,60,0,0" FontSize="15" TextWrapping="Wrap" VerticalAlignment="Top" Height="26" Width="140"><Run Text="请输入最大范围数:"/></TextBlock>
        <Button Content="开始摇号" HorizontalAlignment="Left" Margin="155,273,0,0" VerticalAlignment="Top" Width="80" RenderTransformOrigin="0.209,-1.241" Height="29" Click="Button_Click_1"/>
        <ListBox Height="150" HorizontalAlignment="Left" Margin="358,94,0,0" Name="listBox1" VerticalAlignment="Top" Width="63" />
        <Label Content="" FontSize="15" Foreground="Blue" Height="30" HorizontalAlignment="Right" Margin="0,56,67,0" Name="label1" VerticalAlignment="Top" Width="185" />
        <Label Content="随机摇号器" FontSize="36" Height="59" Width="212" HorizontalAlignment="Left" Margin="139,0,0,0" Name="label2" VerticalAlignment="Top" />
        <Label Content="" BorderThickness="2" BorderBrush="Black" FontSize="72" Height="102" Background="Yellow" HorizontalAlignment="Left" Margin="166,142,0,0" Name="label3" VerticalAlignment="Top" Width="127" />
        <Button Content="终止摇号" Height="29" HorizontalAlignment="Left" Margin="262,273,0,0" Name="button1" VerticalAlignment="Top" Width="80" Click="button1_Click" />
        <Label Content="请输入摇号间隔时间:" FontSize="15" Height="28" HorizontalAlignment="Left" Margin="25,92,0,0" Name="label4" VerticalAlignment="Top" Width="154" />
        <TextBox Height="30" FontSize="16" HorizontalAlignment="Left" Margin="176,92,0,0" Name="textBox2" VerticalAlignment="Top" Width="75" 
                  LostFocus="textBox2_LostFocus"/>
        <Label Content="秒(默认为3秒)" FontSize="14" Foreground="Blue" Height="28" HorizontalAlignment="Left" Margin="251,94,0,0" Name="label5" VerticalAlignment="Top" Width="95" />
        <Label Content="V1.0" FontSize="14" Height="28" HorizontalAlignment="Left" Margin="335,24,0,0" Name="label6" VerticalAlignment="Top" />
        <Label Content="作者:ALEX" FontSize="10" Height="28" HorizontalAlignment="Left" Margin="442,283,0,0" Name="label7" VerticalAlignment="Top" Width="61" />
    </Grid>
</Window>

后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using System.Windows.Threading;

namespace WpfRandom
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private DispatcherTimer dispatcherTimer = null;
        private DispatcherTimer Timer = null;
        private DispatcherTimer MidTimer = null;
        private DispatcherTimer MidTimer1 = null;
        Random rnd = new Random();
        Random rd = new Random();
        int[] intArr;
        int MAXnum;
        int num;
        int k = 0;
        int y = 0;
        int q;
        ArrayList myList = new ArrayList();

        public MainWindow()
        {
            InitializeComponent();

            dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            Timer = new System.Windows.Threading.DispatcherTimer();
            MidTimer = new System.Windows.Threading.DispatcherTimer();
            MidTimer1 = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            Timer.Tick += new EventHandler(Timer_Tick);
            MidTimer.Tick += new EventHandler(MidTimer_Tick);
            MidTimer1.Tick += new EventHandler(MidTimer1_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 3000);
            Timer.Interval = new TimeSpan(0, 0, 0, 0, 50);
            MidTimer.Interval = new TimeSpan(0, 0, 0, 0, 3000);
            MidTimer1.Interval = new TimeSpan(0, 0, 0, 0, 4000);        
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            if (textBox1.Text != "" && Convert.ToInt16(textBox1.Text.ToString()) != 0)
            {
                int MAXnum = Convert.ToInt16(textBox1.Text.ToString());
                int num = rd.Next(1, MAXnum + 1);
                label3.Content = num.ToString();
            }
        }

        private void MidTimer_Tick(object sender, EventArgs e)
        {
            dispatcherTimer.Start();
        }

        private void MidTimer1_Tick(object sender, EventArgs e)
        {
            Timer.Start();
        }

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            Timer.Stop();
            if (k < MAXnum)
            {
                label3.Content = intArr[k].ToString();
                listBox1.Items.Add(intArr[k] + "号");
                k++;
                MidTimer1.Start();
            }
            else
            {
                MidTimer.Stop();
                MidTimer1.Stop();
                MessageBox.Show("摇号已结束!");
                dispatcherTimer.Stop();
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (textBox1.Text != "" && Convert.ToInt16(textBox1.Text.ToString()) != 0)
            {
                MAXnum = Convert.ToInt16(textBox1.Text.ToString());
                if (MAXnum >= 1)
                {
                    intArr = new int[MAXnum];
                    while (y < MAXnum)
                    {
                        num = rnd.Next(1, MAXnum + 1);
                        if (!myList.Contains(num))
                        {
                            myList.Add(num);
                            y++;
                        }                        
                    }
                    for (int i = 0; i < MAXnum; i++)
                    {
                        intArr[i] = (int)myList[i];
                    }

                    Timer.Start();
                    MidTimer.Start();
                }
                else if (MAXnum == 0)
                {
                    listBox1.Items.Clear();
                }
            }
            else if (textBox1.Text == "")
            {
                listBox1.Items.Clear();
            }
            else if (Convert.ToInt16(textBox1.Text.ToString()) == 0)
            {
                listBox1.Items.Clear();
            }
        }

        private void textBox1_LostFocus(object sender, RoutedEventArgs e)
        {
            if (textBox1.Text != "" && Convert.ToInt16(textBox1.Text.ToString()) != 0)
            {
                label1.Foreground = Brushes.Blue;
                label1.Content = "摇号范围: 1 - " + textBox1.Text;
            }
            else if (textBox1.Text == "")
            {
                label1.Foreground = Brushes.Red;
                label1.Content = "请输入摇号范围最大值!";
            }
            else if (Convert.ToInt16(textBox1.Text.ToString()) == 0)
            {
                label1.Foreground = Brushes.Red;
                label1.Content = "请输入大于0的值!";
            }
        }

        private void textBox2_LostFocus(object sender, RoutedEventArgs e)
        {
            if (textBox2.Text != "" && Convert.ToInt16(textBox2.Text.ToString()) != 0)
            {
                q = Convert.ToInt16(textBox2.Text) * 1000;
                dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, q);
                Timer.Interval = new TimeSpan(0, 0, 0, 0, 50);
                MidTimer.Interval = new TimeSpan(0, 0, 0, 0, 5000);
                MidTimer1.Interval = new TimeSpan(0, 0, 0, 0, 5000);
            }
            else if (textBox2.Text == "")
            {
                dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 3000);
                Timer.Interval = new TimeSpan(0, 0, 0, 0, 50);
                MidTimer.Interval = new TimeSpan(0, 0, 0, 0, 5000);
                MidTimer1.Interval = new TimeSpan(0, 0, 0, 0, 5000);
            }
            else if (Convert.ToInt16(textBox2.Text.ToString()) == 0)
            {
                MessageBox.Show("时间必须大于0!");
                textBox2.Text = "";
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Timer.Stop();
            MidTimer.Stop();
            MidTimer1.Stop();
            dispatcherTimer.Stop();
            myList.Clear();
            listBox1.Items.Clear();
            label3.Content = "";
            y = 0;
            k = 0;
        }




    }
}
wpf 定时器 C# 线程
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,