0

I am trying run Run a WPF app as both UI and from command line. In command line mode, i am printing some text on console and later i am coming out of console control using the sendKeys as given below

Console.WriteLine("Printing Something here");

System.Windows.Forms.SendKeys.SendWait("{ENTER}");

FreeConsole();

but the line with SendKeys throws error while running. In command line mode the application crashes. however while debugging on visual studio i have seen the below message on a message box

Changes are not allowed while code is running

Please provide any hint on this, why is this throwing error. so that i can make necessary changes.

Thanks in Advance Sagar

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
Sagar
  • 1,115
  • 2
  • 13
  • 22
  • 3
    _"Changes are not allowed while code is running"_ seems to rather be thrown by Visual Studio itself. It might be that the keypress is sent to the VS window where you have your code opened in a tab. If it sends an ENTER keypress to it then VS will interpret it as you are trying to modify code at runtime. – Visual Vincent Aug 12 '16 at 08:58
  • Is there a way to send Enter key to the console window? – Sagar Aug 12 '16 at 11:09
  • You could try [focusing your window first](http://stackoverflow.com/questions/10898560/how-to-set-focus-to-another-window) – Visual Vincent Aug 12 '16 at 11:14

1 Answers1

0

The command prompt program being debugged doesn't have focus on the UI, the CMD app is sending "Enter" to Visual Studio hence the reason why VS it complaining. Historically SendKeys and WPF have a horrible relationship and usually don't play well with each other. If you are looking to "test" a WPF based UI, I would look for wpf UI testing automation frameworks. They will play really well with the UI And allow you to script out exactly what you plan on performing and even record the outcomes.

Kevin B Burns
  • 1,032
  • 9
  • 24
  • Thanks Kevin for the answer. What i am trying to do here is to display some message on CMD app. because i have requirement to make the WPF app run as both UI and via command line, based on the need. During command line running i need to get out of the command prompt once i display some message – Sagar Aug 16 '16 at 09:23
  • Would it make sense to follow a more NTier style of coding then? Create a data access library and a service library and the CMD application and WPF application interface with that library instead of going this route? I am in a similar boat now, I have to create a web and wpf application, so it really made sense to go about it this way. I can manage the logging and service calls via the service layer and it works very nicely. – Kevin B Burns Aug 16 '16 at 12:54