简体   繁体   English

C#在UI中显示文本框,停止主线程,然后继续

[英]C# showing textbox in UI, stopping main thread and then resuming

I am having a little trouble doing something which should be simple, I am using a simple graphical interface to show data coming from a kinect sensor, and I want to create a transition, that is, I want to show the user a video and then I want the program to resume its operation. 我在做一些应该很简单的事情时遇到了一些麻烦,我正在使用一个简单的图形界面来显示来自kinect传感器的数据,并且我想创建一个过渡,也就是说,我想向用户显示一个视频,然后我希望程序恢复运行。 My code goes like this: 我的代码是这样的:

  private void OnTimedEvent(object sender, ElapsedEventArgs e)
    {



         Random random = new Random();
         int periodo=random.Next(9000,20000); 
         contador.Interval = periodo;

         this.Dispatcher.Invoke(new Action(CreateTextBox),null);
         this.Dispatcher.Invoke(new Action(StopThread), null);
         this.Dispatcher.Invoke(new Action(Advance), null);
         this.Dispatcher.Invoke(new Action(ResetTime), null); 
         this.Dispatcher.Invoke(new Action(ShowSample), null);
    }

This creates a timer with a random time between 9 and 20 seconds. 这将创建一个随机时间在9到20秒之间的计时器。 After that I want to do a series of things, which are: Showing a message in the UI Stopping all activities in the UI Showing a video in the UI Resetting a variable Showing a Video sequence 之后,我想做一系列事情,这些事情是:在UI中显示消息停止UI中的所有活动在UI中显示视频重置变量显示视频序列

and the functions used are: 和使用的功能是:

   private void CreateTextBox() {

        TextBox textBox = new TextBox { 
        FontSize=40,
        };

        textBox.Text = "Siguiente Actividad";
        canvas2.Children.Add(textBox);           

        // System.Threading.Thread.Sleep(1000);

    }

 private void ResetTime() {
        contadorsegundos = 0;

    }

  private void StopThread(){
         System.Threading.Thread.Sleep(8000);
    }

   private void ShowSample() {
        canvas2.Children.Clear();
        MediaElement mediaElement2 = new MediaElement();
        canvas2.Children.Add(mediaElement2);
        string location = "C:\\Users\\PALMA\\Documents\\Visual Studio 2010\\Projects\\Interfaz\\Interfaz\\Videos\\ejercicio1.mp4";
        try
        {
            mediaElement2.Source = new Uri(location);
            mediaElement2.Play();
            //MessageBox.Show(final);
        }
        catch
        {
            //no se atiende la excepción 
            //MessageBox.Show("Not Found");
        }

    }

What happens is that the main thread stops for 8 seconds, but the texbox was never shown, even though the function CreateTextBox was called before the function StopThread, I was expecting that the message would be shown and then the thread would stop. 发生的情况是,主线程停止了8秒钟,但是,即使在函数StopThread之前调用了CreateTextBox函数,也从未显示过texbox,我期望消息会显示出来,然后线程会停止。

Any help will be much appreciated. 任何帮助都感激不尽。

Instead of putting the main thread to sleep for 8 seconds, you should use: 而不是让主线程进入休眠状态8秒钟,您应该使用:

await Task.Delay(8000);

This way you avoid freezing the main thread, which can cause all kinds of unpredictable problems. 这样,您可以避免冻结主线程,否则可能导致各种无法预料的问题。

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

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