Click here to Skip to main content
15,919,879 members
Home / Discussions / C#
   

C#

 
GeneralRe: Using Handler to Track Progress Pin
EvanSaunders23-May-10 22:57
EvanSaunders23-May-10 22:57 
GeneralRe: Using Handler to Track Progress Pin
Łukasz Nowakowski23-May-10 22:59
Łukasz Nowakowski23-May-10 22:59 
QuestionMicrosoft Surface Pin
Łukasz Nowakowski23-May-10 20:59
Łukasz Nowakowski23-May-10 20:59 
QuestionGraphs in C# - Windows application Pin
KaurGurpreet23-May-10 20:46
KaurGurpreet23-May-10 20:46 
AnswerRe: Graphs in C# - Windows application Pin
Abhinav S23-May-10 20:50
Abhinav S23-May-10 20:50 
AnswerRe: Graphs in C# - Windows application Pin
Gaurav Dudeja India24-May-10 1:04
Gaurav Dudeja India24-May-10 1:04 
AnswerRe: Graphs in C# - Windows application Pin
Ravi Bhavnani24-May-10 14:42
professionalRavi Bhavnani24-May-10 14:42 
Questiongeting error message "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." for code given below its aaper after end of function Pin
MS_TJ23-May-10 20:31
MS_TJ23-May-10 20:31 
AnswerRe: geting error message "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." for code given below its aaper after end of function Pin
Dave Kreskowiak24-May-10 4:30
mveDave Kreskowiak24-May-10 4:30 
AnswerRe: geting error message "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." for code given below its aaper after end of function Pin
Bernhard Hiller25-May-10 3:58
Bernhard Hiller25-May-10 3:58 
GeneralRe: geting error message "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." for code given below its aaper after end of function Pin
MS_TJ25-May-10 6:43
MS_TJ25-May-10 6:43 
QuestionCan not open 'User Management' in the request database. Login failed. Pin
Make Up Forever23-May-10 20:03
Make Up Forever23-May-10 20:03 
AnswerRe: Can not open 'User Management' in the request database. Login failed. Pin
Mycroft Holmes23-May-10 23:05
professionalMycroft Holmes23-May-10 23:05 
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 

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.