WPF form in a console app - c#

Im running a console app that loads a dll and calls a method from that dll that creates a WPF form. So I'm just calling to Program.Execute() method and it does all the creation of the form. All reflection business goes well, but the form does not appear.
I've been told that this is because a console app does not have a windows message loop, but I'm sure there is a way to simulate that. For example, I tried playing with System.Windows.Threading.DispatcherFrame, but to no avail - the form still does not show up.
Did anyone else face this problem?

Just call WPF's Application.Run(). Or Window.ShowDialog(), same thing. You will also have to apply the [STAThread] attribute on your Main() method.

Related

How do I run other windows forms while enabling a reference? Once I connect my reference, the application gets stuck on only one form

visual studio problem
I am trying to run different windows forms but once I enable my reference to TrackerLibrary, my Application.Run gets stuck on only being able to run the last form it was on before enabling my reference. Is there some way to fix this? (new to coding so I don't know if I'm missing anything)
The Application.Run method is what starts your application and the program will not go past that method call unless the user exits the application.
Using Application.Run(Form mainForm) will only allow you to start your application with 1 open form and the application will exit when that form is closed.
If you'd like to open more than 1 Form on start, consider using Application.Run(ApplicationContext context).
Application.Run Method documentation
Your application doesn't get "stuck on one form". You can open as many other forms as you want. What you can't do is close that first form without the application exiting. When you call Application.Run and pass a form, that method will return if and only if that form closes, at which point your Main method will also complete, if you have no more code after the Application.Run call.
If you want to be able to close a form without the application exiting then you cannot pass that form to Application.Run. What you can do instead is derive your own class from ApplicationContext and then create an instance of that class and pass it to Application.Run instead. You can then put whatever logic you want in that class to open and close as many forms as you like.
I'm not going to provide any code here because I don't know specifically what it is you want to do. Better that you follow the link above and work through the example it provides in order to understand the concept, then implement it as you need. You might also like to check out my own example of using a custom ApplicationContext here.

Launch a WPF application in a DLL from a console application exe

Currently I have two separate C# projects under the same solution, let's call it Window.exe and Console.exe. Window.exe is a WPF MVVM application that works well standalone.
To eliminate one small issue, my goal is to convert Window.exe into Window.dll, and then use Console.exe to load Window.dll. I tried to call App.Run(), or move the routine in App_Startup, that is used to launch the main window onto a separate method and called it. The new thread that runs Window.dll couldn't really last. It was able to populate the GUI when I stepped into it in debug, but I could not interact with it.
Any ideas on how I should proceed?
I was able to accomplish this by doing two things:
You need to mark the Main method in your console applicaiton as an STAThread method because the UI will need to be on an STA thread. If you don't, you'll get an exception when the constructor for the main window is called.
Make sure you also call InitializeComponent() before Run(). If you don't, the app will run, but the window won't have been set up first.
I was able to get this to work in a solution where I, as well, have a WPF main application and console application for testing things:
[STAThread]
static void Main(string[] args)
{
WpfApp.App app = new WpfApp.App();
app.InitializeComponent();
app.Run();
}

Creating Windows Form outside of Application.Run results in strange behaviour. Why?

After a week of code-debugging I found that the problem my Windows-Forms based GUI was behaving strangely was because I was running the form as:
Form1 myGui = new Form1();
Application.Run(myGui);
changing the code to:
Application.Run(new Form1());
apparently did the trick.
The problem i had was that, for some reason, if I created the form in the first way and tried to marshal a call with the Invoke/BeginInvoke pattern, the call was never resolved, leaving the other thread hanging in the case of Invoke, or simply never calling the function with BeginInvoke.
Can anyone explain why this happens?
Moreover, I'm running the application over Mono and Unity3D.
Thanks everyone
I have worked with many scenarios under Windows and Mono where form references are managed either globally or scoped to classes that are available during a static call to Application.Run in or via the Main method. This sounds too suspicious to be anything but a scoping issue. Have you tried a minimalist app with only the piece of code you mention? I could not reproduce the same results using Unity3D under Mono.

How can I run an application and do not show the console?

I've got a console application and I want to run it from another application. But when I invoke it from code--to call program I use ActiveXObject--it shows the console. How do I get it to not show the console? I mean, the program works but console is shown.
If you make the application a Windows Application instead of a Console Application it will not show or open (or use) a Console. Your code can stay the same - and the application will still function (without ever creating a Form or Window).
That being said, if you control the "other" application, you may want to consider making a class library instead of a separate Application. This would allow you to just include and run code within the library without starting a separate process.
You can create it as a Windows Forms application with a hidden window. That's just one possibility like Reed Copsey pointed out, i used it once because i needed to process some specific Window Messages.

Noob question - how to create a new windows form as part of a plugin?

I am writing a plugin for a C# application and would like to add a dialog window. I have no control over the application, rather, the application loads plugins dynamically using reflection. I am a newbie with windows forms (this is a forms application) but would like to have a dialog window come up to control my plugin. How can I accomplish this?
If I just add a windows form to my application via visual studio no form appears. Application.Run has presumably already been called by the main application. I am almost completely new to forms.
How can I start the form with with my plugin (the plugin has a method that is called when it is started) and make it active?
Edit: I should clarify, the main application application window will not respond (even to minimize or maximize the window) when a plugin is running, so presumably whatever thread is devoted to handling windows messages is used to run the plugin and is, temporarily, not handling any windows messages. Thus my form needs its own thread handling windows messages.
You will need to initialize your code from whatever method the plugin architecture defined as the entry point (where the application will call your plugin).
To show a form, you can call the Show method on it.
// In a method that the plugin framework calls
myPluginForm.Show();
The application that loads your plugin should have some facility to load a window. Check the API documentation. Also, do you know if there are other plugins that can create arbitrary new windows? Usually, the host application can allow the plugin to create certain predefined (by the host) types of windows (such as config, load a file, etc...).
It might also be possible to programmaticaly create a new form and then load it. See here for an example: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx and look for the "examples" section.

Categories

Resources