1
打印機作業正在運行時,我運行了該程序,似乎沒有拾取任何數據。但它工作,如果程序在打印之前已經運行。這是正常的還是我做錯了?請指教?在打印時沒有讀取數據
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);
}
}
}
這是正常的。兩個操作不能在同一個線程中同時執行。 – Sakura
Yus,我得到了那部分,但因爲它循環和打印機不斷打印不應該它最終拿起它。 –