I am trying to timeout and throw an exception after waiting a specified amount of time, and am wondering if the way I'm currently doing it is best.
class Timeout
{
XmlDocument doc;
System.Object input;
public Timeout(XmlDocument doc, System.Object input)
{
this.doc = doc;
this.input = input;
}
public void run()
{
if (input is Stream)
{
doc.Load((Stream)input);
}
else if (input is XmlReader)
{
doc.Load((XmlReader)input);
}
else if (input is TextReader)
{
doc.Load((TextReader)input);
}
else
{
doc.Load((string)input);
}
System.Threading.Thread.CurrentThread.Abort();
}
}
private void LoadXmlDoc(XmlDocument doc, System.Object input)
{
Timeout timeout = new Timeout(doc, input);
System.Threading.Thread timeoutThread = new System.Threading.Thread(new ThreadStart(timeout.run));
timeoutThread.Start();
System.Threading.Thread.Sleep(this.timeout * 1000);
if (timeoutThread.IsAlive)
{
throw new DataSourceException("timeout reached", timeout.GetHashCode());
}
}
This current approach does work, so I'm just wondering if there's a simpler/better way to go about accomplishing the same thing.