Pause execution of a method without locking GUI. C#

前端 未结 4 1538
孤街浪徒
孤街浪徒 2020-12-30 16:19

I\'m working on a card game in C# for a project on my Intro to OOP paper and have got the game working now but am adding \"flair\" to the GUI.

Currently cards are de

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-30 16:45

    Normally I'd simply recommend a function like this to perform a pause while allowing the UI to be interactive.

      private void InteractivePause(TimeSpan length)
      {
         DateTime start = DateTime.Now;
         TimeSpan restTime = new TimeSpan(200000); // 20 milliseconds
         while(true)
         {
            System.Windows.Forms.Application.DoEvents();
            TimeSpan remainingTime = start.Add(length).Subtract(DateTime.Now);
            if (remainingTime > restTime)
            {
               System.Diagnostics.Debug.WriteLine(string.Format("1: {0}", remainingTime));
               // Wait an insignificant amount of time so that the
               // CPU usage doesn't hit the roof while we wait.
               System.Threading.Thread.Sleep(restTime);
            }
            else
            {
               System.Diagnostics.Debug.WriteLine(string.Format("2: {0}", remainingTime));
               if (remainingTime.Ticks > 0)
                  System.Threading.Thread.Sleep(remainingTime);
               break;
            }
         }
      }
    

    But there seems to be some complication in using such a solution when it is called from within an event handler such as a button click. I think the system wants the button click event handler to return before it will continue processing other events because if I try to click again while the event handler is still running, the button depresses again even though I'm trying to drag the form and not click on the button.

    So here's my alternative. Add a timer to the form and create a dealer class to handle dealing with cards by interacting with that timer. Set the Interval property of the timer to match the interval at which you want cards to be dealt. Here's my sample code.

    public partial class Form1 : Form
    {
    
      CardDealer dealer;
    
      public Form1()
      {
         InitializeComponent();
         dealer = new CardDealer(timer1);
      }
    
      private void button1_Click(object sender, EventArgs e)
      {
         dealer.QueueCard(img1, cardImage1);
         dealer.QueueCard(img2, cardImage2);
         dealer.QueueCard(img3, cardImage1);
      }
    }
    
    class CardDealer
    {
      // A queue of pairs in which the first value represents
      // the slot where the card will go, and the second is
      // a reference to the image that will appear there.
      Queue> cardsToDeal;
      System.Windows.Forms.Timer dealTimer;
    
      public CardDealer(System.Windows.Forms.Timer dealTimer)
      {
         cardsToDeal = new Queue>();
         dealTimer.Tick += new EventHandler(dealTimer_Tick);
         this.dealTimer = dealTimer;
      }
    
      void dealTimer_Tick(object sender, EventArgs e)
      {
         KeyValuePair cardInfo = cardInfo = cardsToDeal.Dequeue();
         cardInfo.Key.Image = cardInfo.Value;
         if (cardsToDeal.Count <= 0)
            dealTimer.Enabled = false;
      }
    
      public void QueueCard(Label slot, Image card)
      {
         cardsToDeal.Enqueue(new KeyValuePair(slot, card));
         dealTimer.Enabled = true;
      }
    }
    

提交回复
热议问题