A
AnnabelleTrinh
Guest
I'm using the current code to check if Excel is starting. When it starts, I want to get the app using Marshal.getActiveObject().
private ManagementEventWatcher WatchForProcessStart(string processName)
{
string queryString =
"SELECT TargetInstance" +
" FROM __InstanceCreationEvent " +
"WITHIN 10 " +
" WHERE TargetInstance ISA 'Win32_Process' " +
" AND TargetInstance.Name = '" + processName + "'";
// The dot in the scope means use the current machine
string scope = @"\\.\root\CIMV2";
// Create a watcher and listen for events""
ManagementEventWatcher watcher = new ManagementEventWatcher(scope, queryString);
watcher.EventArrived += ProcessStarted;
watcher.Start();
return watcher;
}
private void ProcessStarted(object sender, EventArrivedEventArgs e)
{
var excelApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
if (excelApp != null)
{
// do stuff
}
}
However, right now, Marshal.getActiveObject() is triggered as soon as Excel is starting meaning that the App isn't completely initialized yet leading to the exception
"System.Runtime.InteropServices.COMException' in mscorlib.dll".
To tackle this issue, I've put the program to sleep for 10 seconds (long enough for Excel app to be fully loaded in my PC) with
System.Threading.Thread.Sleep(25000);
and that works, but I want to find another way do this because the 10-second sleep is hardware-dependent.
The cleanest way I've found would be to check if the process application is fully loaded, which I intended to do with the Status of Win32_Process but it's not implemented yet.
Thank you for your time.
Continue reading...
private ManagementEventWatcher WatchForProcessStart(string processName)
{
string queryString =
"SELECT TargetInstance" +
" FROM __InstanceCreationEvent " +
"WITHIN 10 " +
" WHERE TargetInstance ISA 'Win32_Process' " +
" AND TargetInstance.Name = '" + processName + "'";
// The dot in the scope means use the current machine
string scope = @"\\.\root\CIMV2";
// Create a watcher and listen for events""
ManagementEventWatcher watcher = new ManagementEventWatcher(scope, queryString);
watcher.EventArrived += ProcessStarted;
watcher.Start();
return watcher;
}
private void ProcessStarted(object sender, EventArrivedEventArgs e)
{
var excelApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
if (excelApp != null)
{
// do stuff
}
}
However, right now, Marshal.getActiveObject() is triggered as soon as Excel is starting meaning that the App isn't completely initialized yet leading to the exception
"System.Runtime.InteropServices.COMException' in mscorlib.dll".
To tackle this issue, I've put the program to sleep for 10 seconds (long enough for Excel app to be fully loaded in my PC) with
System.Threading.Thread.Sleep(25000);
and that works, but I want to find another way do this because the 10-second sleep is hardware-dependent.
The cleanest way I've found would be to check if the process application is fully loaded, which I intended to do with the Status of Win32_Process but it's not implemented yet.
Thank you for your time.
Continue reading...