如何通过引用关闭申请?

时间:2017-01-27 19:52:57

标签: c# wpf

我在同一解决方案中有两个项目:Project (A)Project (B)

我已导入Project (A)中的Project (B),因此基本上我可以从A管理B。现在,当我关闭代表主项目的项目B的窗口时,我还需要关闭项目A的窗口。

在项目B中,当OnClosed事件被触发时,我执行此操作:

protected override void OnClosed(EventArgs e)
{
    Environment.Exit(0);
}

问题是项目B仍处于打开状态。如何在A内创建项目B的任何实例?

的情况下解决此问题

感谢。

3 个答案:

答案 0 :(得分:0)

您可以迭代正在运行的进程并按名称关闭进程。

    var processes = System.Diagnostics.Process.GetProcesses();
    foreach(var process in processes)
    {
        Console.WriteLine(process.ProcessName);
        if(process.ProcessName == "NameOfProcessToClose")
        {
            process.CloseMainWindow();
        }
    }

您还可以使用其他进程间机制。 建立IP套接字,命名管道或文件共享,以便将关闭通信到另一个进程。

答案 1 :(得分:0)

您可以使用CrossAppDomainDelegates在另一个应用程序中远程执行代码。
这是一个让您入门的示例:

namespace ApplicationB
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private SecondaryAppHelper _secondaryApp;

        protected override void OnStartup ( StartupEventArgs e )
        {
            _secondaryApp = new SecondaryAppHelper ( );
        }

        protected override void OnExit ( ExitEventArgs e )
        {
            _secondaryApp.Dispose ( );
        }
    }

    sealed class SecondaryAppHelper : IDisposable
    {
        private const string _secondaryDomainName = "ApplicationA Domain";
        private AppDomain _domain;

        public SecondaryAppHelper ( )
        {
            var setup = new AppDomainSetup ( ) { ApplicationBase    = AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
                                                 ConfigurationFile  = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, // Here we share the same app.config
                                                 ApplicationName    = AppDomain.CurrentDomain.SetupInformation.ApplicationName,
                                                 LoaderOptimization = LoaderOptimization.MultiDomainHost };

            _domain = AppDomain.CreateDomain ( _secondaryDomainName, null, setup );

            // Let's ask our new domain to launch ApplicationA
            // Without creating a new domain, the two WPF windows would share the same render thread.
            _domain.DoCallBack ( ( ) =>
                {
                    var thread = new Thread ( ( ) =>
                    {
                        var app = new ApplicationA.App ( );
                        var win = new ApplicationA.MainWindow ( );

                        app.MainWindow = win;

                        app.Startup += ( sender, e ) => { (sender as ApplicationA.App).MainWindow.Show ( ); };
                        app.Run ( );
                    } );

                    thread.SetApartmentState ( ApartmentState.STA ); // Necessary for WPF & WinForms
                    thread.IsBackground = true; // The thread will terminate itself nicely after the app closes
                    thread.Start ( );
                } );
        }

        public void Dispose ( )
        {
            _domain.DoCallBack ( ( ) =>
                {
                    if ( Application.Current != null )
                    {
                        EventHandler handler = null;

                        handler = ( sender, e ) =>
                            {
                                var dispatcher = sender as Dispatcher;
                                dispatcher.ShutdownFinished -= handler;  // Better safe than sorry

                                 AppDomain.Unload ( AppDomain.CurrentDomain );
                            };

                        Application.Current.Dispatcher.ShutdownFinished += handler;

                        Application.Current.Dispatcher.BeginInvokeShutdown ( DispatcherPriority.Background );
                    }
                } );
        }
    }
}

答案 2 :(得分:0)

解决方案非常简单:

1)在项目(b)中右键单击引用,然后单击添加引用

2)选择项目内的项目(a)

3)此时我们需要在B中创建项目A的实例,所以:

public static ProjectA.MainWindow projectA;

4)此时您需要打开项目A的应用程序,例如当您单击menuitem时:

private void MenuItem_Click(object sender, RoutedEventArgs e)
{
    projectA = new ProjectA.MainWindow();
}

这将打开projectA,所以为了关闭它,我们需要在projectA中创建一个方法:

public void Exit()
{
    Close(); //Implicit declaration of Window.Close();
}

所以在项目B中:

projectA.Exit();

将关闭整个应用程序。在这种情况下,你怎么看都没用,可以使用Mutex类或很多代码来实现这个简单的任务。

希望有帮助..