-1

We have a situation where we use the WebClient class to download some data from a server. We have control over both the client and server portions. Currently, each time a user does an action, a webclient is generated and used to make a connection to a server. The client then downloads some data in the background and does some work with it. The problem arises when the user causes this to happen repeatedly... On the server side (NOT .net based!) we see that the connection times out after our timeout period and not teardown is ever sent to the server. This brings me to my question...

Is there a proper way to signal or force a teardown of the HTTP connection used by the webclient class?

Code: pMPTF is a callback class to bring the .net event into the unmanaged world.

void Callback(System::Object^ sender, System::Net::DownloadDataCompletedEventArgs^ e)
{
   WebClient^ w = (WebClient^)sender;
   w->Dispose();
   if( e->Error == nullptr && pMPTF != NULL )
      pMPTF->ParseResponse(e->Result);
   else
      pMPTF->ParseResponse( nullptr );
}

Gives:

error C2039: 'Dispose' : is not a member of 'System::Net::WebClient'

steveg89
  • 1,807
  • 2
  • 15
  • 29
  • I get an compiler error when trying to call dispose. Apparently it's inaccessible from outside the WebClient.. – steveg89 Aug 13 '12 at 19:16
  • 2
    `Dispose` is a public method - it must be, since further down the inheritance chain, `IDisposable` is eventually implemented by WebClient. Furthermore, I can call it just fine from my sample program. Could you post the full error? – Nikola Anusev Aug 13 '12 at 19:19
  • 1
    Hm. I am no C++ guy, but according to the http://stackoverflow.com/questions/350052/how-do-you-dispose-of-an-idisposable-in-managed-c, you should use `delete w;` instead. Apparently, this will be translated to `Dispose()` under the hood. – Nikola Anusev Aug 13 '12 at 19:34
  • Alright. Thanks for the point in the right direction and sorry about the confusion! – steveg89 Aug 13 '12 at 19:39

1 Answers1

1

The operation is completed at that point. If use delete the object will be disposed. I would recommend reading-up on how objects are disposed in managed C++: msdn.microsoft.com/en-us/library/ms177197.aspx

If you want to manually close the response, you can call GetWebResponse and call the Close on the returned WebResponse object.