Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

MessageBox.Show(this, "my message");

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top