PC Review


Reply
Thread Tools Rate Thread

Application.DoEvents and Throwing Dice

 
 
Jeff Gaines
Guest
Posts: n/a
 
      24th Apr 2009

Having read the thread on Application.DoEvents I wonder if anybody has any
thoughts on how to deal with a dice throwing routine? Currently I use:

private void ThrowDice()
// Sets dice numbers and draws dice
{
int count;

while (m_KeepRolling)
{
Random rnd1 = new Random(unchecked((int)DateTime.Now.Ticks));
Random rnd2 = new Random(~unchecked((int)DateTime.Now.Ticks));
for (count = JDice5Common.DICE1; count <= JDice5Common.DICE5; count++)
{
if (!m_DiceHeld[count])
{
m_DiceArray[count] = rnd2.Next(1, 7);
DrawDice(count);
Application.DoEvents();
}
}
}
for (count = JDice5Common.DICE1; count <= JDice5Common.DICE5; count++)
DrawDice(count);
}

m_KeepRolling is a module wide variable which is toggled by clicking the
'Throw Dice' button. Using Application.DoEvents() allows the dice to be
re-drawn with their new values and, I think, gives the OS a chance to
catch up.

What would other people do with this?

--
Jeff Gaines
Damerham Hampshire UK
 
Reply With Quote
 
 
 
 
Fred Mellender
Guest
Posts: n/a
 
      24th Apr 2009
Use a Timer (UI toolbox, components; System.Windows.Forms.Timer). When the
timer fires, throw the dice and draw them. Use Timer.Change(...) to set
interval to random number after a throw. Turn off (on) timer when user
decides to stop (start) throwing (via Change).

Timer will execute on its on thread so that UI is not starved. See the
documentation in the Help.


"Jeff Gaines" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> Having read the thread on Application.DoEvents I wonder if anybody has any
> thoughts on how to deal with a dice throwing routine? Currently I use:
>
> private void ThrowDice()
> // Sets dice numbers and draws dice
> {
> int count;
>
> while (m_KeepRolling)
> {
> Random rnd1 = new Random(unchecked((int)DateTime.Now.Ticks));
> Random rnd2 = new Random(~unchecked((int)DateTime.Now.Ticks));
> for (count = JDice5Common.DICE1; count <= JDice5Common.DICE5; count++)
> {
> if (!m_DiceHeld[count])
> {
> m_DiceArray[count] = rnd2.Next(1, 7);
> DrawDice(count);
> Application.DoEvents();
> }
> }
> }
> for (count = JDice5Common.DICE1; count <= JDice5Common.DICE5; count++)
> DrawDice(count);
> }
>
> m_KeepRolling is a module wide variable which is toggled by clicking the
> 'Throw Dice' button. Using Application.DoEvents() allows the dice to be
> re-drawn with their new values and, I think, gives the OS a chance to
> catch up.
>
> What would other people do with this?
>
> --
> Jeff Gaines
> Damerham Hampshire UK


 
Reply With Quote
 
Family Tree Mike
Guest
Posts: n/a
 
      24th Apr 2009


"Jeff Gaines" wrote:

>
> Having read the thread on Application.DoEvents I wonder if anybody has any
> thoughts on how to deal with a dice throwing routine? Currently I use:
>
> private void ThrowDice()
> // Sets dice numbers and draws dice
> {
> int count;
>
> while (m_KeepRolling)
> {
> Random rnd1 = new Random(unchecked((int)DateTime.Now.Ticks));
> Random rnd2 = new Random(~unchecked((int)DateTime.Now.Ticks));
> for (count = JDice5Common.DICE1; count <= JDice5Common.DICE5; count++)
> {
> if (!m_DiceHeld[count])
> {
> m_DiceArray[count] = rnd2.Next(1, 7);
> DrawDice(count);
> Application.DoEvents();
> }
> }
> }
> for (count = JDice5Common.DICE1; count <= JDice5Common.DICE5; count++)
> DrawDice(count);
> }
>
> m_KeepRolling is a module wide variable which is toggled by clicking the
> 'Throw Dice' button. Using Application.DoEvents() allows the dice to be
> re-drawn with their new values and, I think, gives the OS a chance to
> catch up.
>
> What would other people do with this?
>
> --
> Jeff Gaines
> Damerham Hampshire UK
>


