Launch a WPF application in a DLL from a console application exe - c#

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();
}

Related

Is it possible to open winform form from dependency?

I have a visual studio windows forms application that has multiple projects, each having multiple forms. The project that loads on startup calls another project's form and closes its own. Referring to the startup program as "setup" and the other as "main". I have a scenario where I want to skip setup and be able to return to it later. Since setup is a dependency of main, and not the other way around, I cannot seem to create an instance of the setup form. Is this the case, or am I doing something wrong?
The call from setup to open main is as so
this.Hide();
frmDemo demo = new frmDemo();
demo.ShowDialog();
this.Close();
I want to do the same thing from main to setup form, but I am having trouble.
Essentially it should just be
frmSetup setup = new frmSetup();
setup.show();
But this isn't working because it cannot find the form.
Well if you want to reference Setup from Main then you need to add a reference in Main.
The problem then is that you will create a circular dependency, which is bad design (and I think you will also get compiler errors).
There are some ways to get around this, but the best approach it to make your Main program the first one that starts-up (not setup). Then during startup it launches the set-up form and waits until it is finished and then continues. And then when it needs to launch setup again, then it is no problem.
So you are effectively reversing the dependency.

Run code once and exit formless app: Windows Forms or WPF... does it matter?

I need to write a very small application which writes some system data to a file and then exits. I could do this in a console application but I have no need or desire for a console window to appear during this process.
I would normally use a Windows Forms application with no forms, execute the code in the Main method and then allow the application to exit, however, this time I couldn't help but wonder if this is the best way to do it and whether you could do it with a WPF application instead, what the differences are and whether or not once you've remove any forms/windows and unnecessary reference, it matters or not.
WPF and WinForms are two different libraries that show UIs.
If you never show a UI, you aren't using either of them.
You should start with a WinForms project (WPF projects set extra project metadata that you don't want), then delete the reference to System.Windows.Forms.dll.
Alternatively, start with a console project, then change the Output type to Windows Application.
Windows Forms with no window or console app with the type changes to windows application will give you the same result which is a simple app with Main() method and now windows.
WCF will only make sense if you actually want to display something as you're not going to use any of its features in your case.

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.

How to run code right when a C# application starts?

I have put some code inside of the public MainWindow() {} but I kept getting some obscure XAML parsing errors as soon as I did that (not on my computer but on 3 others I've tried it on - yep!)
Is there the preferred way to run code AS SOON as t he application starts?
The theory is I want it to call home and ask it it's ok to start. If it's not, I want the app to close out. Call it a makeshift copy-protection :)
Under normal circumstances, WPF creates the Main method (the entrypoint of the application) for you. Your options
Create a handler for the Application.Startup event and put your code there. Alternatively, you can override the OnStartup() method.
If that's too late for you, put your code in the App's parameterless constructor (it probably doesn't exist, but you can create it).
If even that's too late, you can create your own Main() method. There are several ways how to do that. Probably the easiest is to put it in another class and tell Visual Studio you want to use this method in the project's properties.
On the other hand, you said you're getting some obscure XAML parsing errors. Maybe you should figure out what exactly do they mean?
You have Window.Loaded event in WPF.
But if if you want to check for run permission before application loads ( due some resource consuption or some business strategy) use a bootstrapper a separate small executable that first launched by mainexe and after if everything ok a bootstrapper runs main exe

WPF form in a console app

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.

Categories

Resources