Question

I have a small application that is running in an infinite loop and watch a directory and print if there is any file in it. I'd like to run this in system tray, but when I click on the icon with right mouse, nothing happens, but menu is shopwing up when there's a file in the target directory and my computer starts to print.

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);
        }
    }
}
Était-ce utile?

La solution

a small application that is running in an infinite loop

That is your problem, the while(true) { ... } loop blocks processing of Windows messages. And that makes your app blind and deaf.

A quick and very dirty fix would be to add an Application.DoEvents() call after the Sleep() but this is not the right way to go. Your OnExit() could then happen while the loop still runs...

Windows is an event-driven OS, so write event-driven solutions. Without using Sleep() or DoEvents().

In your case you should probably be using a Timer and probably a BackgroundWorker or other Thread solution.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top