Click here to Skip to main content
15,901,122 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
QuestionInternals of GC! Pin
Deepasubramanian24-May-06 21:50
Deepasubramanian24-May-06 21:50 
AnswerRe: Internals of GC! Pin
Vasudevan Deepak Kumar24-May-06 22:42
Vasudevan Deepak Kumar24-May-06 22:42 
QuestionDataGridView ColumnHeaders Pin
NRavep24-May-06 20:53
NRavep24-May-06 20:53 
QuestionHow does make web service's pull? Pin
Libra24-May-06 16:26
Libra24-May-06 16:26 
AnswerRe: How does make web service's pull? [modified] Pin
Igor Sukhov28-May-06 19:17
Igor Sukhov28-May-06 19:17 
Questionhow do I force loading of .NET 1.1 Pin
mheschl24-May-06 11:16
professionalmheschl24-May-06 11:16 
AnswerRe: how do I force loading of .NET 1.1 Pin
Ed.Poore25-May-06 21:49
Ed.Poore25-May-06 21:49 
QuestionError when Painting (long message) [modified] Pin
ricecake24-May-06 8:54
ricecake24-May-06 8:54 
I have created a Form that has a PictureBox in it used for displaying multiple images. The constructor for the Form receives an array of filenames. The Form will then display the first image, Sleep() for a user-specified interval, then show the second image, Sleep(), etc.

This all works find and dandy in the normal case. However, if I am dragging another window and it overlaps the Form while trying to change the image, I get an InvalidOperationException telling me that "the object is in use elsewhere", but it won't tell me which object, and then the area where the picture is supposed to be turns into a big red X.

The entire class is about 550 lines so I won't post it here, but here are some relevant code snippets:

[Everything looks OK in the preview but it looks like it eats my whitespace in the <pre> tags when it actually gets posted]

System::Void initialize_thread()
{
    using System::Threading::Thread;
    using System::Threading::ThreadStart;

    draw_images_thread = new Thread(new ThreadStart(this, &PlotDisplayWindow::draw_images));
    draw_images_thread->IsBackground = true;
}
System::Void draw_image(int image_num) {
    picturebox->Image = __try_cast<System::Drawing::Image*>(images_[image_num]);
    Text = filenames_[image_num];
    graphics = this->CreateGraphics();
    graphics->DrawImage(images_[image_num],
                        AutoScrollPosition.X, AutoScrollPosition.Y,
                        images_[image_num]->Width, images_[image_num]->Height);
    graphics->Dispose();
}
__delegate System::Void DrawImageDelegate(int image_num);

System::Void draw_images()
{
    //using System::IAsyncResult;
    using System::Threading::Thread;

    while (running) {
        if (picturebox->InvokeRequired) {
            DrawImageDelegate* draw_image_delegate = new DrawImageDelegate(this, &PlotDisplayWindow::draw_image);
            draw_image_delegate->Invoke(image_number);
            //IAsyncResult* async_result = draw_image_delegate->BeginInvoke(image_number, 0, 0);
            //draw_image_delegate->EndInvoke(async_result);
        }
        else {
            draw_image(image_number);
        }

        Thread::Sleep(sleep_time_ms);

        ++image_number;
        if (image_number == filenames_->Length) {
            image_number = 0;
        }
    }
}
System::Void PlotDisplayWindow_Paint(System::Object* /*sender*/, System::Windows::Forms::PaintEventArgs* e)
{
    using System::Threading::ThreadState;

    if (filenames_->Length > 1) {
        if (draw_images_thread->ThreadState & ThreadState::Suspended) {
            draw_images_thread->Resume();
        }
        else if (draw_images_thread->ThreadState & (ThreadState::Unstarted | ThreadState::Stopped)) {
            initialize_thread();
            draw_images_thread->Start();
        }
    }
    else {
        draw_image(0);
    }
}

Here is the text of the exception box that comes up:
************** Exception Text **************
System.InvalidOperationException: The object is currently in use elsewhere.
   at System.Drawing.Image.get_Width()
   at System.Drawing.Image.get_Size()
   at System.Windows.Forms.PictureBox.get_ImageRectangle()
   at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 1.0.5000.0
    Win32 Version: 1.1.4322.2032
    CodeBase: file:///c:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll
----------------------------------------
driver
    Assembly Version: 0.0.0.0
    Win32 Version: 
    CodeBase: file:///C:/Documents%20and%20Settings/kwokmb1/My%20Documents/Work/code/sample_code/PlotDisplay/driver.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 1.0.5000.0
    Win32 Version: 1.1.4322.2032
    CodeBase: file:///c:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
    Assembly Version: 1.0.5000.0
    Win32 Version: 1.1.4322.2032
    CodeBase: file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll
----------------------------------------
System.Drawing
    Assembly Version: 1.0.5000.0
    Win32 Version: 1.1.4322.2032
    CodeBase: file:///c:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll
----------------------------------------

Can anybody give any advice? I can email a sample project to somebody if they want a closer look.

--
Marcus Kwok

-- modified at 16:07 Wednesday 24th May, 2006
AnswerRe: Error when Painting (long message) Pin
Robert Rohde24-May-06 9:44
Robert Rohde24-May-06 9:44 
QuestionRe: Error when Painting (long message) Pin
ricecake24-May-06 10:06
ricecake24-May-06 10:06 
AnswerRe: Error when Painting (long message) Pin
ricecake24-May-06 10:12
ricecake24-May-06 10:12 
AnswerRe: Error when Painting (long message) Pin
Robert Rohde24-May-06 11:27
Robert Rohde24-May-06 11:27 
AnswerRe: Error when Painting (long message) Pin
ricecake25-May-06 10:55
ricecake25-May-06 10:55 
GeneralRe: Error when Painting (long message) Pin
Robert Rohde25-May-06 11:18
Robert Rohde25-May-06 11:18 
GeneralRe: Error when Painting (long message) Pin
ricecake25-May-06 11:26
ricecake25-May-06 11:26 
QuestionItem collection in custom control Pin
Alexander Pokisluk24-May-06 0:48
Alexander Pokisluk24-May-06 0:48 
QuestionOut of Memory Exception with .NET Interop Pin
Rob Philpott23-May-06 22:28
Rob Philpott23-May-06 22:28 
Questionwindows form sizing Pin
reshsilk23-May-06 8:27
reshsilk23-May-06 8:27 
AnswerRe: windows form sizing Pin
arcticbrew23-May-06 11:52
arcticbrew23-May-06 11:52 
AnswerRe: windows form sizing Pin
Alexander Pokisluk24-May-06 1:40
Alexander Pokisluk24-May-06 1:40 
QuestionSelect text in TextBox when Form loads Pin
ricecake23-May-06 6:56
ricecake23-May-06 6:56 
AnswerRe: Select text in TextBox when Form loads Pin
ricecake23-May-06 7:42
ricecake23-May-06 7:42 
QuestionFinalize() Method [modified] Pin
Tristan Rhodes23-May-06 3:09
Tristan Rhodes23-May-06 3:09 
AnswerRe: Finalize() Method [modified] Pin
Robert Rohde23-May-06 3:34
Robert Rohde23-May-06 3:34 
GeneralRe: Finalize() Method [modified] Pin
Tristan Rhodes23-May-06 4:23
Tristan Rhodes23-May-06 4:23 

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.