AppiumForWindows 菜鸟计划(四)启动服务

这篇博客介绍了如何启动AppiumForWindows服务。通过点击启动按钮,该操作与LaunchCommand绑定。文中深入解析了服务启动的内部过程,涉及ProcessStartInfo的配置和使用,以及在新线程中执行服务启动的详细步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

看到了那个硕大的三角了么,启动服务就是点它,别犹豫,点吧。


查看主页面文件MainWindowVM.xaml,可以看到启动按钮:

    Command="{Binding LaunchCommand}"

说明该按钮绑定了LaunchCommand,查找:

    public ICommand LaunchCommand
    {
        get { return _LaunchCommand ?? (_LaunchCommand = new RelayCommand(() => _ExecuteLaunchCommand(), () => _CanExecuteLaunchCommand())); }
    }

继续查看:

    private void _ExecuteLaunchCommand()
    {
        if (_AppiumEngine.IsRunning)
        {
            _AppiumEngine.Stop();
        }
        else
        {
            _AppiumEngine.Start();
        }
    }

接着我们就找到了真正执行启动服务的地方了:

    public void Start()
    {
        //setup runner
        AppiumServerRunner setup = new AppiumServerRunner(this._NodePath, this._AppiumPackageFolder, _Settings);

        // setup basic process info
        var appiumServerProcessStartInfo = new ProcessStartInfo();
        appiumServerProcessStartInfo.WorkingDirectory = setup.WorkingDirectory;
        appiumServerProcessStartInfo.FileName = setup.Filename;
        appiumServerProcessStartInfo.Arguments = setup.GetArgumentsCmdLine() + " --log-no-color";
        appiumServerProcessStartInfo.RedirectStandardOutput = true;
        appiumServerProcessStartInfo.RedirectStandardError = true;
        appiumServerProcessStartInfo.CreateNoWindow = true;
        appiumServerProcessStartInfo.UseShellExecute = false;

        //set up the process and allow the thread to start it
        _AppiumServerProcess = new Process();
        _AppiumServerProcess.StartInfo = appiumServerProcessStartInfo;
        _AppiumServerProcess.OutputDataReceived += _Process_OutputDataReceived;
        _AppiumServerProcess.ErrorDataReceived += _Process_ErrorDataReceived;

        this._ServerExitMonitorThread = new Thread(() =>
        {
            _FireOutputData("Starting Node Server");
            _OnRunningChanged(true);
            this._AppiumServerProcess.Start();
            this._AppiumServerProcess.BeginOutputReadLine();
            this._AppiumServerProcess.BeginErrorReadLine();
            this._AppiumServerProcess.WaitForExit();
            _OnRunningChanged(false);
            _FireOutputData("Node Server Process Ended");
        });
        this._ServerExitMonitorThread.Name = "Server Exit Monitor";
        this._ServerExitMonitorThread.Priority = ThreadPriority.BelowNormal;
        this._ServerExitMonitorThread.Start();
    }

额,整体来说就是new 一个ProcessStartInfo,各种初始化,各种赋值,然后交给新new的 Process,再然后起一个Thread,再在然后,执行……
appiumServerProcessStartInfo的详细信息:


如果仔细看上边的代码,就会发现,其实启动和停止走的是同一条路,只不过通过if (_AppiumEngine.IsRunning)进行判断,结束自然调用的就是stop了,没啥可说的了……

        public void Stop()
        {
            if (IsRunning && null != _ServerExitMonitorThread && _ServerExitMonitorThread.IsAlive && null != _AppiumServerProcess)
            {
                _AppiumServerProcess.OutputDataReceived -= _Process_OutputDataReceived;
                _AppiumServerProcess.ErrorDataReceived -= _Process_ErrorDataReceived;
                _AppiumServerProcess.Kill();
                _AppiumServerProcess.Dispose();
                _AppiumServerProcess = null;
                _FireOutputData("Killed Node Server.");
                _OnRunningChanged(false);
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值