Click here to Skip to main content
15,903,388 members
Home / Discussions / C#
   

C#

 
QuestionSystem.ObjectDisposedException: Cannot access a disposed object. Pin
Jacob Dixon23-May-10 18:17
Jacob Dixon23-May-10 18:17 
AnswerRe: System.ObjectDisposedException: Cannot access a disposed object. Pin
Luc Pattyn23-May-10 19:02
sitebuilderLuc Pattyn23-May-10 19:02 
GeneralRe: System.ObjectDisposedException: Cannot access a disposed object. Pin
Jacob Dixon24-May-10 2:44
Jacob Dixon24-May-10 2:44 
GeneralRe: System.ObjectDisposedException: Cannot access a disposed object. Pin
Luc Pattyn24-May-10 4:25
sitebuilderLuc Pattyn24-May-10 4:25 
GeneralRe: System.ObjectDisposedException: Cannot access a disposed object. Pin
Jacob Dixon24-May-10 5:01
Jacob Dixon24-May-10 5:01 
GeneralRe: System.ObjectDisposedException: Cannot access a disposed object. Pin
Luc Pattyn24-May-10 5:06
sitebuilderLuc Pattyn24-May-10 5:06 
GeneralRe: System.ObjectDisposedException: Cannot access a disposed object. [modified] Pin
Jacob Dixon24-May-10 5:26
Jacob Dixon24-May-10 5:26 
GeneralRe: System.ObjectDisposedException: Cannot access a disposed object. Pin
Luc Pattyn24-May-10 6:23
sitebuilderLuc Pattyn24-May-10 6:23 
well, now it fails in finished, and rightly so; you basically have:
if (!stop) {
    MemoryStream ms = new MemoryStream(buffer);   <<< long operation
    if (Finished != null) {
        Image img=Image.FromStream(ms);           <<< long operation
        Finished(img);
        ...
}


and during those long operations another thread (the GUI thread handling your attempt to close down) has ample time to modify variables which you have already tested and passed.

Change it to:
if (!stop) {
    MemoryStream ms = new MemoryStream(buffer);   <<< long operation
    if (Finished != null) {
        Image img=Image.FromStream(ms);           <<< long operation
        if (Finished != null && !stop) Finished(img);
    }
        ...
}

which you may simplify to:
MemoryStream ms = new MemoryStream(buffer);   <<< long operation
Image img=Image.FromStream(ms);               <<< long operation
if (Finished != null && !stop) Finished(img);
...

if you want (possibly causing some cycles being wasted in some scenario's)

BTW: the same applies to progress; there too, the inner conditional block should be minimal.
This approach isn't waterproof yet, it still is conceivable that the situation changes (variables change value AND form gets closed AND everything gets collected) between the if and the event being called. One extra safety measure would be to add a small delay in the FormClosing event:
change the variables
Thread.Sleep(100);

as the FormClosing will be followed by a FormClosed event, and only then the form will be disposed.

Smile | :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]

I only read formatted code with indentation, so please use PRE tags for code snippets.

I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).

GeneralRe: System.ObjectDisposedException: Cannot access a disposed object. Pin
Jacob Dixon24-May-10 6:41
Jacob Dixon24-May-10 6:41 
GeneralRe: System.ObjectDisposedException: Cannot access a disposed object. Pin
Luc Pattyn24-May-10 6:54
sitebuilderLuc Pattyn24-May-10 6:54 
GeneralRe: System.ObjectDisposedException: Cannot access a disposed object. Pin
Jacob Dixon24-May-10 7:13
Jacob Dixon24-May-10 7:13 
GeneralRe: System.ObjectDisposedException: Cannot access a disposed object. Pin
Luc Pattyn24-May-10 7:27
sitebuilderLuc Pattyn24-May-10 7:27 
Questiontype object Pin
tek 200923-May-10 10:32
tek 200923-May-10 10:32 
AnswerRe: type object Pin
Pete O'Hanlon23-May-10 10:46
mvePete O'Hanlon23-May-10 10:46 
AnswerRe: type object Pin
tek 200923-May-10 12:28
tek 200923-May-10 12:28 
QuestionOutput type of Class Library cannot be started directly Pin
tan_chin23-May-10 9:33
tan_chin23-May-10 9:33 
AnswerRe: Output type of Class Library cannot be started directly Pin
#realJSOP23-May-10 9:58
professional#realJSOP23-May-10 9:58 
GeneralRe: Output type of Class Library cannot be started directly Pin
tan_chin23-May-10 10:28
tan_chin23-May-10 10:28 
GeneralRe: Output type of Class Library cannot be started directly Pin
Dave Kreskowiak23-May-10 18:26
mveDave Kreskowiak23-May-10 18:26 
AnswerRe: Output type of Class Library cannot be started directly Pin
Pete O'Hanlon23-May-10 10:00
mvePete O'Hanlon23-May-10 10:00 
GeneralRe: Output type of Class Library cannot be started directly Pin
tan_chin23-May-10 10:29
tan_chin23-May-10 10:29 
QuestionExcel number of rows Pin
gmhanna23-May-10 7:37
gmhanna23-May-10 7:37 
AnswerRe: Excel number of rows Pin
Abhinav S23-May-10 7:42
Abhinav S23-May-10 7:42 
QuestionArraylist Goes Null after Passing information back to Main Page from Window. Pin
PDTUM23-May-10 7:23
PDTUM23-May-10 7:23 
AnswerRe: Arraylist Goes Null after Passing information back to Main Page from Window. Pin
Abhinav S23-May-10 7:38
Abhinav S23-May-10 7:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.