穿线有麻烦 [英] Having trouble with threading

查看:55
本文介绍了穿线有麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到穿线问题.这是针对"Simon"游戏的,只是为了使其正常工作,我需要有一个按钮触发一个序列,该序列遍历堆栈中的每个项目,并使按钮连续闪烁.现在,只有第一个按钮闪烁.

Having trouble with threading. It''s for a "Simon" game and just to get it working, I need to have a button trigger a sequence that goes through each item in the stack and make the buttons blink in succession. Right now, only the first button blinks.

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.Windows.Threading;
using System.Threading;


namespace Simon
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    /// 

    public partial class Window1 : Window
    {
        public string myGreen;
        public string myRed;
        public string myYellow;
        public string myBlue;
        public bool isRunning = false;
        private int loopCount = 0;
        private int counter = 0;
        Stack<String> mySimon = new Stack<String>();

        public Window1()
        {
            InitializeComponent();
            mySimon.Push("myGreen");
            mySimon.Push("myRed");
        }
        public void timer()
        {  
            foreach (string i in mySimon)
            {
                if (i == "myGreen" && isRunning == false)
                {
                    DispatcherTimer timerOne = new DispatcherTimer();
                    timerOne.Interval = TimeSpan.FromMilliseconds(1);
                    timerOne.Tick += new EventHandler(greenCount);
                    timerOne.Start();
                    isRunning = true;
                }
                if (i == "myRed" && isRunning ==  false)
                {
                    DispatcherTimer timerOne = new DispatcherTimer();
                    timerOne.Interval = TimeSpan.FromMilliseconds(1);
                    timerOne.Tick += new EventHandler(redCount);
                    timerOne.Start();
                    isRunning = true;
                }
            }
        }
        public void greenCount(Object sender, EventArgs args)
        {
            counter++;
            simonText.Text = counter.ToString();
            if (counter < 29)
            {
                green.Background = Brushes.LawnGreen;
            }
            loopCount++;
        if (counter > 29)
        {
            DispatcherTimer myTimer = (DispatcherTimer)sender;
            myTimer.Stop();
            counter = 0;
            green.Background = Brushes.Green;
            isRunning = false;
            }
        loopCount++;
        }
        public void redCount(Object sender, EventArgs args)
        {
            counter++;
            simonText.Text = counter.ToString();
            if (counter < 29)
            {
                red.Background = Brushes.Pink;
            }
            if (counter > 29)
            {
                DispatcherTimer myTimer = (DispatcherTimer)sender;
                myTimer.Stop();
                counter = 0;
                red.Background = Brushes.Red;
                isRunning = false;
            }
        }

推荐答案

这可能是因为您启动了一个将重置isRunning的线程.不幸的是,在迭代下一项之前,您没有在timer()方法中将其重置.

It''s probably because you''re kicking off a thread that will reset isRunning.  You are unfortunately not reseting it in the timer() method before it iterates through the next item.

您在并发性和线程化方面遇到了几个问题.我建议您在线查看如何使用线程的示例.您正在使自己变得比需要更难.

You''ve got several issues with concurrency and threading it looks like here.  I''d recommend looking online at examples of how to use threading.  You''re making it harder on yourself than needs to be.


这篇关于穿线有麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