I open a FileStream to be written to Asynchronously.
m_oFile.BeginRead(arrInputReport, 0, m_nInputReportLength, new AsyncCallback(ReadCompleted), arrInputReport);
I dispose this using the following code:
if (bDisposing)
{
if (m_oFile != null)
{
m_oFile.Dispose();
m_oFile = null;
}
Unfortunately, after calling the dispose method, ReadComplete method still receives results:
protected void ReadCompleted(IAsyncResult iResult)
{
byte[] arrBuff = (byte[])iResult.AsyncState; // retrieve the read buffer
try
{
m_oFile.EndRead(iResult);
It will get a nullReference error at the m_oFile.EndRead line. Checking for null gets rid of the exception but just traps the program in this method. How can I dispose of the ReadComplete method?
If you don’t have reliable access to the instance you used, then I would suggest using the
AsyncStateto store all the things you need. For example:with:
Note a null-check isn’t enough, because if a new file has been used, you would be calling it on the wrong
FileInfoinstance – not a good thing.