2013-07-31 24 views
0

我試圖創建一個「連接到服務器」窗口。在程序打開啓動畫面後需要顯示此窗口。該窗口僅包含一個MediaElement,並且在此MediaElement中,我需要顯示一個.avi文件。 在Window的.cs文件中,我創建套接字,它需要連接到我的服務器並檢查更新。 另外,當接受(從服務器)響應時,我不希望啓動畫面停止顯示.avi文件。無法看到窗口中MediaElement的內容 - WPF

我的問題是窗口不顯示.avi文件。當我用mp3文件(用於測試..)替換.avi文件時,它會執行相同的結果。

一般情況下,我的代碼看起來像這樣:

private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     this.MediaElement.Play(); 

     ------------------------------ 
     | Socket's Code Comes Here | 
     ------------------------------ 

     if (this.IsUpdateNeeded == true) //IsUpdateNeeded == My Own Variable.. 
     { 
      MessageBox.Show("New Version Is Here !", "New Version Is Here !", MessageBoxButtons.OK, .MessageBoxIcon.Information); 
      GoToWebsite(); 
     } 
     else 
     { 
      MessageBox.Show("You Own The Latest Version", "No Update Is Needed", MessageBoxButtons.OK, MessageBoxIcon.Information); 
     } 
     this.MediaElement.Stop(); 
     this.Close(); 
    } 

誰能幫助我的數字出來嗎?

+1

你需要運行在另一個線程的「套接字代碼」。主(UI)線程阻塞,因此無法顯示視頻。 – grantnz

+0

@grantnz謝謝你的回覆!你能給我一個想法/例子我應該怎麼做在另一個線程?有一些好方法可以做到嗎? – Aviv

回答

1

代碼在另一個線程運行(因此不會阻塞主UI線程,並停止視頻播放)

private void Window_Loaded(object sender, RoutedEventArgs e) 
{ 
    this.MediaElement.Play(); 

    Task.Factory.StartNew(
     new Action(delegate() 
     { 
      /* 
      * ------------------------------ 
      Socket's Code Comes Here | 
      ------------------------------ 
      */ 

      Dispatcher.Invoke(
         new Action(delegate() 
         { 

          if (this.IsUpdateNeeded == true) //IsUpdateNeeded == My Own Variable.. 
          { 
             MessageBox.Show("New Version Is Here !", "New Version Is Here !", MessageBoxButton.OK, MessageBoxImage.Information); 
             GoToWebsite(); 
          } 
          else 
          { 
           MessageBox.Show("You Own The Latest Version", "No Update Is Needed", MessageBoxButton.OK, MessageBoxImage.Information); 
          } 
          this.MediaElement.Stop(); 
          this.Close(); 
         }     
        )); 
       } 
      ) 
     ); 
    } 
+0

好吧,我會盡快試用..非常感謝! – Aviv