I’m getting a terrible exception thrown and IDK what to do to fix it 🙁
System.ObjectDisposedException
ObjectDisposedException- at System.Threading.Timer.throwIfDisposed()
at System.Threading.Timer.Change(UInt32 dueTime, UInt32 period)
at System.Threading.Timer.Change(Int32 dueTime, Int32 period)
at System.Net.HttpWebRequest.ConnectionClient.Read(Byte[] data, Int32 offset, Int32 length)
at System.Net.HttpReadStream.NetworkRead(Byte[] data, Int32 offset, Int32 length)
at System.Net.ContentLengthReadStream.doRead(Byte[] data, Int32 offset, Int32 length)
at System.Net.HttpReadStream.ReadToDrain(Byte[] buffer, Int32 offset, Int32 length)
at System.Net.HttpReadStream.doClose()
at System.Net.ContentLengthReadStream.doClose()
at System.Net.HttpReadStream.Finalize()
I’ve read that this occurs when you try and reuse an HttpWebRequest or something like that, but I’ve double and triple checked the whole project and I’m not reusing any HttpWebRequest
I am downloading HTML on a separate thread, and sometimes I need to abort that thread, but I call Join so that shouldn’t be a big issue right?
Is there any way to catch this Exception or have it not be thrown? Or is there any way I can find exactly where it is been thrown from (which instance of HttpWebRequest or what ever object throws this)
EDIT:
Ok, a little more about what’s going on. I’m making HttpWebRequest calls on separate threads off the UI (like most people) but when I close the application, it hangs because the HTTP requests haven’t succeeded or timed out. So I decided to Abort them (such a violent word, don’t you think?) Thats when the exceptions started happening. If there is a better way to do it, i’m all ears!
Here is one of the methods that I’m calling that sometimes gets interrupted. I run this until there are at most 6 threads:
private static void startImageDownloading()
{
ThreadStart start = () =>
{
while (_list.Count > 0) // _list contains all of the images that need to be downloaded
{
ImageRequest req; // ImageRequests are containers that hold all the request info
lock (_list)
{
if (_list.Count > 0)
req = _list.Dequeue();
else continue;
}
if (req.Item.Parent.IsDisposed)
continue;
using (MemoryStream i = getImageWeb(req))
if (i != null)
{
foreach (var callback in req.Callbacks)
callback(i); // Callbacks is a List<ImageDelegate> object
if (req.Item.Parent != null && !req.Item.Parent.IsDisposed)
req.Item.Parent.Damage(); // Cross thread Invalidate call
}
}
Thread.CurrentThread.Priority = ThreadPriority.Lowest;
};
Thread t = new Thread(start) { IsBackground = true, Priority = ThreadPriority.BelowNormal };
t.Start();
_downloadThreads.Add(t);
}
Here is the getImageWeb:
private static MemoryStream getImageWeb(ImageRequest req)
{
int tries = 0;
try
{
MemoryStream ret = null;
while (tries < maxRetries && (ret = downloadImageFile(req.Uri)) == null && !req.Cancelled)
tries++;
return ret;
}
catch { }
return null;
}
And finally downloadImageFile:
private static MemoryStream downloadImageFile(string url)
{
MemoryStream ret = null;
try
{
var uri = new Uri(url);
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
webRequest.Method = "GET";
webRequest.Timeout = 10000;
webRequest.ProtocolVersion = HttpVersion.Version10;
webRequest.Proxy = null;
using (var webResponse = (HttpWebResponse)webRequest.GetResponse())
{
if (webResponse.StatusCode.ToString().Equals("OK"))
{
ret = new MemoryStream();
using (Stream responseStream = webResponse.GetResponseStream())
{
if (responseStream == null)
return null;
CopyStream(responseStream, ret);
}
ret.Position = 0;
}
}
}
catch (WebException e)
{
string error = "";
if (e.Response != null)
using (var response = e.Response)
using (var resp = response.GetResponseStream())
using (var errorStream = new StreamReader(resp))
error = errorStream.ReadToEnd();
}
catch (Exception exp)
{
}
return ret;
}
Aborting a thread causes any [non-shared] referenced objects to be disposed automatically (if
IDisposableis implemented by that object). There’s a timer which is configured to do something in intervals (it seems some async network activity). Aboting the thread caused that timer to be disposed and this is the reason for exception.Try to call appropriate disconnecting method instead of aborting the thread. As a general rule never abort a thread unless you are sure where it is and what’s the consequences of stopping it from finishing his job. More details is needed for talking more precise about your case.