I'm using Rebex FTP library (plain old FTP, no security needed). I'm working asynchronously, in a multi-threaded environment, so the code in my class looks pretty much like this:
public class MyDownloader : IDisposable
{
private readonly Ftp ftp = new Ftp();
private MemoryStream ms = new MemoryStream();
public void Download()
{
// connect, login, etc.
object state = null;
ftp.BeginGetFile("remote/path", ms, MyCallback, state);
}
private void MyCallback(IAsyncResult ar)
{
try
{
var size = ftp.EndGetFile(ar);
}
catch
{
// log, whatever
}
}
public void Dispose()
{
// disconnect if needed
ftp.Dispose();
}
}
The Questions
Let's say that there's a so-called race between 2 threads: Thread A calls the Dispose
method in my class, and Thread B invokes the callback method (MyCallback
) on the same time.
- Should I write my own mechanism for checking the state of my class (disposed or not)?
- Is it possible that after calling FTP's
Dispose
method, my class' MyCallback
method will be executed at all?
Thanks!