在打印时不读取数据

时间:2016-03-04 14:09:01

标签: c#

当打印机作业正在运行时,我运行了这个程序,它似乎没有获取任何数据。但是它有效,如果程序在打印之前已经运行。这是正常的还是我做错了什么?请告知?

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);
            }

        }
    }

1 个答案:

答案 0 :(得分:0)

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

是否可以将SpeechSynthesizer的创建时间移出?

当它执行synthensizer.Speak(printeroutput);时,它将阻止你的线程。