簡體   English   中英

如何在WPF應用程序中使用Thread.Sleep?

[英]How to use Thread.Sleep in WPF application?

在WPF應用程序VS 2010中,我基本上需要打印文檔,並在開始打印幾秒鍾后檢查錯誤。

目前,我正在使用Thread.Sleep(2000)但是它不起作用。 我認為try代碼在UI線程中運行。

知道如何解決嗎? 還是更好的方法?

在這種情況下,我不在乎UI線程是否被阻塞了幾秒鍾。

try
{
    printer = new Printer();
    printer.PrintTicket(dataAdv);
    // check eventual problem during printing like paper jamp
    Thread.Sleep(2000);
    if (monitorPrinter.IsPrinterReady() == false)
    {
        isPrinterReady = false;
        MessageBox.Show("Problem during the printing!!!");
    }
}

解決問題的方法,謝謝大家指出正確的方向。

try
{
   printer = new Printer();
   printer.PrintTicket(dataAdv);

   System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
   dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
   dispatcherTimer.Interval = new TimeSpan(0, 0, 5);
   dispatcherTimer.Start();
}

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
   if (monitorPrinter.IsPrinterReady() == false)
   {
      MessageBox.Show("SOME PROBLEM WHEN PRINTING!");
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM