暂停方法的执行没有锁定GUI。 C# [英] Pause execution of a method without locking GUI. C#

查看:168
本文介绍了暂停方法的执行没有锁定GUI。 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在C#中的卡牌游戏的一个项目在我的介绍OOP的纸张,并得到了本场比赛,现在的工作,但我加入风骚的图形用户界面。

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.

目前发牌,并瞬间出现在UI。我想有处理卡它涉及下一之前后片刻的停顿编程。

Currently cards are dealt and appear on the UI instantaneously. I want to have to program pause for a moment after dealing a card before it deals the next.

在游戏开始下面的代码运行来填充代表PictureBoxes他们(最终将一个循环):

When a game is started the following code runs to populate the PictureBoxes that represent them (will be a loop eventually):

        cardImage1.Image = playDeck.deal().show();
        cardImage2.Image = playDeck.deal().show();
        cardImage3.Image = playDeck.deal().show();
        cardImage4.Image = playDeck.deal().show();
        cardImage5.Image = playDeck.deal().show();
        ...



我在使用System.Threading.Thread.Sleep尝试(100) ;每笔交易()。节目()之间也在里面每个这样的方法,但所有它实现我的GUI锁定,直到所有的睡觉已经处理再一次显示所有的卡。

I have tries using System.Threading.Thread.Sleep(100); between each deal().show() and also inside each of those methods but all it achieves is locking up my GUI until all of the sleeps have processed then display all of the cards at once.

我已经使用定时器的组合也尝试和while循环,但它导致了同样的效果。

I have also tried using a combination of a timer and while loop but it resulted in the same effect.

会是什么实现的最佳方式理想的结果?

What would be the best way of achieving the desired result?

推荐答案

的问题是,的您在UI运行任何的代码将阻塞UI和冻结该程序。当你的代码运行(即使它的运行 Thread.sleep代码),信息(如画图或点击)发送到UI将不会被处理(直到控制返回到消息循环,当你退出事件处理程序),从而导致其死机。

The problem is that any code that you run on the UI will block the UI and freeze the program. When your code is running (even if it's running Thread.Sleep), messages (such as Paint or Click) sent to the UI will not be processed (until control returns to the message loop when you exit your event handler), causing it to freeze.

要做到这一点,最好的办法是在后台线程运行,然后的 调用 到睡觉之间的UI线程,就像这样:

The best way to do this is to run on a background thread, and then Invoke to the UI thread between sleeps, like this:

//From the UI thread,
ThreadPool.QueueUserWorkItem(delegate {
    //This code runs on a backround thread.
    //It will not block the UI.
    //However, you can't manipulate the UI from here.
    //Instead, call Invoke.
    Invoke(new Action(delegate { cardImage1.Image = playDeck.deal().show(); }));
    Thread.Sleep(100);

    Invoke(new Action(delegate { cardImage2.Image = playDeck.deal().show(); }));
    Thread.Sleep(100);

    Invoke(new Action(delegate { cardImage3.Image = playDeck.deal().show(); }));
    Thread.Sleep(100);

    //etc...
});
//The UI thread will continue while the delegate runs in the background.





另外,你可以做一个计时器,显示下一个计时器周期每个图像。如果您使用的定时器,你应该在一开始做的是启动定时器;不要等待它,否则你会介绍了同样的问题。

Alternatively, you could make a timer and show each image in the next timer tick. If you use a timer, all you should do at the beginning is start the timer; don't wait for it or you'll introduce the same problem.

这篇关于暂停方法的执行没有锁定GUI。 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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