Hi again!
One problem solved, a new one occurs...
I have an application, which reads and writes to a website periodly every 5 seconds. I'm using (Http)WebRequest object. The application makes about 1200 successful read and write -process, but after that an error occurs. There were not enough free threads in the ThreadPool object to complete the operation. The error occurs especially in read process.
The first, I have openConnection() -method, where I create
a new instance of HttpWebRequest (named _webReq) for every
read and write process.
public XmlDocument readData()
{
XmlDocument data = null;
if(this._webReq != null)
{
WebResponse webRes = null;
StreamReader sr = null;
Stream stream = null;
try
{
//Reading xml over HTTP
this._webReq.Method = "GET";
webRes = this._webReq.GetResponse();
stream = webRes.GetResponseStream();
if(stream != null)
{
sr = new StreamReader(stream);
string xmlStr = sr.ReadToEnd();
//Let's create a new Xml -document
data = new XmlDocument();
data.InnerXml = xmlStr;
}
else
{
LogWriter.error("Getting a responsestream failed!", null);
}
}
catch(WebException ex);
{
data = null;
LogWriter.error("WebExcepion was catched in readData method.", ex.Message);
}
finally
{
if(sr != null)
{
sr.Close();
}
if(webRes != null)
{
if(stream != null)
{
stream.Close();
}
webRes.Close();
}
}
}
return data;
}
And I have own method for closing the connection
public bool closeConnection()
{
if(this._webReq != null)
{
try
{
this._webReq = null;
}
catch(Exception ex)
{
return false;
}
}
//else...the object doesn't exist, there is no connection to close
return true;
}
When the error occurs, the stack trace looks like 'at System.Net.HttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state) at System.Net.HttpWebRequest.GetResponse()'
So, where is the problem? Should I use ThreadPool or something....
br.
JuhaKa
One problem solved, a new one occurs...
I have an application, which reads and writes to a website periodly every 5 seconds. I'm using (Http)WebRequest object. The application makes about 1200 successful read and write -process, but after that an error occurs. There were not enough free threads in the ThreadPool object to complete the operation. The error occurs especially in read process.
The first, I have openConnection() -method, where I create
a new instance of HttpWebRequest (named _webReq) for every
read and write process.
public XmlDocument readData()
{
XmlDocument data = null;
if(this._webReq != null)
{
WebResponse webRes = null;
StreamReader sr = null;
Stream stream = null;
try
{
//Reading xml over HTTP
this._webReq.Method = "GET";
webRes = this._webReq.GetResponse();
stream = webRes.GetResponseStream();
if(stream != null)
{
sr = new StreamReader(stream);
string xmlStr = sr.ReadToEnd();
//Let's create a new Xml -document
data = new XmlDocument();
data.InnerXml = xmlStr;
}
else
{
LogWriter.error("Getting a responsestream failed!", null);
}
}
catch(WebException ex);
{
data = null;
LogWriter.error("WebExcepion was catched in readData method.", ex.Message);
}
finally
{
if(sr != null)
{
sr.Close();
}
if(webRes != null)
{
if(stream != null)
{
stream.Close();
}
webRes.Close();
}
}
}
return data;
}
And I have own method for closing the connection
public bool closeConnection()
{
if(this._webReq != null)
{
try
{
this._webReq = null;
}
catch(Exception ex)
{
return false;
}
}
//else...the object doesn't exist, there is no connection to close
return true;
}
When the error occurs, the stack trace looks like 'at System.Net.HttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state) at System.Net.HttpWebRequest.GetResponse()'
So, where is the problem? Should I use ThreadPool or something....
br.
JuhaKa