You could put the ThrowDice logic in a background worker that does a report
progress. The report progress handler handles updates on the UI thread to
display the die values. I suspect you are using Application.DoEvents because
the code is running on the UI thread, right?

Mike

 
Reply With Quote
 
Jeff Gaines
Guest
Posts: n/a
 
      25th Apr 2009
On 24/04/2009 in message <(E-Mail Removed)> Peter
Duniho wrote:

Many thanks Fred, Mike and Peter :-)

>I don't understand what the value of generating new dice values at top
>speed is. I also don't understand why you create two Random instances,
>but use only one.


I want to show the dice 'rolling', i.e. re-draw them as their values
change. I used to play the game (based on Yahtzee) with my late Father In
Law who was convinced he could select the numbers he needed by pressing
the 'Stop' button at the right time. He was also convinced that as I wrote
the program it produced better scores for me!


>Also, you should create and use a single Random instance, allocated once
>outside the loop (or even better, outside the method). For sure, you
>should abandon the method you have now, which basically creates two random
> number generators that are intrinsically linked, resulting in your random numbers being correlated (meaning they aren't random any more). Of course, you aren't using both Random instances right now, but if you were, it would be a problem. And creating a new Random() instance with each iteration of the loop, seeded with the time, removes nearly all semblance of randomness altogether.


That's interesting. I first wrote the game 15 years or so ago. When I
first wrote it in C# I had Petzold next to me (at least one of his books)
and he said that to get reasonably random numbers you should seed the
generator twice. I'll see if I can find the book.
Interestingly, having followed your advice, the game appears to throw more
pairs and higher values. I may set up a test bed and set it running to
gather some statistics as appearances can often be deceptive.

>Also, we have no idea what "DrawDice()" actually does, so it's difficult
>to offer advice regarding that method.


It just puts the appropriate image of a die in a picture box and then
refreshes the Picture Box.

>Now, all that said...either of the approaches mentioned so far would be a
>big improvement over what you've got now. Which one is most appropriate
>for you depends on what you're really doing. A Timer would be more
>appropriate if you actually don't want to run the dice at full speed. The
> BackgroundWorker is better if you really do want to run at full speed. Though even the BackgroundWorker will be fine for timed iterations, just by putting a call to Thread.Sleep() for the desired interval.
>
>(I actually would prefer a regular Thread instance rather than
>BackgroundWorker, but BackgroundWorker can in fact be simpler to use, and
>if it's just the one, and you don't have a high demand for other uses of
>the ThreadPool, it's probably fine).


I have added a 'JDiceThrower' class with a Background Worker that raises
an event as it creates new random numbers (so that the main app can
re-draw the dice) and a final event when throwing is finished. Creation of
random numbers continues until a flag is set by the main app. As I want to
continually create new random numbers until the player presses the 'stop'
button I can't think of an alternative to that.

Thanks again, I will keep working on it...

--
Jeff Gaines Damerham Hampshire UK
The facts, although interesting, are irrelevant
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Throwing two dice and print out the total of each throw Bob Microsoft Access 6 23rd Dec 2009 11:50 PM
NotifyIcon Bug, Application.EnableVisualStyles, Application.DoEvents, and Memory Leaks Michael S. Malley Microsoft C# .NET 0 11th Mar 2004 08:43 PM
NotifyIcon Bug, Application.EnableVisualStyles, Application.DoEvents, and Memory Leaks Michael S. Malley Microsoft C# .NET 0 11th Mar 2004 06:22 PM
NotifyIcon Bug, Application.EnableVisualStyles, Application.DoEvents, and Memory Leaks Michael S. Malley Microsoft C# .NET 0 10th Mar 2004 09:52 PM
NotifyIcon Bug, Application.EnableVisualStyles, Application.DoEvents, and Memory Leaks Michael S. Malley Microsoft Dot NET 0 10th Mar 2004 09:52 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:50 AM.