Click here to Skip to main content
15,889,909 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
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 
I'm sorry I was nervous about putting taht much code in the first post. I know its not recommended..

Ok the LoadPicture Class:

class LoadPicture : IDisposable
{
    public delegate void FinishedDelegate(Image img);
    public delegate void ProgressDelegate(int current, int max, string c, string t);
    public event FinishedDelegate Finished;
    public event ProgressDelegate ProgressChanged;

    string Tag;
    bool stop;

    public LoadPicture(string Tag)
    {
        this.Tag = Tag;
        stop = false;
    }

    ~LoadPicture()
    {
        ProgressChanged = null;
        Finished = null;
    }

    public void Load()
    {
        SqlConnection conn = new SqlConnection(AssetConfig.ConnectionString);
        SqlCommand cmd = new SqlCommand("SELECT Picture FROM Inventory_Pictures WHERE PictureID=@Tag", conn);
        cmd.Parameters.AddWithValue("@Tag", Tag);

        try
        {
            conn.Open();

            // HERE I AM WORKING ON A BETTER WAY OF TELLING IF IDataReader DOES NOT CONTAIN DATA...
            // HAVEN'T FIGURED IT OUT YET BUT THIS IS JUST TEMP
            SqlDataReader tmp = cmd.ExecuteReader();
            if (tmp.HasRows)
            {
                tmp.Close();

                IDataReader r = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
                if (r != null)
                {
                    r.Read();

                    long size = r.GetBytes(0, 0, null, 0, 0);
                    byte[] buffer = new byte[size];
                    int bufferSize = 1024;
                    int dataIndex = 0;
                    long bytesRead = 0;

                    while ((bytesRead < size) && ((size - bytesRead) > 1024))
                    {
                        if (stop)
                            break;

                        bytesRead += r.GetBytes(0, dataIndex, buffer, dataIndex, bufferSize);
                        dataIndex += 1024;

                        decimal FileSize = (Convert.ToDecimal(size) / 1024) / 1024;
                        decimal CurrentSize = (Convert.ToDecimal(bytesRead) / 1024) / 1024;

                        // YOU NOTICE HERE I CHECK FOR IT BEING NULL BEFORE CALLING IT (So why is it trying to call it
                        // after the frmViewAsset is disposed since frmViewAsset should be disposing of it before closing?
                        if (ProgressChanged != null)
                            ProgressChanged(dataIndex, Convert.ToInt32(size), Math.Round(CurrentSize, 2).ToString(), Math.Round(FileSize, 2).ToString());
                    }

                    if (!stop)
                    {
                        bytesRead += r.GetBytes(0, dataIndex, buffer, dataIndex, Convert.ToInt32((size - bytesRead)));

                        MemoryStream ms = new MemoryStream(buffer);
                        if (Finished != null)
                            Finished(Image.FromStream(ms));

                        ms.Close();
                    }
                }
                r.Close();
            }
        }
        catch (Exception ex)
        {
            Error.WriteError(ErrorType.SQL, "LoadPicture", ex.ToString());
        }
        finally
        {
            if (conn != null)
            {
                conn.Close();
                conn.Dispose();
            }

            if (cmd != null)
                cmd.Dispose();
        }
    }

    public void Stop()
    {
        stop = true;
    }

    public void Dispose()
    {
        System.GC.SuppressFinalize(true);
    }
}


Here is frmViewAsset (some of it) and you notice how it initializes the events:
LoadPicture lp;
Thread t4;
private void frmViewAsset_Load(object sender, EventArgs e)
{
    lp = new LoadPicture(AssetID);
    lp.ProgressChanged += new LoadPicture.ProgressDelegate(p_ProgressChanged);
    lp.Finished += new LoadPicture.FinishedDelegate(p_Finished);

    t4 = new Thread(new ThreadStart(PopulatePicture));
    t4.Start();
}
private void PopulatePicture()
{
    lp.Load();
}
private void p_ProgressChanged(int current, int max, string c, string t)
{
    if (!this.IsDisposed)
    {
        Invoke((Action)(() => // <-- THIS IS LINE 388
        {
            pbPicture.Maximum = max;
            pbPicture.Value = current;

            lbPicProgress.Text = string.Format("{0}MB / {1}MB", c, t);
        }));
    }
}
private void p_Finished(Image img)
{
    Invoke((Action)(() =>
    {
        picAsset.Image = img;
    }));
}
private void frmViewAsset_FormClosing(object sender, FormClosingEventArgs e)
{
    lp.Stop();
    lp.Dispose();
}


You will see on the frmViewAsset I am calling lp.Stop(); which should be stopping the datareader from reading any further (breaking out of the while statement in the LoadPicture class). You also notice I call lp.Dispose() which should be setting both PRogressChanged and Finished events to null

I'm assuming that LoadPicture is having a hard time disposing before frmViewAsset can dispose (hints why it is still called). But why?!?!

FYI I have also tried this on form closing:
lp.ProgressChanged -= new LoadPicture.ProgressDelegate(p_ProgressChanged);
lp.Finished -= new LoadPicture.FinishedDelegate(p_Finished);




The ultimate goal is to load the picture by every 1024 bytes which allows me to display a progress bar and label showing 1.0MB out of 2.0MB (that kind of thing). All this works except if the user closing this form before it has finished loading the picture (which I need it to safely dipose / stop loading it without the user noticing). Right now the user won't notice but thats because on error it writes the Error to the error log. That doesn't work for me Smile | :) ... I like to try to do things the correct way lol

** EDIT **
Don't know if it will make a difference but I neglected to mention that the PROGRESSBAR and the LABEL are both in the status bar. They are not labels directly on the form.
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 
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
mve#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 

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.