制作使用了Thread.sleep UI线程等待 [英] Making UI Thread wait using Thread.sleep

查看:135
本文介绍了制作使用了Thread.sleep UI线程等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用C#编写的代码为WP7:

I have written this code in C# for WP7 :

public void btn_handler(object sender, EventArgs args)
    {
        Button btn_Pressed = (Button)sender;
        ImageBrush br = new ImageBrush();
        br.ImageSource = new BitmapImage(new Uri("/images/cat.png"
                                                 , UriKind.Relative));

        btn_Pressed.Background = br;

        Thread.Sleep(5000);

       SolidColorBrush sBrush = new SolidColorBrush(); 
       sBrush.Color = System.Windows.Media.Colors.White;
       btn_Pressed.Background = sBrush;            
    }



每当用户点击该按钮,我想按钮的背景改变到的图像。经过约5秒,我想背景变回白色。目前,程序并没有改变按钮的背景图像,它等待5秒和直接改变的背景白。

Whenever the user clicks the button, I want the background of the button to change to an image. After about 5 secs, I want the background to change back to White. Currently, the program doesnt change the background image of the button, it waits for 5 secs and directly changes the background to White.

我对WP一个小白。我试图寻找一个解决方案,我得到了什么是创建一个DispatcherThread但我不明白如何进行。请帮助:(

I am a noob to WP. I tried searching for a solution and what I got was to create a DispatcherThread but I didnt understand how to proceed. Please help :(

推荐答案

您目前的做法是不正确,是保持UI纤维性忙着它更新UI时,它得到。免费的。

You current approach is incorrect. It is keeping the UI thready busy. It updates the UI when it gets free.

下面是发生了什么事。

按钮获取点击。UI线程改变按钮背景图像。然后睡5秒,然后将它更改背景为白色。需要注意的是UI线程仍然很忙。它只会更新实际UI时这将是免费的。一旦它变回颜色为白色它得到免费的更新UI和你在屏幕上看到的变化。

Button Gets click. UI thread change the button background to Images. Then it sleeps for 5 secs and then it changes the background to white. Note that UI thread is still busy. It will only update the actual UI when it will be free. Once it has changed back color to white it gets free and updates the UI and you see the change on screen.

您需要做到这一点。

 //inside the button click event create a background worker
 BackgroundWorker worker = new BackgroundWorker();
 worker.RunWorkerCompleted += new 

 RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
 worker.DoWork += new DoWorkEventHandler(worker_DoWork);
 worker.RunWorkerAsync();

 Button btn_Pressed = (Button)sender;
 ImageBrush br = new ImageBrush();
 br.ImageSource = new BitmapImage(new Uri("/images/cat.png", UriKind.Relative));

 btn_Pressed.Background = br;


 public static void worker_RunWorkerCompleted(object sender, 
                                              RunWorkerCompletedEventArgs e)
    {
    //once backgroudn work i.e. DoWork is complete this method will be 
    //called and code below will execute in UI thread
    SolidColorBrush sBrush = new SolidColorBrush(); 
    sBrush.Color = System.Windows.Media.Colors.White;
    btn_Pressed.Background = sBrush;  
    }

 public  static  void worker_DoWork(object sender, DoWorkEventArgs e)
    {
    //it will wait 5 secs in the background thread
    Thread.Sleep(5000);
    }

这篇关于制作使用了Thread.sleep UI线程等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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