启动离线ClickOnce应用程序,并等待退出 [英] Start an offline ClickOnce Application and wait for Exit

查看:142
本文介绍了启动离线ClickOnce应用程序,并等待退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经部署了的ClickOnce Windows窗体应用程序(App A)

I have deployed a ClickOnce Windows Forms application (App A)

另外一个应用程序(App B)开始应用带一个文件名作为参数。
我做这个代码

Another application (App B) starts App A with a filename as parameter. I do this with this Code

var basePath = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
var location = String.Format(@"{0}\{1}\{2}\{3}",
    basePath, "MyCompany", "MyProduct", "MyApp.appref-ms");

var fileName = @"c:\temp\somefile.ext";
var uri = new Uri(fileName).ToString();

Process.Start(location, uri);



一个应用程序从抓取 AppDomain.CurrentDomain.SetupInformation.ActivationArguments文件名。 ActivationData [0] 和展示的内容。

这就像一个魅力。不过,现在我想App B就等待应用程序A退出。
但是通话Process.WaitForExit()立即返回。

This works like a charm. However, now I want App B to wait for App A to exit. But a call to Process.WaitForExit() returns instantly.

有没有办法打开的ClickOnce应用程序,并等待它要退出吗?我可以,如果需要的话,改变应用程序被运行结束的方式,但要求是,我需要运行的应用程序作为一个ClickOnce的应用程序(我知道的地方,我的用户配置文件 AppData\Local\Apps \2.0\ 文件夹中的EXE存在,并且可以直接启动,但如果我这样做, ApplicationDeployment.IsNetworkDeployed ApplicationDeployment.CurrentDeployment 为null。在我松的ClickOnce更新功能)。

Is there a way to open a ClickOnce App and wait for it to exit? I can, if necessary, change the way the app is opend but the requirement is that I need to run the app as a ClickOnce app (I know that somewhere in my user profile AppData\Local\Apps\2.0\ folder the exe exists and can be started directly but If I do that ApplicationDeployment.IsNetworkDeployed is false and ApplicationDeployment.CurrentDeployment is null. In that I loose the ClickOnce Update Capabilities).

推荐答案

我的建议是使用的应用程序中的一个互斥,并让应用程序b检查并等待它。这是从我的观点来看,最彻底的方法。

my suggestion would be to use Mutex in App A, and let App B check and wait for it. This is the cleanest way from my point of view.

应用一个这样做的时候启动:

App A does this when starts:

    private static Mutex mutex;

    public static void Main()
    {
        // if you want your app to be limited to a single instance
        // across ALL SESSIONS (multiple users & terminal services), then use the following line instead:
        // string mutexName = string.Format("Global\\{0}", ProgramInfo.AssemblyGuid);
        var mutexName = string.Format("Local\\{0}", SOME_SHARED_GUID);
        mutex = new Mutex(true, mutexName, out singleInstance);

        if (singleInstance == false)
        {

           // that means your app has more than one instance running
           // you need to decide what to do here.
        }

        // rest of initialization code

        Application.Run();

        // release the mutex so App B can continue
        mutex.ReleaseMutex();
    }

和App B就只是等待互斥被释放:

and App B just waits for the mutex to be released:

Process.Start(location, uri);
Thread.Sleep(5000);  // give it 5 seconds or so to check for updates and start
var mutexName = string.Format("Local\\{0}", SOME_SHARED_GUID);
mutex = new Mutex(false, mutexName);
mutex.WaitOne();

这篇关于启动离线ClickOnce应用程序,并等待退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