Unhandled Exception runtime error

Forum - I have a small WinForms app that interacts over the USB with a microcontroller which digitizes an applied anaolg voltage and plots the points in a pictureBox control. The operational code and some GUI control code are in a DoLoop in a thread set up and called by the Run_btn_Click event handler. The code is C++ Windows::Forms being developed under Visual C++ Windows::Forms 2008.

The Run_btn_Click handler code looks like this:

1
2
3
4
5
private: System::Void Run_btn_Click(System::Object^  sender, System::EventArgs^  e) {
			 Thread^ thread = gcnew Thread(gcnew ThreadStart(this, &Form1::DoLoop));
			 thread->IsBackground = true;
			 thread->Start();
	} // End of Run_btn_Click 


All app code compiles; it all runs fine except for the printDocument1_PrintPage handler (which is called by the printToolStripMenuItem_Click handler) ; this PrintPage handler throws an Unhandled Exception error when the Print button in the printDialog window (associated with the printToolStripMenuItem_Click event) is clicked.

The Details section of the error window reads (in part):

************** Exception Text **************
System.ArgumentNullException: Value cannot be null.
Parameter name: image
at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y)
at SimpleWinUSBDemo.Form1.printDocument1_PrintPage(Object sender, PrintPageEventArgs e) in c:\program files (x86)\microsoft visual studio 9.0\projects\loggeri2r4\form1.h:line 990
----------------------------<other lines>-------------------------------
at System.Drawing.Printing.PrintDocument.Print()
at SimpleWinUSBDemo.Form1.printToolStripMenuItem_Click(Object sender, EventArgs e) in c:\program files (x86)\microsoft visual studio 9.0\projects\loggeri2r4\form1.h:line 984
---------------------------<plus other lines>--------------------------

The printToolStripMenuItem_Click and printDocument1_PrintPage handlers with problem lines marked are shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
private: System::Void printToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 PrintDocument^ printDocument1 = gcnew PrintDocument;
			 printDocument1->PrintPage += gcnew PrintPageEventHandler(this, &SimpleWinUSBDemo::Form1::printDocument1_PrintPage);
			 if(printDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
				{
				   printDocument1->Print();  // Line 984 Unhandled Exception
				}
		 } // End of printToolStripMenuItem_Click

private: System::Void printDocument1_PrintPage(System::Object^  sender, System::Drawing::Printing::PrintPageEventArgs^  e)
		 {
		    e->Graphics->DrawImage(pictureBox1->Image,0,0);  // Line 990 Unhandled Exception
		 } // End of  _PrintPage 


The above handlers work fine in a single-thread version of this code.

Assuming the above means I am not extracting the pictureBox Image into the PrintPage routine, I've tried for over a week to adapt examples of invokes, delegates, etc. without success and I am simply not getting the idea and could really use a bit of help. Also, if my fundamental approach to extracting and printing that image is not correct I'd very much like to be corrected in that direction with what is the correct (or a better) way to do it. Thanks in advance for any assistance the Forum might care to offer me.

MickH
The problem is that the gui is not threadsafe. You need to be very carefull when you access data from another thread.
coder777 - thanks for the reply. I do understand that care is needed in cross-thread data acquisition. My basic problem is that I don't know how to do it. I have no experience at all with cross-thread operations. I would much appreciate some example code that would fit my situation or a reference to some. Thanks again.

MickH
25 June20
1:30 PM PDT USA
I couldn't find any examples for C++, only for C#. Maybe it helps to understand the concepts.

https://www.codeproject.com/Articles/17688/Updating-the-GUI-from-Another-Thread-Made-Easy
https://docs.microsoft.com/en-us/archive/msdn-magazine/2003/february/give-net-apps-a-fast-and-responsive-ui-with-multiple-threads

Another way to go might be the BackgroundWorker class.
Last edited on
Thomas1965 - thank you for the reply. I will study the two references carefully. Maybe C++/CLI example code doesn't exist.

Another way to go might be the BackgroundWorker class.


Truth-in-advertising: I have a working version of this app using BackgroundWorker; I just want to see what it would take to do it using basic multithreading.

I'll probably try one more time by re-posting a clarified version of the original question.

Thanks again for your help.

MickH
26 June 20
11:09 AM PDT USA
Registered users can post here. Sign in or register to post.