-4

how do you find the difference in time using two methords. and how to call subtract two methords using TimeSpan.

static void Main(string[] args)
{
    int baba;

    var stopWatch = new StopWatch();

    Console.WriteLine("Please enter in any value to get the start time");
    string ti = Console.ReadLine();            
    baba = Convert.ToInt32(ti);
    Console.WriteLine(baba);

    stopWatch.Start();
    Console.WriteLine(Console.Read());

    if(ti != null)
    {
        stopWatch.Stop();
        Console.WriteLine(Console.Read());
    }           

}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • 7
    have you read the documentation? if so where do you struggle? which error do you get? what have you tried so far? – styx Mar 20 '19 at 08:46
  • 2
    `ti` will never be `null`. In the worst case it will be an empty string : `""` – Mong Zhu Mar 20 '19 at 09:06
  • please describe exactly which time difference you want to calculate. Or do you want to have the elapsed time between `stopWatch.Start()` and `stopWatch.Stop()` ? if the latter is your concern then I would suggest to have a look at the property: `stopWatch.ElapsedMilliseconds`. – Mong Zhu Mar 20 '19 at 09:14
  • Mong Zhu I was trying to calculate the time difference not the elapse time. I wanted to use the time span but I could not call the methods from another class – Abraham Mar 20 '19 at 09:46
  • styx I want to get the time difference but how can I subtract the two methods the Start() method from the Stop() method. I tried calling them and both are public but they are not accessible. how can I access them and subtract the start time from the stoppage time – Abraham Mar 20 '19 at 09:56
  • did you write your own `StopWatch` class ? or did you use [class from the framework](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.stopwatch?view=netframework-4.7.2) ? – Mong Zhu Mar 20 '19 at 12:31
  • It is my own stopwatch class – Abraham Mar 21 '19 at 09:17
  • then you should have posted it as well, the calculation will be performed there, am I correct?=! – Mong Zhu Mar 21 '19 at 09:18

1 Answers1

0

See the possible duplicated link in the comment. Another way to calculate time difference is to get it from TimeSpan:

DateTime time1 = DateTime.Now;
Thread.Sleep(1000);
DateTime time2 = DateTime.Now;
TimeSpan ts = time2 - time1;
Console.WriteLine(ts.TotalMilliseconds);

Check it here: https://dotnetfiddle.net/bfHEKY

Xi Duan
  • 93
  • 1
  • 9
  • Yes this is easy but this works when you are in the same class. I created another class called stopwatch if you follow well and that's where I invoked the start() and stop() methods which get me the start time and stop time. now I want to get the difference between both times – Abraham Mar 20 '19 at 09:48