1

I'm using WMI to detect when a process is open. Once the process is opened I need to show a new form. The thing is that I noticed that it doesn't work. And I don't have any idea why is this happening.

Debugging I also noticed that, in fact, the code in the new form is executed in spite of the form isn't showed.

private async void Form1_Load(object sender, EventArgs e)
{
   SetupEventWatcher();
}

private void SetupEventWatcher()
{
   string tick = "0.1";
   string lcuProcess = "LeagueClientUx.exe";
   string gameProcess = "notepad.exe";

   string lcuQuery = String.Format(@"
      SELECT * 
      FROM __InstanceOperationEvent 
      WITHIN {0} 
      WHERE TargetInstance ISA 'Win32_Process' 
      AND TargetInstance.Name = '{1}'", tick, lcuProcess
    );

   string gameQuery = String.Format(@"
      SELECT * 
      FROM __InstanceOperationEvent 
      WITHIN {0} 
      WHERE TargetInstance ISA 'Win32_Process' 
      AND TargetInstance.Name = '{1}'", tick, gameProcess
   );

   string scope = @"\\.\root\CIMV2";

   ManagementEventWatcher watcherLCU = new ManagementEventWatcher(scope, lcuQuery);
   watcherLCU.EventArrived += new EventArrivedEventHandler(OnEventArrived_Lcu);
   watcherLCU.Start();

   ManagementEventWatcher watcherGame = new ManagementEventWatcher(scope, gameQuery);
   watcherGame.EventArrived += new EventArrivedEventHandler(OnEventArrived_Game);
   watcherGame.Start();
}

// Detect when the game starts.
private static void OnEventArrived_Game(object sender, EventArrivedEventArgs e)
{       
   // If process is open. 
   if (e.NewEvent.ClassPath.ClassName.Contains("InstanceCreationEvent"))
   {
       InGame form1 = new InGame(summonerName);
       form1.Show();
   }
}

Once the notepad is opened (at first I thought it doesn't work due to the game process but I'm trying now with notepad and is happening the same stuff) the second form is supposed to be showed. (which in this case it's not being showed). What's happening?

Omar
  • 374
  • 1
  • 14
  • I'd like to know the reason of the down votes so I can improve my question. – Omar Jun 27 '21 at 01:21
  • form1.ShowModal(); perhaps – Ross Bush Jun 27 '21 at 01:33
  • Does -> InGame(summonerName) return a form or null? – Ross Bush Jun 27 '21 at 01:34
  • @RossBush In C# doesn't exists the ShowModal() method. (or at least I don't have that method) – Omar Jun 27 '21 at 01:35
  • @RossBush It's a WinForms, but I'm taking the string summonerName from the first WinForm to work with it in the second one – Omar Jun 27 '21 at 01:37
  • The WMI watcher event is raised in a different thread from the main UI thread. You need to use `Control.Invoke()` to get back onto the main UI thread before you actually show the new form. See duplicate. – Peter Duniho Jun 27 '21 at 02:18
  • When I try to use `form1.Invoke((MethodInvoker)(() => form1.Show()));` it gives me the next error `: 'Invoke or BeginInvoke cannot be called on a control until the window handle has been created.'` I also tried with the code from the duplicate `form1.BeginInvoke(new OpenFormDelegate(OpenNewForm));` but got `Object reference not set to an instance of an object.'` note that I made a `static InGame form1;` and initialize it in the `form_load` so I can have a control to invoke. would you mind to help me? Thanks! – Omar Jun 27 '21 at 03:05
  • @PeterDuniho I solved it by using `form1.CreateGraphics()` in the form load, then using `control.Invoke()` in the Thread. Neverthless I noticed if I close the control, I need to create the graphics again before open it in the thread, I also noticed that I can't creathe the graphics in the thread. How could I open/close the WinForm in the thread as many times as I want? thanks! – Omar Jun 27 '21 at 03:32
  • _"When I try to use form1.Invoke((MethodInvoker)(() => form1.Show())); it gives me the next error"_ -- you need to use a form instance that already exists, not the form you're trying to create. You need to call `Invoke()` _before_ even instantiating the new form; do all of the work to create and show the new form in the code that gets invoked. You definitely should not be calling `CreateGraphics()`. – Peter Duniho Jun 27 '21 at 03:40
  • @PeterDuniho I'm so sorry for real but I don't get it. *"You need to call Invoke() before even instantiating the new form"*. I'm lost here, because I don't know what's the control I should use to call `.Invoke()` method. *"you need to use a form instance that already exists"* what do you mean with this? I'm sorry but I'm neither good at programming nor speaking English. Thanks in advance – Omar Jun 27 '21 at 04:06
  • _"I don't know what's the control I should use to call .Invoke() method"_ -- an existing `Form` you've already shown would be the ideal option. If your program has not already shown a form, then you may need to create a thread specifically for the purpose of showing the form. See e.g. https://stackoverflow.com/a/4699443/3538012 – Peter Duniho Jun 27 '21 at 16:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/234270/discussion-between-omar-and-peter-duniho). – Omar Jun 27 '21 at 21:59

0 Answers0