简体   繁体   English

使用Thread.sleep使UI线程等待

[英]Making UI Thread wait using Thread.sleep

I have written this code in C# for WP7 : 我用C#为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;            
    }

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. 大约5秒后,我希望背景变回白色。 Currently, the program doesnt change the background image of the button, it waits for 5 secs and directly changes the background to White. 目前,该程序不会更改按钮的背景图像,它会等待5秒钟并直接将背景更改为白色。

I am a noob to WP. 我是WP的菜鸟。 I tried searching for a solution and what I got was to create a DispatcherThread but I didnt understand how to proceed. 我试图寻找一个解决方案,我得到的是创建一个DispatcherThread,但我不明白如何继续。 Please help :( 请帮忙 :(

You current approach is incorrect. 您当前的方法是不正确的。 It is keeping the UI thready busy. 它保持了用户界面的繁忙。 It updates the UI when it gets free. 它在获得免费时更新UI。

Here is what is happening 这是正在发生的事情

Button Gets click. 按钮获取单击。 UI thread change the button background to Images. UI线程将按钮背景更改为图像。 Then it sleeps for 5 secs and then it changes the background to white. 然后它睡了5秒然后它将背景变为白色。 Note that UI thread is still busy. 请注意,UI线程仍然很忙。 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. 一旦它将颜色变回白色就会自由并更新UI,您会在屏幕上看到更改。

You need to do this 你需要这样做

 //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);
    }

You should never ever block the UI thread by calling Thread.Sleep . 您永远不应该通过调用Thread.Sleep阻止UI线程。

I think that the best solution is to create a storyboard in your XAML that will perform the required visual changes. 我认为最好的解决方案是在XAML中创建一个能够执行所需视觉更改的故事板 Your button click event handler should then simply call Begin on the storyboard. 然后,您的按钮单击事件处理程序应该只在故事板上调用Begin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM