-4

I am connecting with an instrument using the following command.

instrument = programmer.GetInstrument(_address);

I want to know how long it takes for this command to be executed.

Sophman
  • 85
  • 2
  • 17
  • 4
    Can you give more details as to why a stopwatch will not work in your case? – jgg99 Sep 15 '15 at 20:55
  • I need this to be deterministic and want to make sure I am only tracking the time to execute this command. At least this is what I am told. I am going to run it with a simple timer a few times and post the results soon. – Sophman Sep 15 '15 at 21:00
  • `I am going to run it with a simple timer a few times and post the results soon.` You asked this question before trying it ? – Eser Sep 15 '15 at 21:09
  • @Eser I have already tried it but always get the same results which does not make sense in the context that it is executed. Stop flagging me with negative comments.I said I will try it a few more times... – Sophman Sep 15 '15 at 21:12
  • 2
    `Stop flagging me with negative comments.` I though I can vote freely here. If you don't like it try to ask better questions. – Eser Sep 15 '15 at 21:18

1 Answers1

2

If you just want to do an in-line thread, you could do this code:

System.Threading.Thread t = new System.Threading.Thread(delegate()
{
    try
    {
        DateTime cmdStartTime = DateTime.UtcNow;
        instrument = programmer.GetInstrument(_address);
        DateTime cmdEndTime = DateTime.UtcNow;

        TimeSpan totalCmdRuntime = cmdEndTime - cmdStartTime;
        Debug.WriteLine(String.Format("Programmer.GetInstrument({0}) command took {1} mSec to execute", _address, totalCmdRuntime.TotalMilliseconds));
    }
    catch { }
});
t.Name = "GetInstrumentTimer";
t.Start();
Jerren Saunders
  • 1,188
  • 1
  • 8
  • 26
  • this should do it. I will try it right now and find out if this makes a difference in my timing. – Sophman Sep 15 '15 at 21:14
  • This is essentially the same as using a stopwatch, but with much lesser precision, which may not be good for your use case. – jgg99 Sep 15 '15 at 21:17
  • Yes. It is likely your command is executing faster than the resolution of the timer you are using. See SO http://stackoverflow.com/questions/3744032/why-are-net-timers-limited-to-15-ms-resolution for some alternatives. – Jerren Saunders Sep 15 '15 at 21:21