C#打印在无限循环中,系统托盘菜单做';不要出现

本文关键字:菜单 无限循环 打印 系统 | 更新日期: 2023-09-27 18:21:29

我有一个小应用程序,它在无限循环中运行,监视目录并打印其中是否有文件。我想在系统托盘中运行这个程序,但当我用鼠标右键单击图标时,什么都没发生,但当目标目录中有文件,我的计算机开始打印时,菜单就会打开。

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections.Generic;
using System.IO;
using System.Drawing.Printing;
using System.Text;
namespace CreativeGastPrint
{
    public class SysTrayApp : Form
    {
        [STAThread]
        public static void Main()
        {
            Application.Run(new SysTrayApp());
        }
        private NotifyIcon trayIcon;
        private ContextMenu trayMenu;
        public SysTrayApp()
        {
            trayMenu = new ContextMenu();
            trayMenu.MenuItems.Add("Exit", OnExit);
            trayIcon = new NotifyIcon();
            trayIcon.Text = "PrintApp";
            trayIcon.Icon = new Icon("cg.ico");
            trayIcon.ContextMenu = trayMenu;
            trayIcon.Visible = true;
            FileHandler handler = new FileHandler("C:''print");
            Stopwatch stopwatch = Stopwatch.StartNew();
            while (true)
            {
                try
                {
                    handler.FullDirList();
                    handler.FullContent();
                    List<FileInfo> temp = new List<FileInfo>();
                    foreach (FileInfo f in handler.Files)
                    {
                        temp.Add(f);
                    }
                    foreach (FileInfo f in temp)
                    {
                        DirectoryInfo d = f.Directory;
                        String content = System.IO.File.ReadAllText(f.FullName, Encoding.UTF8);
                        Printer printer = new Printer(content);
                        printer.PrintController = new StandardPrintController();
                        List<String> installed = new List<String>();
                        for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
                        {
                            installed.Add(PrinterSettings.InstalledPrinters[i]);
                        }
                        if (installed.Contains(d.Name))
                        {
                            printer.PrinterSettings.PrinterName = d.Name;
                        }
                        printer.Print();
                    }
                    handler.DeleteFiles();
                    System.Threading.Thread.Sleep(2000);
                    stopwatch.Stop();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error: " + e.Message);
                }
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            Visible = false; // Hide form window.
            ShowInTaskbar = false; // Remove from taskbar.
            base.OnLoad(e);
        }
        private void OnExit(object sender, EventArgs e)
        {
            Application.Exit();
        }
        protected override void Dispose(bool isDisposing)
        {
            if (isDisposing)
            {
                // Release the icon resource.
                trayIcon.Dispose();
            }
            base.Dispose(isDisposing);
        }
    }
}

C#打印在无限循环中,系统托盘菜单做';不要出现

在无限循环中运行的小型应用程序

这就是您的问题,while(true) { ... }循环会阻止Windows消息的处理。这会让你的应用程序变得又盲又聋。

一个快速且非常糟糕的解决方案是在Sleep()之后添加一个Application.DoEvents()调用,但这不是正确的做法。当循环仍在运行时,您的OnExit()可能会发生。。。

Windows是一个事件驱动的操作系统,因此编写事件驱动的解决方案。不使用CCD_ 5或CCD_。

在您的情况下,您可能应该使用Timer和BackgroundWorker或其他Thread解决方案。