You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#课程项目:点击老虎机按钮后延迟3秒生成3张随机图片

Add 3-Second Delay Before Generating Slot Machine Images in C#

Got it, let's sort out this delay effect for your slot machine project! The biggest thing to watch out for is not freezing the UI while waiting—you don't want your app to feel unresponsive. Below are two common approaches depending on which UI framework you're using (most likely WinForms or WPF):

For WinForms Projects

Use async/await with Task.Delay—this lets the UI stay responsive while waiting, unlike Thread.Sleep which would lock up the whole interface. Here's how to modify your button click event:

// Mark the event handler as async
private async void btnSpin_Click(object sender, EventArgs e)
{
    // Disable the button first to prevent double-clicks
    btnSpin.Enabled = false;

    // Wait 3 seconds (3000 milliseconds) asynchronously
    await Task.Delay(3000);

    // Run your existing image generation logic
    GenerateRandomSlotImages();

    // Re-enable the button once done
    btnSpin.Enabled = true;
}

// Your existing image generation method (adjust to match your code)
private void GenerateRandomSlotImages()
{
    Random random = new Random();
    // Replace with your actual image resources/paths
    List<Image> slotImages = new List<Image>
    {
        Properties.Resources.Cherry,
        Properties.Resources.Bar,
        Properties.Resources.Seven
    };

    // Assign random images to your PictureBox controls
    pbSlot1.Image = slotImages[random.Next(slotImages.Count)];
    pbSlot2.Image = slotImages[random.Next(slotImages.Count)];
    pbSlot3.Image = slotImages[random.Next(slotImages.Count)];
}

For WPF Projects

Same async/await approach works here too—just adjust the UI control references and image loading:

private async void btnSpin_Click(object sender, RoutedEventArgs e)
{
    btnSpin.IsEnabled = false;

    // Wait 3 seconds using a TimeSpan for readability
    await Task.Delay(TimeSpan.FromSeconds(3));

    GenerateRandomSlotImages();

    btnSpin.IsEnabled = true;
}

private void GenerateRandomSlotImages()
{
    Random random = new Random();
    // Replace with your image URIs (make sure images are added to your project)
    List<ImageSource> slotImages = new List<ImageSource>
    {
        new BitmapImage(new Uri("pack://application:,,,/Assets/Cherry.png")),
        new BitmapImage(new Uri("pack://application:,,,/Assets/Bar.png")),
        new BitmapImage(new Uri("pack://application:,,,/Assets/Seven.png"))
    };

    // Assign to your WPF Image controls
    imgSlot1.Source = slotImages[random.Next(slotImages.Count)];
    imgSlot2.Source = slotImages[random.Next(slotImages.Count)];
    imgSlot3.Source = slotImages[random.Next(slotImages.Count)];
}

Quick Tips

  • Avoid Thread.Sleep: It blocks the UI thread, making your app appear frozen—always use Task.Delay for asynchronous waits in UI code.
  • Add Loading Feedback: For a better user experience, you could show a small loading spinner or animate the slots "spinning" during the 3-second delay, instead of just waiting.
  • Check Image Paths: Make sure your image resources are correctly embedded in the project and their paths match what's in your code.

内容的提问来源于stack exchange,提问作者Marios P

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:13:35