How to maintain 'scope' when using unmanaged queue code within a .Net Form Application ?
Posted on 2006-07-14
I have a .Net Form Application which has one main thread created within the Form1_Load() :
private: System::Void Form1_Load(System::Object * sender, System::EventArgs * e)
PanelThread = new Thread(new ThreadStart(this, MainPanelThreadProc));
When this thread runs it polls five different sockets of unmanged code (or C/C++ files) that have been included into this project from projects of yester-year.....
It successfully polls these sockets, and the data from these sockets is transferred back to Form1 via global extern 'string' arrays. The data is then displayed to the screen. Hence, I know I have no problem collecting data.
I wanted to add one more unmanaged 'C' file which contains queue code responsible for collecting various elements from each socket. I didn't want to have to learn the .Net queue functions given time, although it may have been the better way to go. The queue has no problem displaying five icons on the Form when called by MainPanelThreadProc() during each iteration:
// global declaration
// declared as void SetPictureStatus(...)
SetPictureStatus(...); // Loads queue objects & displays pictures based on queue line up i.e. 1st, 2nd,
if(Group1_SocketFlag == 1) // collects data and would update the head queue object if possible !!
GroupOneData(); // declared as void Form1::GroupOneData()
if(Group2_SocketFlag == 1) // collects data and would update the head queue object if possible !!
ReadGroupTwoData() // declared as void Form1::GroupTwoData()
if(Group3_SocketFlag == 1) // collects data and would update the head queue object if possible !!
GroupThreeData() // declared as void Form1::GroupTwoData()
SetPictureStatus() successfully loads the queue with five objects and initializes LineUp. To keep things simple, it looks something like this:
QueueIndex = (FormAppQueue.head + 1) % 6; // head is one greater than actual head
QObject = FormAppQueue.elements[QueueIndex]; // get head
QObject.LineUp = LineUp; // tracks total queue objects
All I want to do for the moment is prove one simple test which is to read GroupOneData, deposite one 'member' into the queue head, and remove one 'object' from the queue based on that deposit.
However, when I step through the debugger the program reveals that the data is being read and deposited, but when I circle back to SetPictureStatus() and perform a check on the 'member' deposited from GroupOneData, the data isn't there ! I can cleary identify the head object by using the LineUp number. In other words, the queue objects created by SetPictureStatus() maintain their existence and LineUp status but any other assigned data is not really being assigned.
All the line-up numbers are still in place but not _one_ of the queue objects has the updated member value which was clearly assigned during runtime. I set a watch on QHead within both functions, and observe that both values reside while inside GroupOneData(). Once the program goes on to the other functions and comes back around to SetPictureStatus(), the only data that remains is the original LineUp data from the first call to load.
I'm guessing this problem revloves around the MainPanelThread and the scope it will allow for unmanaged code. Keep in mind that SetPictureStatus() is able to call the global declared Queue and initialize it with 'LineUp.' Why then can't any other function have the same success ?
.... I can provide a scaled down project which would be Form1.h, Form1.cpp and queue.cpp if necessary.