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

来源:互联网 发布:unity3d实现场景漫游 编辑:程序博客网 时间:2024/06/11 02:22

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


查看主页面文件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);            }        }
0 0