4

In a VS2010 C# project we have a class derived from a MarshalByRefObject and we use Activator.GetObject to set it. We are using this derived class to talk to a machine across the network.

Sometimes the target machine is on and able to be pinged but it isn't running the program we want to talk to, this causes a 30 second wait followed by an exception. Is there a way to tell if my derived MarshalByRefObject is valid?

Currently a try/catch is handling this situation, but the 30 second wait is not acceptable.

LightLabyrinth
  • 362
  • 3
  • 14
  • can you put what the exact error msg as well as show how you are Marshalling the code..? are you Doing any Tunneling .. Tunel Claass – MethodMan Feb 10 '12 at 17:43
  • "No connection could be made because the target machine actively refused it IP:Port" I'm don't believe we are using any tunneling. – LightLabyrinth Feb 10 '12 at 18:26

1 Answers1

4

You could try having a Task call a Ping() method, and use the explicit timeout on the task...

Task task = new Task(() => { try { obj.Ping(); } catch {} });
task.Start();
if(!task.Wait(1000)) throw new TimeoutException();
// handle other task exceptions etc
Scott
  • 21,211
  • 8
  • 65
  • 72
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thank you, it's not what I was looking for but it has drastically reduced wait times. – LightLabyrinth Feb 10 '12 at 18:24
  • @LightLabyrinth note: you should probably have a try/catch **inside** the delegate – Marc Gravell Feb 10 '12 at 20:29
  • It appears this solution causes a "SocketException was unhandled by user code"-"No connection could be made because the target machine actively refused it IP:port". This is inside a try/catch. Using google it seems this is occurring in another thread so I cannot catch it... I tried putting a try/catch inside the delegate with no luck. – LightLabyrinth Feb 10 '12 at 21:08
  • 1
    @Light it is Task(() => { try { obj.Ping(); } catch {} }); right? – Marc Gravell Feb 10 '12 at 21:45