コード例 #1
0
        /// <summary>
        /// Starts a new process.
        /// </summary>
        public IRedisProcess Start(int port = RedisDefaults.DefaultPort)
        {
            var cmd = ConstructProcessCommandLine(port);

            List <string> errorOutput    = new List <string>();
            List <string> standardOutput = new List <string>();

            Process redisServerProcess = null;

            try
            {
                redisServerProcess = Process.Start(cmd);
                standardOutput.Add("redis-server started on successfully");
            }
            catch (Exception ex)
            {
                errorOutput.Add(string.Format("Cound not start redis-server.  Error: {0}", ex.Message));
            }

            RedisProcess redisProcess = new RedisProcess(redisServerProcess)
            {
                ErrorOutput    = errorOutput,
                StandardOutput = standardOutput
            };

            return(redisProcess);
        }
コード例 #2
0
        /// <summary>
        /// Starts a new process with awaiter.
        /// </summary>
        public Task <IRedisProcess> StartAsync(int port = RedisDefaults.DefaultPort, int timeoutMilliseconds = 5000)
        {
            var initializeWaiter = new TaskCompletionSource <IRedisProcess>().Timeout(timeoutMilliseconds);

            var cmd = ConstructProcessCommandLine(port, true);

            List <string> errorOutput    = new List <string>();
            List <string> standardOutput = new List <string>();

            Process redisServerProcess = null;

            try
            {
                redisServerProcess = Process.Start(cmd);
            }
            catch (Exception ex)
            {
                redisServerProcess?.Kill();
                initializeWaiter.TrySetException(
                    new Exception(string.Format("Cound not start redis-server.  Error: {0}", ex.Message)));
            }

            redisServerProcess.OutputDataReceived += (sender, eventArg) =>
            {
                lock (lockObject)
                {
                    if (IsInitializeSussess(eventArg))
                    {
                        standardOutput.Add("redis-server started on successfully");

                        RedisProcess redisProcess = new RedisProcess(redisServerProcess)
                        {
                            ErrorOutput    = errorOutput,
                            StandardOutput = standardOutput
                        };

                        initializeWaiter.TrySetResult(redisProcess);
                    }
                }
            };

            redisServerProcess.BeginOutputReadLine();

            return(initializeWaiter.Task.ContinueWith(t => {
                if (t.IsFaulted)
                {
                    redisServerProcess?.Kill();
                    throw t.Exception.InnerException;
                }
                return t.Result;
            }));
        }