c#EventArrivedEventHandler卡在无限循环中,can';t参考观察者

c#EventArrivedEventHandler卡在无限循环中,can';t参考观察者,c#,multithreading,event-handling,C#,Multithreading,Event Handling,在过去的一周里,我一直在谷歌上搜索这个问题,它正在破坏我的安宁!请帮忙。。。EventArriveDevenHandler卡在一个循环中,如果我停止它,它将无法捕获事件。但是当我使用处理程序方法时,线程仍然专注于循环,并且不会关注我试图在处理程序中创建的新表单!奇怪的是,如果我只使用一些小的东西,比如MessageBox,它不会引起问题,只是尝试实例化一个表单会导致按钮无法绘制。然后在程序停止响应后不久。如果您想知道表单代码在哪里,它只是一个由.NET制作的标准表单,在代码中除事件处理程序之外的

在过去的一周里,我一直在谷歌上搜索这个问题,它正在破坏我的安宁!请帮忙。。。EventArriveDevenHandler卡在一个循环中,如果我停止它,它将无法捕获事件。但是当我使用处理程序方法时,线程仍然专注于循环,并且不会关注我试图在处理程序中创建的新表单!奇怪的是,如果我只使用一些小的东西,比如MessageBox,它不会引起问题,只是尝试实例化一个表单会导致按钮无法绘制。然后在程序停止响应后不久。如果您想知道表单代码在哪里,它只是一个由.NET制作的标准表单,在代码中除事件处理程序之外的任何地方都可以使用

谢谢

class MainClass
{
    public static void Main()
    {
        TaskIcon taskbarIcon;
        EventWatch myWatcher;

        taskbarIcon = new TaskIcon();
        taskbarIcon.Show();

        myWatcher = new EventWatch();
        myWatcher.Start();

        Application.Run();
    }
}

public class TaskIcon
{
    public void Show()
    {
        NotifyIcon notifyIcon1 = new NotifyIcon();
        ContextMenu contextMenu1 = new ContextMenu();
        MenuItem menuItem1 = new MenuItem();
        MenuItem menuItem2 = new MenuItem();

        contextMenu1.MenuItems.AddRange(new MenuItem[] { menuItem1, menuItem2 });

        menuItem1.Index = 0;
        menuItem1.Text = "Settings";
        menuItem1.Click += new EventHandler(notifyIconClickSettings);

        menuItem2.Index = 1;
        menuItem2.Text = "Exit";
        menuItem2.Click += new EventHandler(notifyIconClickExit);

        notifyIcon1.Icon = new Icon("app.ico");
        notifyIcon1.Text = "Print Andy";
        notifyIcon1.ContextMenu = contextMenu1;
        notifyIcon1.Visible = true;
    }

    private static void notifyIconClickSettings(object Sender, EventArgs e)
    {
        MessageBox.Show("Settings Here");
    }

    private static void notifyIconClickExit(object Sender, EventArgs e)
    {
        //taskbarIcon.Visible = false; // BONUS QUESTION: Why can't I hide the tray icon before exiting?

        Application.Exit();
    }
}

public class EventWatch
{
    public void Start()
    {
        string thisUser = WindowsIdentity.GetCurrent().Name.Split('\\')[1];

        WqlEventQuery query = new WqlEventQuery();

        query.EventClassName = "__InstanceCreationEvent";
        query.Condition = @"TargetInstance ISA 'Win32_PrintJob'";
        query.WithinInterval = new TimeSpan(0, 0, 0, 0, 1);

        ManagementScope scope = new ManagementScope("root\\CIMV2");
        scope.Options.EnablePrivileges = true;

        ManagementEventWatcher watcher = new ManagementEventWatcher(scope, query);

        watcher.EventArrived += new EventArrivedEventHandler(showPrintingForm);

        watcher.Start();
    }
    void showPrintingForm(object sender, EventArrivedEventArgs e)
    {
        // MessageBox.Show("This will draw just fine");

        Form1 myForm;
        myForm = new Form1();
        myForm.Show(); // This causes a hangup
    }
}

我的猜测是
ManagementEventWatcher
从与UI线程不同的线程调用
eventarrized
处理程序。然后在该线程上执行
showPrintingForm
,从不同于UI线程的线程访问UI是错误的。您需要将代码封送回UI线程。

@Chris,太棒了,您的提示让我用Application.Run(new Form1())启动表单;而且它会正常启动,谢谢!