コード例 #1
0
ファイル: Program.cs プロジェクト: jimxshaw/samples-csharp
        static void Main(string[] args)
        {
            Console.Write("Press enter to star the timer.");
            Console.ReadLine();

            StopWatch myStopWatch = new StopWatch();
            myStopWatch.Start();

            Console.Write("Press enter to stop the timer.");
            Console.ReadLine();
            myStopWatch.Stop();

            Console.ReadLine();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            var stopWatch = new StopWatch();

            for (var i = 0; i < 2; i++)
            {
                stopWatch.Start(DateTime.Now);

                for (var j = 0; j <= 1000; j++)
                {
                    Thread.Sleep(1);
                }

                stopWatch.Stop(DateTime.Now);

                Console.WriteLine(stopWatch.GetInterval().ToString());
                Console.ReadLine();
            }
        }
コード例 #3
0
        static void Main(string[] args)
        {
            try
            {
                var watch = new StopWatch();
                watch.Start();
                Thread.Sleep(5000);
                Console.WriteLine(watch.Stop());

                watch.Start();
                Thread.Sleep(3000);

                Console.WriteLine(watch.Stop());
            }
            catch (Exception)
            {
                Console.WriteLine("Error");
            }
        }
コード例 #4
0
        static void Main(string[] args)
        {
            var stopWatch = new StopWatch();

            Console.WriteLine("Press Enter to start StopWatch or Exit");
            while (true)
            {
                var input = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(input))
                {
                    stopWatch.Start();
                    Console.ReadLine();
                    stopWatch.Stop();
                    Console.WriteLine(stopWatch.Duration());
                    continue;
                }
                break;
            }
        }
コード例 #5
0
        static void Main(string[] args)
        {
            StopWatch stopwatch = new StopWatch();

            stopwatch.Start();
            int[]  array = new int[20];
            Random rand  = new Random();

            for (int i = 0; i < array.Length; i++)
            {
                array[i] = rand.Next(1, 50);
                Console.Write(array[i] + " ");
            }
            selectionSort(ref array);
            Console.WriteLine("Sorted array: \n");
            printArray(ref array);
            stopwatch.Stop();
            Console.WriteLine("Time used: " + stopwatch.GetElapsedTime());
        }
コード例 #6
0
        static void Main(string[] args)
        {
            var stopwatch = new StopWatch();

            Console.WriteLine("This is a StopWatch application");
            Console.WriteLine("Press 'space' to start and stop, 'ESC to quit'");
            Console.WriteLine();

            while ((Console.ReadKey().Key != ConsoleKey.Escape) || (Console.ReadKey().Key == ConsoleKey.Spacebar))
            {
                if (stopwatch.MeasurementStarted == false)
                {
                    stopwatch.Start();
                }
                else
                {
                    stopwatch.Stop();
                }
            }
        }
コード例 #7
0
        static void Main(string[] args)
        {
            try
            {
                var Stopper = new StopWatch();
                Stopper.Start();
                System.Threading.Thread.Sleep(1000);
                Stopper.Stop();
                Console.WriteLine(Stopper.Interval.Seconds);

                Stopper.Start();
                System.Threading.Thread.Sleep(1200);
                Stopper.Stop();
                Console.WriteLine(Stopper.Interval.Seconds);

                Stopper.Start();
                Stopper.Start();
            }
            catch (Exception e) {
                Console.WriteLine(e);
            }
        }
