locked
delaying code execution RRS feed

  • Question

  • I have created a card game using the following code:

                 fileName = "C:\\Playing Cards\\"+ hands[0, 0] + ".jpg";

                filename2 = "C:\\Playing Cards\\"+ hands[0, 1] + ".jpg";

                filename3 = "C:\\Playing Cards\\"+ hands[0, 2] + ".jpg";

                filename4 = "C:\\Playing Cards\\"+ hands[0, 3] + ".jpg";

                filename5 = "C:\\Playing Cards\\"+ hands[0, 4] + ".jpg";

                image1.Source = newBitmapImage(newUri(fileName, UriKind.RelativeOrAbsolute));

                image2.Source = newBitmapImage(newUri(filename2, UriKind.RelativeOrAbsolute));

                image3.Source = newBitmapImage(newUri(filename3, UriKind.RelativeOrAbsolute));

                image4.Source = newBitmapImage(newUri(filename4, UriKind.RelativeOrAbsolute));

                image5.Source = newBitmapImage(newUri(filename5, UriKind.RelativeOrAbsolute));

     I would like to have a 1 second delay between the lines of code that set the image sources to make it appear the cards are being dealt in real time. I have tried .Thread.Sleep(xxx) but that does not work.


    John Scholl

    Wednesday, March 21, 2018 3:08 AM

Answers

  • Generally speaking, if you think you have a use for Thread.Sleep, it's a good idea to think again.

    I recommend using a Timer. Set its interval to 1 second, and set an image in the tick event handler. You will have to keep track of which image you are up to for each tick, obviously.

    There are three Timer classes available. I believe yours is a Windows Forms application, so you will probably want this one;

    https://msdn.microsoft.com/en-us/library/system.windows.forms.timer%28v=vs.110%29.aspx

    • Marked as answer by johnscholl Wednesday, March 21, 2018 4:42 AM
    Wednesday, March 21, 2018 4:40 AM

All replies

  •  I would like to have a 1 second delay between the lines of code that set the image sources to make it appear the cards are being dealt in real time. I have tried .Thread.Sleep(xxx) but that does not work.

    *How* did you try it - *exactly*? Show the code you used, including where
    you put it.

    What does "that does not work" mean in this case - *precisely*?
    Was there no observable pause? Did it hang? Did it pause too long?
    Too short? Did the program abort or hang? Were there build errors?

    To get detailed, specific help you *must* provide detailed, specific
    descriptions and examples. Otherwise we have to make too many
    assumptions and too many guesses.

    Tip: When introducing a pause via sleep after changing a visible
    component, you should always do an update or refresh operation
    on any changed visible component immediately before the pause/sleep.
    That will ensure that the components change *before* the timed
    sleep begins. Otherwise they may not change immediately.

    - Wayne

    Wednesday, March 21, 2018 3:45 AM
  • Generally speaking, if you think you have a use for Thread.Sleep, it's a good idea to think again.

    I recommend using a Timer. Set its interval to 1 second, and set an image in the tick event handler. You will have to keep track of which image you are up to for each tick, obviously.

    There are three Timer classes available. I believe yours is a Windows Forms application, so you will probably want this one;

    https://msdn.microsoft.com/en-us/library/system.windows.forms.timer%28v=vs.110%29.aspx

    • Marked as answer by johnscholl Wednesday, March 21, 2018 4:42 AM
    Wednesday, March 21, 2018 4:40 AM