public async Task LaunchAppAsync(HoloLensApplication app)
        {
            if (_devicePortal == null || !IsConnected)
            {
                Message = "Launch app failed.";
                return;
            }

            // 実行中のプロセスのリストを取得
            var processes = await _devicePortal.GetRunningProcessesAsync();

            // ターゲットのアプリを起動
            await _devicePortal.LaunchApplicationAsync(app.AppId, app.PackageName);

            var targetProcesses = processes.Processes
                                  // 間違って自分自身を殺さないようにフィルタ
                                  .Where(x => x.PackageFullName != app.PackageName)
                                  // 終了してはいけないプロセスを除外
                                  .Where(x => !DoNotCloseApps.Contains(x.Name, StringComparer.OrdinalIgnoreCase))
                                  // 空のものは除外
                                  .Where(x => !string.IsNullOrWhiteSpace(x.PackageFullName));

            // 順次他のプロセスを終了していく
            foreach (var process in targetProcesses)
            {
                try
                {
                    await _devicePortal.TerminateApplicationAsync(process.PackageFullName);
                }
                catch (Exception ex)
                {
                    Message = $"{process.AppName} の停止に失敗しました。";
                    Debug.WriteLine(ex);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Main entry point for handling listing processes
        /// </summary>
        /// <param name="portal">DevicePortal reference for communicating with the device.</param>
        /// <param name="parameters">Parsed command line parameters.</param>
        public static void HandleOperation(DevicePortal portal, ParameterHelper parameters)
        {
            RunningProcesses runningProcesses = null;

            if (parameters.HasFlag(ParameterHelper.Listen))
            {
                ManualResetEvent runningProcessesReceived = new ManualResetEvent(false);

                WebSocketMessageReceivedEventHandler <RunningProcesses> runningProcessesReceivedHandler =
                    delegate(DevicePortal sender, WebSocketMessageReceivedEventArgs <RunningProcesses> runningProccesesArgs)
                {
                    if (runningProccesesArgs.Message != null)
                    {
                        runningProcesses = runningProccesesArgs.Message;
                        runningProcessesReceived.Set();
                    }
                };

                portal.RunningProcessesMessageReceived += runningProcessesReceivedHandler;

                Task startListeningForProcessesTask = portal.StartListeningForRunningProcessesAsync();
                startListeningForProcessesTask.Wait();

                runningProcessesReceived.WaitOne();

                Task stopListeningForProcessesTask = portal.StopListeningForRunningProcessesAsync();
                stopListeningForProcessesTask.Wait();

                portal.RunningProcessesMessageReceived -= runningProcessesReceivedHandler;
            }
            else
            {
                Task <DevicePortal.RunningProcesses> getRunningProcessesTask = portal.GetRunningProcessesAsync();
                runningProcesses = getRunningProcessesTask.Result;
            }

            foreach (DeviceProcessInfo process in runningProcesses.Processes)
            {
                if (!string.IsNullOrEmpty(process.Name))
                {
                    Console.WriteLine(process.Name);
                }
            }
        }