increasing Excel process place in taskmanager hierarchy

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

Hi,
I have an application that uses a excel in conjuction with a third
party application. I create an excel instance in c# and mark it as
invisible. The third party application is programmed to interact with
excel and everything works fine as I monitor interactions with my
hidden excel sheet. The problem comes if a user opens another
instance of excel. Is there any way that I can use the process
classes or another means to increase my invisible excel's task-
manager priority to be above every other excel instance? Making my
Excel application visible and then invisible temporarily makes it most
recent one but it creates a flickering and is likely resource
intensive.

Thanks,
Bob
 
I've never had need to try this, but
System.Diagnostics.Process.PriorityClass seems to be appropriate for this.
 
I've never had need to try this, but
System.Diagnostics.Process.PriorityClass seems to be appropriate for this..

I don't think that is the right solution. It throws an exception when
I try to change the priority either in C# or manually in task
manager. More generally, I am not trying to increase the programs
processor priority (ie how fast it executes), but rather just make
sure it is the top excel instance in visibility. Does a process with
higher priority show higher in visibility?
 
That is a different question than you asked before in my opinion...

The solution will likely involve FindWindow,
http://www.pinvoke.net/default.aspx/user32.FindWindow, and
SetForegroundWindow,
http://www.pinvoke.net/default.aspx/user32.SetForegroundWindow.

Now, the tricky part which I cannot answer, is how are you going to know
when to execute the code? I mean, that if you put in a timer and check if a
process of Excel exists, then you will be constantly moving Excel to the
front. This would be annoying as a user.

Then there is also the condition where there are more than one Excel
window...

I don't think that is the right solution. It throws an exception when
I try to change the priority either in C# or manually in task
manager. More generally, I am not trying to increase the programs
processor priority (ie how fast it executes), but rather just make
sure it is the top excel instance in visibility. Does a process with
higher priority show higher in visibility?
 
Thanks for your response, sorry if my question was unclear. Is this
the correct way to implement your suggestion, I am not used to
PInvoke. If it is correct, it doesn't seem to work.

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);

void b1_Click(object sender, EventArgs e)
{




Process[] allProcesses=Process.GetProcesses();
for (int i = 0; i < allProcesses.Length; i++)
{
if (allProcesses.ProcessName.Contains("EXCEL")){
excelHndl=allProcesses.Handle;
excelProc = allProcesses;
}
}

SetForegroundWindow(excelHndl);
}
Thanks Again,
Bob
 
Close... This line:

excelHndl=allProcesses.Handle;

should be:

excelHndl=allProcesses.MainWindowHandle;
 
Close...  This line:

  excelHndl=allProcesses.Handle;

should be:

  excelHndl=allProcesses.MainWindowHandle;




Thanks for your response, sorry if my question was unclear. Is this
the correct way to implement your suggestion, I am not used to
PInvoke.  If it is correct, it doesn't seem to work.
 [DllImport("user32.dll")]
      [return: MarshalAs(UnmanagedType.Bool)]
      static extern bool SetForegroundWindow(IntPtr hWnd);
      void b1_Click(object sender, EventArgs e)
      {
          Process[] allProcesses=Process.GetProcesses();
            for (int i = 0; i < allProcesses.Length; i++)
          {
           if   (allProcesses.ProcessName.Contains("EXCEL")){
                  excelHndl=allProcesses.Handle;
                  excelProc = allProcesses;
              }
          }

          SetForegroundWindow(excelHndl);
}
Thanks Again,
Bob


Beautiful, thanks! This seems to work fine as long as I initialize the
excel instance to be visible, get the main window handle then, and
then make it invisible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top