Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6540753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:01:05+00:00 2026-05-25T11:01:05+00:00

I’ve googled this problem for the past week, it’s killing my peace! Please help…

  • 0

I’ve googled this problem for the past week, it’s killing my peace! Please help… EventArrivedEventHandler is stuck in a loop, and if I stop it, then it won’t catch events. But when I use a handler method, the thread is still concentrating on the loop, and won’t give attention to the new form I’m trying to make in the handler! Strange thing is, if I just use something small, like a MessageBox, it doesn’t cause an issue, just trying to instantiate a form causes the buttons to NOT draw. Then shortly after the program stops responding. In case you’re wondering where the form code is, it’s just a standard form made by .NET, that works everywhere else in the code except for in the event handler.

Thanks!

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
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:01:06+00:00Added an answer on May 25, 2026 at 11:01 am

    My guess would be that the ManagementEventWatcher calls the EventArrived handler from a different thread than the UI thread. Then your showPrintingForm is executed on that thread and accessing UI from a different thread than the UI thread is bad. You need to marshal your code back onto the UI thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I'm parsing an XML file, the creators of it stuck in a bunch social
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.