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

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


↙↙↙阅读原文可查看相关链接,并与作者交流