开发者

how to start new instance of second project in new process

开发者 https://www.devze.com 2023-04-06 23:34 出处:网络
I\'m looking to show window (WPF) that is defined in separate class library project as new separate process. Is there any way to do this?

I'm looking to show window (WPF) that is defined in separate class library project as new separate process. Is there any way to do this?

I need to start the second project instance in new process, because there stay occupied memory when I start it by this way:

secondProject.WPFWindow win = new secondProject.WPFWindow();
win.Show();

I have ONE solution with multiple projects.

  • StartUp project is WPF app., output type: Windows a开发者_JS百科pplication (exe file).
  • All other projects are WFP app., output type: Class library (dll file).

Now I am running "applications" (other projects in this one solution built as dll) by this code:

secondProject.WPFWindow win = new secondProject.WPFWindow();
win.Show();

I want is runnig that apps in new process... Normally I would use Process.Start(), but I can't is this case because it needs exe file as agrument and I have (and want) DLLs.


You could pass command line arguments to the main .exe to tell it which of the 'sub-apps' to run. Then the main .exe can just launch itself in a new process and tell the new instance which sub-app to run. For example in the main .exe app, put logic like this in your Application class:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        //See if exactly one command line argument was passed
        if (e.Args.Length == 1)
        {
            //See if the argument is one of our 'known' values
            switch (e.Args[0])
            {
                case "launchApp2":
                    secondProject.WPFWindow win2 = new secondProject.WPFWindow();
                    win2.Show();
                    break;
                case "launchApp3":
                    thirdProject.OtherWPFWindow win3 = new thirdProject.OtherWPFWindow();
                    win3.Show();
                    break;
            }
        }

        //Probably want to call the base class always?
        base.OnStartup(e);
    }
}

Then anytime you want to launch one of the sub-app in a new process, you can do so like this:

public void LaunchApp2()
{
    //Start a new process that is ourself and pass a command line parameter to tell
    //  that new instance to launch App2
    Process.Start(Assembly.GetEntryAssembly().Location, "launchApp2");
}


Let's assume you have a solution with 2 projects - one project compiles to an application (EXE), while the second compiles to a class library (DLL). I'll assume that the DLL has some type defined (say, a window) which you want to start from the EXE.

The simplest way to do this, is simply add a reference to the DLL. Right-click on the EXE project in the Solution Explorer, and choose Add Reference.... Wait a minute while the dialog opens. From the Projects tab, select the DLL project. Click OK.

Now, in your EXE project, WPFWindow will available as an imported type. You need to add a

using secondProject;

to the top of each code file that uses WPFWindow. I normally do this automatically using the keyboard shortcut CTRL + Period.

The method I've described is the standard method of using DLLs in C#. You can load them manually, but that is a little more complex, and likely isn't what you want to do.

Edit:

Alexei is correct. I think what we have here is an XY Problem. What you're trying to do is probably very easy, but the approach (instantiating a window defined in a DLL) is not.

Be aware that any code you run (in your case, your WPFWindow) has to originate from an application, even though the code itself is defined in a DLL. A DLL by itself normally provides no information to the operating system about how to run any of the code contained within.

Consider adding another EXE project which runs your WPFWindow which you call using Process. This suggestion might be wrong, though, as we still don't know what your end goal is. You're asking "How do I flap my wings like a bird?" when the correct question could be "How do I buy a plane ticket?"

0

精彩评论

暂无评论...
验证码 换一张
取 消