Threading problem

  • Thread starter Thread starter Paul E Collins
  • Start date Start date
P

Paul E Collins

I have written a game that allows the user to upload high scores to the Web.
I now want this upload to occur on a separate thread so that the window
remains responsive.

My attempt looks like this:

m_worker = new Thread(new ThreadStart(UploadScores));
m_worker.Start();
// ...
private void UploadScores()
{
try { /* ... do the upload ... */ }
catch (ThreadAbortException ex) { /* user cancelled */ }
catch (Exception ex) { /* set an error property for the caller to read
*/ }
}
// ...
private void btnCancel_Click(object sender, System.EventArgs e)
{
if (m_worker != null)
{
m_worker.Abort();
m_worker.Join(); // wait for the thread to abort
}
}

This seems to work. However, if I cancel and restart the upload a few
times - usually three or four - any subsequent upload attempts fail, timing
out after about a minute.

Is there something wrong with the code above? Am I not disposing of the
thread properly in btnCancel_Click? Is it possible that I am running out of
HttpWebRequest / HttpWebResponse objects because the thread aborts during
the upload?

Any help would be welcome. Debugging threads is a pain.

P.
 
Paul,

How are you performing the upload? If you are posting to a server, then
why not use the HttpWebRequest and HttpWebResponse classes? They provide an
Abort method which is "nicer" than just aborting a thread.

Hope this helps.
 
Back
Top