コード例 #1
0
        static public int PrintListWithDelay(int[] myIntArray, int myDelayMilliseconds, string processName, string textPreface, bool reverseList, bool trackFirstProcess, ref DataModel d)
        {
            //Using ref passing of DataModel d to centralize tracking of timing
            int ErrorCount = 0;

            //Note: i needs to be a zero-index counter
            if (reverseList)
            {
                for (int i = myIntArray.Length - 1; i >= 0; i--)
                {
                    Log(textPreface + myIntArray[i].ToString());

                    if (trackFirstProcess)
                    {
                        d.SetFirstProcessName(i, processName);
                    }

                    Thread.Sleep(myDelayMilliseconds);
                }
            }
            else
            {
                for (int i = 0; i < myIntArray.Length; i++)
                {
                    Log(textPreface + myIntArray[i].ToString());

                    if (trackFirstProcess)
                    {
                        d.SetFirstProcessName(i, processName);
                    }

                    Thread.Sleep(myDelayMilliseconds);
                }
            }


            return(ErrorCount);
        }
コード例 #2
0
        static int Main(string[] args)
        {
            DateTime start = DateTime.UtcNow;

            Log("Starting Console: " + start.ToString());

            DataModel d = new DataModel();

            foreach (var c in ConfigurationManager.AppSettings)
            {
                Log("We have app.config value for '" + c.ToString() + "' with value '" + ConfigurationManager.AppSettings[c.ToString()] + "'");
            }

            Log("");

            var app                  = new CommandLineApplication();
            var argList              = app.Option("-l | --list <value>", "List of numbers seperated by commas (e.g. '--list 1,2,3,4,5')", CommandOptionType.SingleValue);
            var argGetEndpoint       = app.Option("-g | --get <value>", "API endpoint to call a GET request to (e.g. '--get http://worldtimeapi.org/api/timezone/Europe/London.json')", CommandOptionType.SingleValue);
            var argDelayMilliseconds = app.Option("-d | --delay <value>", "Delay interval in milliseconds between printing out numbers (e.g. '--delay 200')", CommandOptionType.SingleValue);
            var argRunSqlTest        = app.Option("-r | --runSqlTest", "Run the use cases that simulate API calls of 200, 500 and timeout", CommandOptionType.NoValue);

            app.HelpOption("-? | -h | --help");

            app.OnExecute(() =>
            {
                //Parse list and sum evens
                d.StringListOfNumbers = argList.HasValue() ? argList.Value() : "1,2,3,4,5,6,7,8,9";
                if (d.ParseStringIntoList())
                {
                    Log("Using list of: '" + d.StringListOfNumbers + "' with length " + d.NumberList.Count.ToString());

                    Log("The sum of all even numbers in the list is: " + d.GetEvenSum().ToString());

                    Log("");
                }
                else
                {
                    LogError("Error parsing list into integers, please check your input: '" + d.StringListOfNumbers + "'");

                    return(-1);
                }

                //Call REST endpoint
                d.GetEndpoint = argGetEndpoint.HasValue() ? argGetEndpoint.Value() : "http://worldtimeapi.org/api/timezone/Europe/London.json";
                Log("Calling endpoint: " + d.GetEndpoint);
                Log("Response: " + GetWebText(d.GetEndpoint));
                Log("");

                //Print out numbers from list with a delay between each
                //Need to confirm is int
                int _delay;
                if (!argDelayMilliseconds.HasValue())
                {
                    //Default
                    _delay = 200;
                }
                else if (int.TryParse(argDelayMilliseconds.Value(), out _delay))
                {
                    //Success parsed into int
                }
                else
                {
                    LogError("Error parsing delay milliseconds into integer, please check your input: '" + argDelayMilliseconds.Value() + "'");

                    return(-1);
                }

                Task t_user = Task.Factory.StartNew(() => PrintListWithDelay(d.IntList().ToArray(), _delay, "user", "User chosen delay " + _delay.ToString() + "ms: ", false, false, ref d));
                Task.WaitAll(t_user);
                Log("");


                //Run multiple threads
                List <Task> taskList = new List <Task>();

                Task t1 = Task.Factory.StartNew(() =>
                {
                    d.StartNewTask(1, "t1");
                    PrintListWithDelay(d.IntList().ToArray(), 500, "t1", "t1 (forward) - Delayed  500ms: ", false, true, ref d);
                    d.CompleteTask(1);
                });
                taskList.Add(t1);
                Task t2 = Task.Factory.StartNew(() =>
                {
                    d.StartNewTask(2, "t2");
                    PrintListWithDelay(d.IntList().ToArray(), 1000, "t2", "t2 (reverse) - Delayed 1000ms: ", true, true, ref d);
                    d.CompleteTask(2);
                });
                taskList.Add(t2);

                //Makes sure all tasks are complete
                Task.WaitAll(taskList.ToArray());

                //This command compares which t1/t2 task finishes first
                //Log(d.SummarizeTaskTracker());

                Log(d.SummarizeNumberListOrder());


                if (argRunSqlTest.HasValue())
                {
                    //Simulate 200, 500 and timeout web calls
                    if (ConfigurationManager.ConnectionStrings["ae_code_challange"] == null)
                    {
                        LogError("Unable to find connection string for 'ae_code_challange' in app.config");
                    }
                    else
                    {
                        using (SqlConnection sqlcon_connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ae_code_challange"].ConnectionString))
                        {
                            if (sqlcon_connection.State != System.Data.ConnectionState.Open)
                            {
                                sqlcon_connection.Open();
                            }

                            //Allow the input enpoint to be used, though it will not give the same 200/500/timeout responses
                            string baseEndpoint = argGetEndpoint.HasValue() ? argGetEndpoint.Value() : "http://*****:*****@"spInsert_server_response_log";

                                SqlCommand command  = new SqlCommand(sql, sqlcon_connection);
                                command.CommandType = System.Data.CommandType.StoredProcedure;

                                command.Parameters.Add("@start_time", SqlDbType.DateTime2).Value  = logevent.start_time;
                                command.Parameters.Add("@end_time", SqlDbType.DateTime2).Value    = logevent.end_time;
                                command.Parameters.Add("@status_code", SqlDbType.SmallInt).Value  = logevent.status_code;
                                command.Parameters.Add("@response_text", SqlDbType.VarChar).Value = logevent.response_text;
                                command.Parameters.Add("@error_code", SqlDbType.SmallInt).Value   = logevent.error_code;

                                command.ExecuteNonQuery();
                            }
                        }
                    }
                }

                Log("");
                Log("Program is complete.  Press any key to quit");
                Console.Read();

                return(0);
            });


            return(app.Execute(args));
        }