SendKeys to a differnt application from System tray App

M

Manoj Nair

Hi,

The problem :
Have a system tray application.This has a menu item 'Exit'.
On click of this a differnt application with a UI which runs in the
background should close.
The other application requires a Keyboard combination of Ctrl + Shift + Alt
+ X to close it properly.
After closing the other application the tray app should exit. (Process.Kill
cannot be used)
The following code doesnt work all the times.
Any Ideas.
Thanks in advance
------
//
//[DllImport("user32.dll")] public static extern int FindWindow(string
lpClassName,string lpWindowName);

int handle = Win32.FindWindow(null,"Basic Service");

//[DllImport("user32.dll")]public static extern bool SetForegroundWindow(int
hWnd);

Win32.SetForegroundWindow(handle);

handle = Win32.FindWindow(null,"Basic Service");

//[DllImport("user32.dll")]public static extern bool SetActiveWindow(int
hWnd);

Win32.SetActiveWindow(handle);

SendKeys.SendWait("+^%(x)");

processor.EndTasks();

noiIcon.Visible = false;

this.Close();

------------------------------
 
D

Derrick Coetzee [MSFT]

Manoj said:
Have a system tray application.This has a menu item 'Exit'.
On click of this a differnt application with a UI which runs in the
background should close.
The other application requires a Keyboard combination of Ctrl + Shift
+ Alt + X to close it properly.

Assuming that you are in control of both applications, this really just
isn't a good idea. Use some kind of interprocess communication, such as
remoting, a listening socket, or a shared file to ask the other application
to shut down; do not use SendKeys(). In particular, I've read this, although
this may not be up-to-date:

"But before you run off to use SendKeys, be advised that it's not as
reliable as SendInput. [...] For some reason, SendKeys doesn't always work.
I suspect it's a focus or timing problem-the keys are sent but then vanish
into the ether because the window you think has the focus doesn't actually
have it yet."
- http://msdn.microsoft.com/msdnmag/issues/05/01/CQA/

Also, I'm not sure why an application which runs in the background would
have a UI at all, but if it has a message loop you can probably close it by
sending it a close message. This page shows how:

http://www.codeproject.com/csharp/cskillapp.asp

I hope this helps.
 

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

Top