在打印时没有读取数据

问题描述:

打印机作业正在运行时,我运行了该程序,似乎没有拾取任何数据。但它工作,如果程序在打印之前已经运行。这是正常的还是我做错了?请指教?在打印时没有读取数据

static void Main(string[] args) 
    { 
     string printerName = "Printer Name"; 
     string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName); 
     ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); 

     while (true) 
     { 
      Console.WriteLine("Not printing"); 

      ManagementObjectCollection coll = searcher.Get(); 
      var alreadyPrinting = false; 
      foreach (ManagementObject printer in coll) 
      { 
       foreach (PropertyData property in printer.Properties) 
       { 
        //Console.WriteLine(string.Format("{0}: {1}", property.Name, property.Value)); //To check Printing information 
        if (Convert.ToInt32(printer.Properties["PrinterStatus"].Value) == 4 && !alreadyPrinting) 
        { 

         string printeroutput = "Printer is printing"; 
         SpeechSynthesizer synthensizer = new SpeechSynthesizer(); 
         synthensizer.Volume = 100; 
         synthensizer.Rate = -2; 

         //Synchronous 
         synthensizer.Speak(printeroutput); 
         Console.Write(printeroutput); 
         Console.WriteLine(); 
         alreadyPrinting = true; 
        } 
       } 
       Thread.Sleep(1000); 
      } 

     } 
    } 
+2

这是正常的。两个操作不能在同一个线程中同时执行。 – Sakura

+0

Yus,我得到了那部分,但因为它循环和打印机不断打印不应该它最终拿起它。 –

你是 '允许' 来创建多个SpeechSynthesizer。如果其中一台打印机正在打印,它将每秒创建一个SpeechSynthesizer

是否有可能将SpeechSynthesizer的创建移到外面?

而当它执行synthensizer.Speak(printeroutput);它会阻止你的线程。

+0

Yus,这是可能的,但问题不在于SpeechSynthesizer,谢谢。 –