// JP opened flex table

Click to See Complete Forum and Search --> : Thread quandry with my design...Please help.


Rob McVicar
April 1st, 2004, 04:39 PM
Hey all,

I have a little question which may be more of a coding-style question, but here it goes.

I have a dialog-based app which programs a remote system serially. This program (originally) on startup commences to download all of the remote systems data to the applications database. This took waaaay too long and was giving the users headaches, but at the time I had little in the way of requirements telling me what an acceptable time to download was.

Anyhow, while the download of data was taking place, the user (in single threaded mode) could not click any of the myriad of buttons on the dialog to access varoius data property sheets.

We (the bosses really) now want the GUI active while downloading the data. OK go multithreaded with this, not a problem... create a worker thread to download the data while the GUI remains alive in a GUI thread (main thread).

Since this program has many many many property sheets to access and the user can now theoretically do this before (or while) the data is downloaded, which will cause great grief, how might be the best way to go about this???

I have the data now downloaded in groups/propsheet, so what I was planning was to have the worker thread go through and download each property sheet's data and set a flag indicating that accessing the propsheet is now OK, but if I try to access this page when the data is not there, I display a splash screen telling the user to hang on the data will be downloaded right away. Once the worker thread does this download, how would I have the worker thread tell the GUI thread to display this page??? I tried a couple of ways and they all crash the program.

The other way I tried was to do a worker thread for each program group. This really did not work, but maybe someone has an idea on this one.

If anyone could give me some hints I'd really appreciate it!:D

Thanks!!!

Rob

Andreas Masur
April 1st, 2004, 04:46 PM
[Moved thread]

Sam Hobbs
April 2nd, 2004, 08:16 PM
Originally posted by Rob McVicar
Since this program has many many many property sheets to access and the user can now theoretically do this before (or while) the data is downloadedIs the user supposed to be able to do that, or are you saying they can eventhough they are not supposed to be able to do that?
Originally posted by Rob McVicar
I have the data now downloaded in groups/propsheet, so what I was planning was to have the worker thread go through and download each property sheet's data and set a flag indicating that accessing the propsheet is now OK, but if I try to access this page when the data is not there, I display a splash screen telling the user to hang on the data will be downloaded right away.I doubt you realize how confused I am. I don't know the relationship of the groups, property sheets, threads, remote system(s), users and perhaps much else. I don't understand the events either; that is, you say that a property sheet is downloaded, then it can be accessed. However you seem to also be saying that the program is doing the download, so this is confusing. Also, I don't understand why the data would not be there. So perhaps you are describing two programs as if they are one; one program at either end.

Rob McVicar
April 5th, 2004, 09:00 AM
Hey Sam,

Sorry for the confusion. This program has gotten that way since it was first developed (management forethought & indecision).

Here is how it goes:

1) The main program connects remotely to a phone system via serial link.

2) The program then starts to download all of the data from the phone system to my program. Initially this was a single threaded design because the user waiting a few minutes for the data to complete a download was no big deal, and we had to get this out quickly.

3) Once the download is complete (the old way), the user may go to any of the system data programs and edit the data. Each of the property sheets/pages/buttons contain different sections and types of data for the system. These are broken down into what we call "programs". i.e program 0101 progams all class of service data for the telephone system.

4) Once the data is edited, the user clicks the Send button to send the data back to the system.

Now while all of this is OK for the business sector, some people here complained that the gui isnot live during the upload & download process. That is where the multithreaded idea came to mind. So...now I've been only getting half said (nothing written) ideas on how this should work. It started as follows....

1) While the download is taking place (using a worker thread), if the user tries to access a program which has not been downloaded yet, finish downloading the current program and go straight to the once requested by the button click

2) In the meantime, display a bitmap property sheet stating "Please Wait...", then once the data for the program requested is finished downloading, display the propertysheet for that data program.

3) Once this is done, resume downloading the rest of the data not yet received from the system.

4) BUT! If the user requests to send newly changed data to the system, halt the downloading again and send the data changes first.

My original question was also directed at how the best way to construct this via several threads, and also how would I have the worker thread communicate to the main thread that it was done receiving the data, so that I may display the propertysheet containing that received data. This is my main conundrum.

Any design tips and recommendations you could give me on the interthread relations would be greatly appreciated.

Sorry for the confusion, if I had a spec to work from I could have taken excerpts from that.:rolleyes:

Thanks again!

Rob

Sam Hobbs
April 5th, 2004, 11:12 PM
There are many ways to solve this problem. If I suggest a solution then probably someone else will have a better one, but since you are not getting help, I will suggest something.

If I understand what you are saying, then the worker thread probably does not need to do anything to notify the main thread that it is complete, except to simply complete. The main thread can determine that a worker thread has competed without the worker thread doing anything except completing.

How the main thread does that can be done in many ways, but one possibility is for it to do that in a UI update function. I assume that the buttons and such are (or could be) commands processed in the mainframe. The UI update functions for them can use WaitForSingleObject or WaitForMultipleObjects to determine if a thread has completed. The mainframe could also update the document which could update the view as needed.

Rob McVicar
April 6th, 2004, 09:03 AM
Sam,

Thanks very much! That is pretty much the conclusion I came to as well, kind of. What I was going to do was this...

If the user clicks one of the buttons (which is controlled via the main GUI thread), suspend the current downloading thread using SuspendThread, this is convienient since the way the "Programs" are downloaded from the phone system they are very modular and break-points are readily available throughout the download. So if I need to access other data right away, I can suspend the current download, start another thread to download the immediately required data, and use the waitforsingleobject() on this, update the GUI, and then resume the other thread.

If you could, please tell me a little more on the WaitForSingleObject() function.

Also, the SuspendThread() function should be called like this...
1) thread_ptr->SuspendThread()
or this
2) SuspendThread(thread_ptr)

Thanks Again for your help.

Sam Hobbs
April 6th, 2004, 09:55 AM
Originally posted by Rob McVicar
If you could, please tell me a little more on the WaitForSingleObject() function.

Also, the SuspendThread() function should be called like this...
1) thread_ptr->SuspendThread()
or this
2) SuspendThread(thread_ptr)How SuspendThread is called depends on whether you are calling CWinThread::SuspendThread or ::SuspendThread.

I think the CodeGuru FAQ and my web site both have sample usees of WaitForSingleObject.

Rob McVicar
April 6th, 2004, 10:04 AM
Sam,

I'll check your web site and codeguru for those items. I just got sidetracked onto something else, but I'll see those spots in the next day or so. If I have any further questions I'll reply to this post again.


Thanks again for all of your help!!!

Rob McV

//JP added flex table