コード例 #8
0
        static void Main(string[] args)
        {
            var startTime = StopWatch.Start();

            System.Console.WriteLine("Start Time : " + startTime);

            Thread.Sleep(60000);
            var endTime = StopWatch.Stop();

            System.Console.WriteLine("End Time : " + endTime);
            Console.WriteLine(StopWatch.Duration());

            startTime = StopWatch.Start();

            System.Console.WriteLine("Start Time : " + startTime);

            Thread.Sleep(60000);
            endTime = StopWatch.Stop();

            System.Console.WriteLine("End Time : " + endTime);
            Console.WriteLine(StopWatch.Duration());
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: blightfoot71/My-Repository
        static void Main(string[] args)
        {
            var timer = new StopWatch();

            timer.Start();

            Thread.Sleep(1000);

            timer.Stop();

            Console.WriteLine("Duration: " + timer.Duration);

            timer.Start();

            for (var i = 0; i < 1000; i++)
            {
                Thread.Sleep(1);
            }

            timer.Stop();

            Console.WriteLine("Duration: " + timer.Duration);

            timer.Start();

            Thread.Sleep(1000);

            timer.Stop();

            Console.WriteLine("Duration: " + timer.Duration);
            timer.Start();

            Console.WriteLine("Press Enter to stop the timer.");
            Console.Read();

            timer.Stop();

            Console.WriteLine("Duration: " + timer.Duration);
        }
コード例 #10
0
        static void Main(string[] args)
        {
            var      stopWatch = new StopWatch();
            DateTime startTime;

            while (true)
            {
                Console.WriteLine("Please press \"Y\" to start the StopWatch");

                var userInput  = Console.ReadKey();
                var pressedKey = userInput.KeyChar.ToString();
                if (pressedKey == "Y")
                {
                    startTime = stopWatch.Start();
                }
                Console.WriteLine("Please Press \"N\" to stop the StopWatch");
                userInput  = Console.ReadKey();
                pressedKey = userInput.KeyChar.ToString();
                if (pressedKey == "N")
                {
                    stopWatch.Stop(startTime);
                }
            }
        }
コード例 #11
0
        static void Main(string[] args)
        {
            Console.WriteLine("Press 1 to Start the watch\nPress 2 to stop the watch\nPress 0 to exit the program.");
            var       userInput = Convert.ToInt32(Console.ReadLine());
            StopWatch stopWatch = new StopWatch();
            bool      isStarted = false;

            while (userInput != 0)
            {
                if (userInput == 1)
                {
                    stopWatch.Start();
                    if (isStarted)
                    {
                        throw new InvalidOperationException("Stop Watch is already running");
                    }
                    isStarted = true;
                    Console.WriteLine("Started. Press 2 to Stop or 0 to exit.");
                    userInput = Convert.ToInt32(Console.ReadLine());
                }
                else if (userInput == 2)
                {
                    if (isStarted)
                    {
                        stopWatch.Stop();
                        isStarted = false;
                        Console.WriteLine("Stopped. Press 1 to Start or 0 to exit.");
                    }
                    else
                    {
                        Console.WriteLine("Start the watchor 0 to exit.");
                    }
                    userInput = Convert.ToInt32(Console.ReadLine());
                }
            }
        }
コード例 #12
0
        static void Main(string[] args)
        {
            var stopWatch = new StopWatch();

            Console.WriteLine("Welcome to Panda-Karady\'s Stop Watch");
            Console.WriteLine("Press 1 - Start, 2 - Stop, 0 - Exit");
            while (true)
            {
                var userInput = Console.ReadLine();
                var isNumber  = int.TryParse(userInput, out var value);
                if (isNumber)
                {
                    if (value == 1)
                    {
                        stopWatch.Start();
                    }
                    else if (value == 2)
                    {
                        stopWatch.Stop();
                    }
                    else if (value == 0)
                    {
                        Console.WriteLine("**********************");
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Not valid input. Try again...");
                    }
                }
                else
                {
                    Console.WriteLine("Not valid input. Try again...");
                }
            }
        }
コード例 #13
0
        private void btn_RunSim_Click(object sender, EventArgs e)
        {
            isSimPaused = false;
            if (isSimRunning)
                return;
            disableConfiguration();

            label_Info.Text = "Configuring Simulation.";
            label_Info.Refresh();
            System.Threading.Thread.Sleep(250);
            // ********** VERIFY CONFIGURATION **********
            // This could be moved to click/change events
            // and the Begin button disabled until config
            // is validated.
            bool valid = true;

            if (selected_IApplicationEventGenerator.ForeColor == Color.Red)
                valid = false;
            if (selected_IDeployer.ForeColor == Color.Red)
                valid = false;
            if (selected_ILocation.ForeColor == Color.Red)
                valid = false;
            if (selected_INodeFactory.ForeColor == Color.Red)
                valid = false;
            if (selected_IPhysicalProcessor.ForeColor == Color.Red)
                valid = false;
            if (selected_IRandomizerFactory.ForeColor == Color.Red)
                valid = false;
            if (selected_GraphicsRunning.ForeColor == Color.Red)
                valid = false;

            string filename = text_IOFolder.Text + "\\"
                    + text_IOFileTag.Text + "_SimInfo.txt";

            if (filename.IndexOfAny(System.IO.Path.GetInvalidPathChars()) > -1)
                valid = false;

            if (!valid)
            {
                enableConfiguration();
                label_Info.Text = "ERR: Fix Highlighted Red/Yellow Options.";
                return;
            }

            // To prevent SW from getting stuck. Seems to happen when restarting many visualizations.
            sw = new StopWatch.StopWatch();
            sw.Reset();
            resetTimeTick = 0;

            // Set up modules & settings
            Type INodeFactoryType = (Type)this.cb_INode.SelectedItem;
            Type IPhysicalProcessorType = (Type)this.cb_IPhysicalProcessor.SelectedItem;
            Type IDeployerType = (Type)this.cb_IDeployer.SelectedItem;
            Type IApplicationEventGeneratorType = (Type)this.cb_IApplicationEventGenerator.SelectedItem;
            Type IRandomizerFactoryType = (Type)this.cb_IRandomizerFactory.SelectedItem;
            Type ILocationType = (Type)this.cb_ILocation.SelectedItem;
            double x1 = double.Parse(text_FieldX1.Text);
            double y1 = double.Parse(text_FieldY1.Text);
            double x2 = double.Parse(text_FieldX2.Text);
            double y2 = double.Parse(text_FieldY2.Text);

            double timeScale = double.Parse(TimeScale.Text);
            if (!cb_InvertScale.Checked)
                timeScale = 1 / timeScale;                          // Sim seconds per real second
            secPerTick = timeScale / double.Parse(FPS.Text);        // Sim seconds per frame

            bool multirun = false;
            int numruns = 1;
            if (cb_Multirun.Checked && (tb_Multirun.BackColor == Color.White))
            {
                multirun = true;
                numruns = int.Parse(tb_Multirun.Text);
                disableConfigurationMultirun();
            }

            outputFile = null;

            // write to Run Info File
            string indent = "     ";
            StreamWriter simInfoFile = new StreamWriter(filename, false);
            simInfoFile.WriteLine("Simulation Tag: " + text_IOFileTag.Text);
            simInfoFile.Write("Run Flags: ");
            if (cb_AppSetsSink.Checked)
                simInfoFile.Write("[Application Sets Sink Node] ");
            if (cb_randomSink.Checked)
                simInfoFile.Write("[Randomized Sink Node] ");
            if (cb_GraphicsOff.Checked)
                simInfoFile.Write("[Graphics Off] ");
            else
                simInfoFile.Write("[Graphics On] ");
            if (cb_Multirun.Checked)
                simInfoFile.Write("[Multirun with " + tb_Multirun.Text + " Runs]");
            simInfoFile.Write("\n");

            simInfoFile.WriteLine("ILocation: " + ILocationType.ToString());
            simInfoFile.WriteLine(indent + "Initial Corner: ("
                + text_FieldX1.Text + ", " + text_FieldY1.Text + ")");
            simInfoFile.WriteLine(indent + "Final Corner:   ("
                + text_FieldX2.Text + ", " + text_FieldY2.Text + ")");

            // Initial randomizer factory
            IRandomizerFactory randomFactoryMultirun
                        = (IRandomizerFactory)Activator.CreateInstance(IRandomizerFactoryType);
            randomFactoryMultirun.PanelObjs = setPanelObjValues(PanelObjs_IRandomizerFactory);
            randomFactoryMultirun.Initialize();

            simInfoFile.WriteLine("IRandomizerFactory: " + IRandomizerFactoryType.ToString());
            foreach (PanelObj pObj in PanelObjs_IRandomizerFactory)
            {
                pObj.UpdateInfo();
                simInfoFile.WriteLine(indent + pObj.name + ": Text = " + pObj.text
                    + "; Value = " + pObj.value);
            }

            simInfoFile.WriteLine("IDeployer: " + IDeployerType.ToString());
            foreach (PanelObj pObj in PanelObjs_IDeployer)
            {
                pObj.UpdateInfo();
                simInfoFile.WriteLine(indent + pObj.name + ": Text = " + pObj.text
                    + "; Value = " + pObj.value);
            }

            simInfoFile.WriteLine("IPhysicalProcessor: " + IPhysicalProcessorType.ToString());
            foreach (PanelObj pObj in PanelObjs_IPhysProc)
            {
                pObj.UpdateInfo();
                simInfoFile.WriteLine(indent + pObj.name + ": Text = " + pObj.text
                    + "; Value = " + pObj.value);
            }

            simInfoFile.WriteLine("INodeFactory: " + INodeFactoryType.ToString());
            foreach (PanelObj pObj in PanelObjs_INode)
            {
                pObj.UpdateInfo();
                simInfoFile.WriteLine(indent + pObj.name + ": Text = " + pObj.text
                    + "; Value = " + pObj.value);
            }

            simInfoFile.WriteLine("IApplicationEventGenerator: " + IApplicationEventGeneratorType.ToString());
            foreach (PanelObj pObj in PanelObjs_IApplicationEventGenerator)
            {
                pObj.UpdateInfo();
                simInfoFile.WriteLine(indent + pObj.name + ": Text = " + pObj.text
                    + "; Value = " + pObj.value);
            }

            simInfoFile.WriteLine();

            // Start running simulation
            label_Info.Text = "Simulation Running";
            label_Info.Refresh();
            System.Threading.Thread.Sleep(250);

            for (run = 0; run < numruns; run++)
            {
                // SIM START
                isSimRunning = true;
                reporter = new Reporter(secPerTick);
                reporter.EnableGraphics = !cb_GraphicsOff.Checked;

                IRandomizerFactory randomFactory = (IRandomizerFactory)Activator.CreateInstance(IRandomizerFactoryType);
                randomFactory.PanelObjs = setPanelObjValues(PanelObjs_IRandomizerFactory);
                if (run == 0)
                    randomFactory.SetSeed(randomFactoryMultirun.InitialSeed);
                else
                    randomFactory.SetSeed(randomFactoryMultirun.CreateRandomizer().Next());

                simInfoFile.WriteLine("Run #" + run + ": Randomizer Seed Info");
                simInfoFile.WriteLine(indent + "Random Seed: " + randomFactory.InitialSeed);

                Nodes nodes = new Nodes(randomFactory.CreateRandomizer());

                EventManager eventMgr = new EventManager();

                IPhysicalProcessor physProc
                    = (IPhysicalProcessor)Activator.CreateInstance(IPhysicalProcessorType);
                physProc.PanelObjs = setPanelObjValues(PanelObjs_IPhysProc);

                ReporterIWF repIWF = new ReporterIWF(secPerTick, physProc.TransmissionSpeed,
                    physProc.PropagationSpeed);
                repIWF.Attach(reporter);
                physProc.RepIWF = repIWF;

                INodeFactory nodeFactory = (INodeFactory)Activator.CreateInstance(INodeFactoryType);
                nodeFactory.PanelObjs = setPanelObjValues(PanelObjs_INode);
                nodeFactory.Initialize(eventMgr, physProc, randomFactory, reporter);

                XYDoubleLocation[] field = new XYDoubleLocation[2];
                field[0] = new XYDoubleLocation(x1, y1);
                field[1] = new XYDoubleLocation(x2, y2);

                IDeployer deployer = (IDeployer)Activator.CreateInstance(IDeployerType);
                deployer.PanelObjs = setPanelObjValues(PanelObjs_IDeployer);
                deployer.Initialize(nodes, nodeFactory, field, randomFactory);

                //SimulationCompleteEvent finalEvent = new SimulationCompleteEvent();
                //finalEvent.Time = 0 /*hours*/   * 60 * 60
                //                + 10 /*minutes*/ * 60
                //                + 0 /*seconds*/;
                //eventMgr.AddEvent(finalEvent);

                deployer.Deploy();

                // Physical Processor needs to be initialized after the nodes are deployed.
                physProc.Initialize(nodes, eventMgr);
                repIWF.MaxBitDistance = physProc.MaximumRange;

                IApplicationEventGenerator eventGen = (IApplicationEventGenerator)
                        Activator.CreateInstance(IApplicationEventGeneratorType);
                eventGen.PanelObjs = setPanelObjValues(PanelObjs_IApplicationEventGenerator);
                eventGen.Attach(reporter);
                eventGen.Initialize(eventMgr, nodes, randomFactory.CreateRandomizer(),
                    field);

                if (cb_AppSetsSink.Checked)
                {
                    if (cb_randomSink.Checked)
                    {
                        eventGen.GenerateEvent();
                        nodes.SetRandomSinkNode();
                    }
                    else
                    {
                        INode[] furthestPair = nodes.FindFurthestNodes();
                        eventGen.GenerateEvent(furthestPair[0].Location);
                        furthestPair[1].IsSink = true;
                    }
                }
                else
                    eventGen.GenerateEvent();

                nodes.InitializeNodes();

                tab_INode.SelectedTab = tab_Statistics;

                backgroundWorker_RunSim.RunWorkerAsync(eventMgr);
                while (backgroundWorker_RunSim.IsBusy)
                    Application.DoEvents();

                if (cb_GraphicsOff.Checked)
                {
                    label_Info.Text = "Run " + (run + 1).ToString() + " of " + numruns.ToString()
                        + " is running.";
                    label_Info.Refresh();

                    runSimStruct runSimArgs;
                    runSimArgs.outputFile = outputFile;
                    runSimArgs.run = run;

                    backgroundWorker_RunSim.RunWorkerAsync(runSimArgs);
                    while (backgroundWorker_RunSim.IsBusy)
                        Application.DoEvents();
                }
                else
                {
                    label_Info.Text = "Visualizer Running";
                    label_Info.Refresh();
                    currTimeTick = 0;
                    timerDraw.Tick += new EventHandler(runVisualizer);
                    timerDraw.Start();
                }
            }

            if (cb_GraphicsOff.Checked)
            {// Press "stop"
                EventArgsMessage args = new EventArgsMessage("Simulation Complete.");
                btn_Stop_Sim_Click(sender, args);
                enableConfigurationMultirun();
            }

            simInfoFile.Close();
        }
コード例 #14
0
ファイル: Program.cs プロジェクト: spkennealy/C_sharp_notes
        public static void Main(string[] args)
        {
            var stopWatch = new StopWatch();

            stopWatch.Start();
        }