Thread with timeout, pls help

  • Thread starter Thread starter Dan Pavel
  • Start date Start date
D

Dan Pavel

Hi, I am kind of new in programming and I have a problem.
I am getting some values through SNMP and when I don't get an answer
from remote host, my refresh function keeps waiting. I cannot modify the
function because is from a .dll. I need some timeout thread who can stop
after 2 seconds if I don't get any answer from the remote host.

Here it is:

private string mib_read(string host, uint[] mib)
{
{
string val;
ManagerSession sess = new ManagerSession(host,"public");
ManagerItem mi = new ManagerItem(sess,mib);
try
{
logText("before [read] refresh...");
mi.Refresh(); //<-------------The crash !!! logText("after [read]
refresh...");
val=mi.Value.ToString();
}
catch (Exception e)
{
logText("Refresh Failed........."+e);
val="NULL";
}
return val;
}
}

Please help.
 
Hi,


It does depend of the lib functionality. Maybe it does provide a property
or a constructor overload where you can set the timeout.

If not you are pretty much without much else to do.

The only other possible solution I can think of is create a thread to do the
Refresh while the main thread sleep for two seconds, then check if the
worker thread is still blocked ( you may use a flag for this) and if it is
do accordingly.


cheers,
 
Hi,

There are 2 problems with this solution:
1. If I wait 2 seconds for every MIB read, my apllication will become
very slow.
2. I am not very good with threads, so if you can, give me a more
detailed solution.

I think that what I need is somehow to perform a check something like
that:

If the mi.Refresh() return a value within 2 seconds, continue to value
assign, else (if the 2 seconds passed) forgot the mi.Refresh value and
assign "NULL" to val.

Thanks in advance,
Dan
 
Back
Top