簡體   English   中英

啟動脫機ClickOnce應用程序並等待退出

[英]Start an offline ClickOnce Application and wait for Exit

我已經部署了ClickOnce Windows窗體應用程序(App A)

另一個應用程序(App B)以文件名作為參數啟動App A. 我使用此代碼執行此操作

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

App A從AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0]獲取文件名並顯示內容。

這就像一個魅力。 但是,現在我希望App B等待App A退出。 但是對Process.WaitForExit()的調用會立即返回。

有沒有辦法打開ClickOnce應用程序並等待它退出? 如果有必要,我可以更改應用程序的運行方式,但要求是我需要將應用程序作為ClickOnce應用程序運行(我知道在我的用戶配置文件AppData\\Local\\Apps\\2.0\\文件夾中的某個地方存在exe並且可以直接啟動但是如果我這樣做,則ApplicationDeployment.IsNetworkDeployedfalseApplicationDeployment.CurrentDeployment為null。我放松了ClickOnce更新功能)。

我的建議是在App A中使用Mutex ,然后讓App B檢查並等待它。 從我的角度來看,這是最干凈的方式。

App A在啟動時執行此操作:

    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只是等待釋放互斥鎖:

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

問題是啟動appref-ms進程實際上並沒有啟動應用程序它啟動部署清單,然后啟動應用程序本身,因此您開始的進程會立即退出。

如果您知道名稱(我假設您這樣做),您可以添加一個檢查以查看應用程序何時啟動,如下所示:

string myAppName = "YourAppName";
DateTime startTime = DateTime.Now;
int newProcessId = 0;
List<int> runningProcessIds = new List<int>();

//find all the running processes and record their Ids
foreach (void proc_loopVariable in Process.GetProcessesByName(myAppName)) {
    proc = proc_loopVariable;
    runningProcessIds.Add(proc.Id);
}

//start the new process
Process.Start(location);

//wait for the new application to be started
while (!(Process.GetProcessesByName(myAppName).Count != runningProcessIds.Count)) {
    //timeout if we have not seen the application start
    if ((DateTime.Now - startTime).TotalSeconds > 30)
        break; 
}

//loop through all the running processes again to find the id of the one that has just started
foreach (void proc_loopVariable in Process.GetProcessesByName(myAppName)) {
    proc = proc_loopVariable;
    if (!runningProcessIds.Contains(proc.Id)) {
        newProcessId = proc.Id;
        break; 
    }
}

//wait for the application to finish
Process.GetProcessById(newProcessId).WaitForExit();

Debug.WriteLine("Finished");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM