0

Essentially, this miniature Windows Form C# (.NET 3.5) application is having issues while listening to an event.

I setup two buttons on my form to directly call my two primary functions (these are sending keystrokes to another process window and toggling my boolean to show if it was sent or not already).

The part that is having issues are the actual events I am monitoring to automatically fire the same functions that the two buttons do. The eventhandlers are ONLY not successful if I DON'T include a MessageBox.Show("Blah Blah");

Example:

public tester()
InitializeComponent();
wsiRemote.clsWsiEventsClass Events = new wsiRemote.clsWsiEventsClass();
Events.AuthorRecordStarted += new __clsWsiEvents_AuthorRecordStartedEventHandler(Events_AuthorRecordStarted);
private static bool _pedcheck = false;
    public static void SendkeyT()
    {
        foreach (Process w in System.Diagnostics.Process.GetProcessesByName("WinScribe Internet Author"))
        {
            IntPtr hwnd = w.MainWindowHandle;
            SetForegroundWindow(hwnd);
        }

        if (_pedcheck == false)
        {
            foreach (Process p in System.Diagnostics.Process.GetProcessesByName("Pedable"))
            {
                    IntPtr hWnd = p.MainWindowHandle;
                    SetForegroundWindow(hWnd);
                InputSimulator.SimulateKeyDown(VirtualKeyCode.CONTROL);
                InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_D);
                InputSimulator.SimulateKeyUp(VirtualKeyCode.CONTROL);
                _pedcheck = true;
            }
        else
        {
            MessageBox.Show("Boolean Failure");
        }

    }
    public void button1_Click(object sender, EventArgs e)
    {
        SendkeyT();
    }

    public void Events_AuthorRecordStarted(string msg)
    {
       // MessageBox.Show("Recording Started");
        SendkeyT();
    }

If I uncomment the MessageBox, it fires off and displays the box to the user. Once the user clicks Okay, the function fires properly. I have noticed, the window that is being sent the keys and made active begins flashing in the taskbar if I don't include the messagebox, So I am assuming it's having to do with the foreground setup.

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

If anyone has any suggestions, I would greatly appreciate it.

Thanks!

000
  • 26,951
  • 10
  • 71
  • 101
mbeacom
  • 1,466
  • 15
  • 25
  • "I have noticed, the window that is being sent the keys and made active begins flashing in the taskbar if I don't include the messagebox" The flashing symptom sounds like the Operating System setting that prevents applications from stealing focus. It was available in the old TweakUI application, or could be done manually by editing the registry. Does the target system have this enabled by chance? – Idle_Mind Jul 08 '13 at 19:45
  • Thank you for the reply, @Idle_Mind. I just checked around and I am not seeing that to be the issue (as far as I can tell). I still can't explain why the button method is working fine, yet the API based hook isn't (unless calling a `MessageBox.Show`). From what I can tell, I am thinking it could have something to do with the button utilizing `object sender, EventArgs e` through event invoking. I will continue to look into the UI issue and I appreciate your assistance. Do you happen to have any other ideas? Thank you again! – mbeacom Jul 09 '13 at 12:50

2 Answers2

1

some times, the "Application"s main window handle is not the handle of the "Logical" main window, (which you want to set focus to), and most of these times, the main window is even hidden, so Set focus does not make any difference.

try itterating the Process's windows and find the one that you need to set forground.

You can use Process Explorer in order to investigate your app. (if it is a specific app you want to take action on.)

use: How to enumerate all windows within a process? to assist in getting the child windows.

Community
  • 1
  • 1
Tomer W
  • 3,395
  • 2
  • 29
  • 44
0
MessageBox.Show(this, "my message");

Passing "this" to MessageBox.Show method solved my problem.

ali
  • 529
  • 4
  • 26