0

If this is the way to get "timeout" of a system in Java:

timeoutMs = System.currentTimeMillis() + DataLog.getTimeout();
 try {
            dataLog.thread.join(DataLog.getTimeout());
        }
        catch (InterruptedException e) {}
        if (!dataLog.thread.interrupted()) {
            dataLog.thread.interrupt();
        }
        if (System.currentTimeMillis() > timeoutMs) {
            return ("Communication timeout");
        } else {
            return (dataLog.getResult());
        }
    }

Is the following code a good way to get the timeout of the system using C#?

Stopwatch sw = new Stopwatch();
    sw.Start();
    while (true)
    {
        timeoutMs=DataLog.getTimeout();
     }

It is a small part of a big Project so I do not have possibility to see the result in the output..please let me know If it is correct or not!

Thanks in advance...

m k
  • 9
  • 4
  • 4
    Code provided doesn't makes sense. Your java code seems to compute a time in future, where as your c# code loops indefinitely calling a method. – Sriram Sakthivel May 06 '14 at 11:04
  • Actually, The only thing that I want to do is converting that one line java code to C# – m k May 06 '14 at 11:10
  • What does `getTimeout` method returns? – Sriram Sakthivel May 06 '14 at 11:13
  • It returns: public static long getTimeout() { return Timeout.getDataLogTimeout(); } – m k May 06 '14 at 11:16
  • And `Timeout.getDataLogTimeout()` presumably returns `Settings.getDataLogTimeout()`. – vgru May 06 '14 at 11:17
  • In Timeout class there is a function which is called getDataLogTimeout() – m k May 06 '14 at 11:17
  • 1
    @mk: you should describe **what** you are trying to accomplish. The code you have posted doesn't make sense. It's hard to suggest a way to do something, if you don't describe the purpose of the code. – vgru May 06 '14 at 11:18

2 Answers2

0

First of all, if this is your way of pausing a thread (e.g. to display a dialog for a certain amount of time), it's a very bad way to do it.

That being said, if you're using a Stopwatch, you could spin the CPU by simply doing this:

// start the stop 
var sw = Stopwatch.StartNew();
while (sw.ElapsedMilliseconds < 1000)
{
     /* do nothing */ 
}

And, of course, you can do a nasty Thread.Sleep call to create a pause:

Thread.Sleep(1000);

The closest to your code in C# would look something like:

var timeoutMs = Environment.TickCount + DataLog.Timeout;

But I am pretty sure that you are doing something terribly wrong.

vgru
  • 49,838
  • 16
  • 120
  • 201
  • @mk: For the updated question, this link should be more appropriate: [Close a MessageBox after several seconds](http://stackoverflow.com/q/14522540/69809). There are many examples in that thread. – vgru May 06 '14 at 11:32
  • Thanks but I do not get their relevance! MessageBox and Timeout! – m k May 06 '14 at 11:38
0

Assuming you want the current time in milliseconds and then add the timeout to it from your method, assuming a millseconds return value:

var x = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
var timeout = DataLog.getTimeOut();
var timeoutMs = TimeSpan.FromMilliseconds(x).Add(TimeSpan.FromMilliseconds(timeout))
Jaycee
  • 3,098
  • 22
  • 31