Search code examples
c#winapinotepadmessage-loopwm-command

Why does Save As dialog block the sender?


The aim of my program is to trigger Save As dialog of Notepad via Save As item in Notepad's menu.

SendMessage(hWndOfNotepad, WM_COMMAND, SaveAsMenuItemId, IntPtr.Zero);
Console.log("Done!");

When run the code above, Notepad's Save As dialog show up and the console will log "Done!" untill user click Save Button. Why does my program have to wait? How to avoid it?


Solution

  • Your program blocks because the file dialog is a modal dialog. The message that you sent is synchronous and so does not return until it has been fully processed. Specifically when the modal file dialog closes.

    If you wish to automate other programs you should use the platform automation framework, UI Automation.