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

C#

 
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 
Hi Jacob,

I think you handled the stopping and disposing the wrong way. For one, you should not implement a destructor; normally one implements a Dispose() method that disposes first by providing and calling a Dispose(bool) method, then calls SuppressFinalize. Your Dispose() not calling Dispose(bool) at all, I think your destructor is unreachable. On top of that I see no need for all of it.

This is what I would do:

D1. remove "IDisposable" and all dispose related code from LoadPicture. There is no need for LoadPicture to be disposable, it is not holding on to lots of memory that otherwise may get lost.

D2. implement a better Stop method; all you need is for LoadPicture to really have stopped somehow before you close the Form, and make the Progress/Finished events point to something that does no longer exist.

public void Stop() {
    stop = true;
    ProgressChanged = null;
    Finished = null;
}


This is not waterproof yet, there is a slim chance of Load() to have tested say ProgressChanged (turned out to be not null), the main thread starting to close the form, setting ProgressChanged null, stop true, actually then closing the form, and only then Load calling ProgressChanged.

So you could:
- make stop volatile
- and test both the event and stop in that order (opposite from the Stop order), hence:

if (ProgressChanged != null && !stop) ProgressChanged(dataIndex, ...)


That should take care of it.

I do have more comments:

C1.
you used a strange way to cope with the end of the stream. I would simply use a loop containing
bytesRead += r.GetBytes(0, dataIndex, buffer, dataIndex, bufferSize);
dataIndex += bytesRead;

and exit the loop when bytesRead turns zero.

C2.
your progress reporting seems expensive, in two ways:
a. you switch from int/long to decimal, to cope with small sizes, which would vanish when divided by 1024 or 1024*1024.
b. and you report very frequently, so consecutive reports will not differ by much, probably wasting a lot of CPU cycles, and actually slowing down progress significantly.

I would use a larger data buffer (say 64KB), calculate an integer percentage locally, compare it to the previous value, and only report it on a change of percentage value.

C3.
You don't want your thread (t4) to ever prevent your app from exiting, so it is a background thread. You should tell it so: t4.IsBackground=true; before t4.Start();

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 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 
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 

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.