2014-06-25 46 views
-1

我嘗試在某個值爲true時對滑塊進行「動畫處理」,直至達到指定值。但由於某種原因,它不像我想象的那麼容易:移動滑塊直到達到數值

while (depthScan == true) 
{ 
    depth_max_slider.Value += 10; 
    Console.WriteLine("Working"); 
    if (blobCount <= 400) 
    { 
     depth_max_slider.Value += 10; 

    } 
    else 
    { 
     depthScan = false; 
    } 

} 

我在做什麼錯了?

+0

到底是什麼回事?請提供它正在做什麼的信息。 –

+0

基本上整個程序凍結並在後臺運行腳本(這是邏輯,因爲它是一個while循環),對用戶來說不明顯。我希望滑塊動畫,以便用戶可以看到發生了某些事情。 – Oerf

+0

blobCount是否正在更新?好像你的其他人永遠不會開火,所以你的循環永遠不會結束。 –

回答

0

像這樣的東西應該做的伎倆......

public class MyWindow : Window 
    { 
     void StartButton_Click(object sender, RoutedEventArgs e) 
     { 
      // This method will run on the UI thread 

      BackgroundWorker worker = new BackgroundWorker(); 

      worker.DoWork += worker_DoWork; 
      worker.WorkerReportsProgress = true; 
      worker.ProgressChanged += worker_ProgressChanged; 

      worker.RunWorkerAsync(); // start the thread 

      // UI thread continues immediately 

      // exit click event handler, UI thread goes back to WPF control so it can keep rendering... 
     } 

     void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) 
     { 
      // This method will run on the UI thread 

      double depth_max = (double) e.UserState; // grab the value of depth_max passed over from background thread 

      depth_max_slider.Value = depth_max; // update the slider with this value 

      // exit ProgressChanged event handler, UI thread goes back to WPF control so it can keep rendering... 
     } 

     void worker_DoWork(object sender, DoWorkEventArgs e) 
     { 
      // This method will run on the background thread 

      BackgroundWorker worker = sender as BackgroundWorker; 

      int percentComplete = 0; 
      double depth_max; 

      while(depthScan == true) 
      { 
       depth_max += 10; // NOTE: don't touch the slider control from the background thread, use a double instead! 

       worker.ReportProgress(percentComplete, depth_max); // pass depth_max as the 2nd param, "user state", will show up in ProgressChangedEventArgs in ProgressChanged handler 

       Console.WriteLine("Working"); 
       if(blobCount <= 400) 
       { 
        depth_max += 10; 

        worker.ReportProgress(percentComplete, depth_max); 

       } 
       else 
       { 
        depthScan = false; 
       } 

      } 
     } 
    }