المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Event Handling in Windows Services [modified]



C# Programming
08-20-2010, 11:31 PM
Hi,

This question is a little generic, but is there a difference in the way Windows Services handle events vs. Windows Forms?

I want to create my own hotkey program to test out a new class I found in one of the articles here(UserActivityHook.cs). When I press 1 key, a combination of keys are sent

In a windows form, I could do something like this (shortened examples):

private void btnStart_Click(object sender, EventArgs e)
{
hook = new UserActivityHook();
hook.KeyPress += new KeyPressEventHandler(KeyPressHandler);
}

public void KeyPressHandler(object sender, KeyPressEventArgs e)
{
string strKeyPressed = e.KeyChar.ToString();

if(strKeyPressed == 'a')
{
SendKeys.Send("B");
System.Threading.Thread.Sleep(50);
SendKeys.Send("N");
}
}
So for a Windows Service, I have a service up and running, I have confirmed that it starts properly with a few tests.

So if the windows service starts, and I do the same thing:

protected override void OnStart(string[] args)
{
hook = new UserActivityHook();
hook.KeyPress += new KeyPressEventHandler(KeyPressHandler);
}Assuming I have the same event handler, shouldn't this accomplish the same thing? (i.e. the Button click does the same thing as a service starts)

What's happening is that the Windows Forms app sends the hotkeys, but the Windows Service does not, so the event handler didn't run.

I don't know how straight forward this is, but I'm new to windows services http://www.barakasoft.com/script/Forums/Images/smiley_line.gif

Thanks!
modified on Friday, August 20, 2010 1:33 PM