[C#] BackgroundWorker 製作畫面時間

  • 515
  • 0

摘要:[C#] BackgroundWorker 製作畫面時間

      BackgroundWorker Worker = new BackgroundWorker();

         Worker.DoWork += new DoWorkEventHandler(DoWork);
         Worker.ProgressChanged += new ProgressChangedEventHandler(Worker_ProgressChanged);
         Worker.WorkerReportsProgress = true;
         Worker.RunWorkerAsync();

      public void DoWork(object sender, DoWorkEventArgs e)
      {
         BackgroundWorker worker = sender as BackgroundWorker;
         while (true)
         {
            worker.ReportProgress(0);//傳遞變更Bar的資料藉以更動畫面資料
            Thread.Sleep(1000);
         }
      }

      private void Worker_ProgressChanged(object sender,
          ProgressChangedEventArgs e)
      {
         lblNowDateTime.Text = DateTime.Now.ToString("yyyy/MM/dd dddd HH:mm:ss");
      }