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?