I had an issue before ,where OnTimer () function was not working and I started using thread which is working fine
as of now
http://www.experts-exchang
What will be your recommendations?
Main Topics
Browse All TopicsI have a Dialog box under the VC++ Client/Server (unmanaged code) application which contain two list box..One set of listbox1 data is filled from database and one set set of listbox2 from webservice...Both of them are called when I click on Search button after giving the necessary inputs in the dialog box ..The functions used to populate from database are below
--------------------------
PrepareForSearch();
DoSearch();
"DisplayResult();
--------------------------
after that I am calling Web service call via create thread...
The issue is if I select the data in the listbox( which is already filled by database) without waiting for 5 seconds for the Web serivce call to finish..the code just hangs..
The webservice data normally comes back in 3-4 seconds so if on that period someone clicks on listbox1..the program crashes..
what would be ideal solution in the scenario?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I had an issue before ,where OnTimer () function was not working and I started using thread which is working fine
as of now
http://www.experts-exchang
What will be your recommendations?
I recommend avoiding the use of a secondary thread. See the article.
If you must use a thread (and you probably don't need to here) you must avoid accessing U/I elements such as list controls while in the secondary thread. You need to set up a convoluted sequence of posting messages (or other technique) so the main thread can handle that.
I've just realised
pWnd->VFSWEbService
Tha
If my guess is correct then you need to use a list (array, some other container) to fill with the items, then when the thread finishes the main app can fill the listbox from the intermediate collection.
Actually my problem of crash is different...but I understand that nothing should be UI related in the worker thread...
I will try to be clear..
I have two list boxes on a dialog..which gets called when I click on Button search...
1. List1 fills very quickly from database.
2. List2 takes time around 3-4 seconds..(Webservice)
Now problem is if I click on List1 data lets say in 1 seconds without even allowing List2 to get filled ...the application crashes..
since the dialog box has to close, and list2 data is not populated and windows didn't get the message that web service thread is complete..the application crash...
I don't know if this is possible or not, to get the flag across application...I tried but didn't work
bool flag = pWnd->PostMessage(WM_WEBSER
or May be it is only putting bandage on much bigger problem which can come in later time as referred in the tutorials provided by Dan...
Please let me know your views...
The problem is that MFC is not thread safe. Directly accessing one MFC based window from a different thread (as you do) can result in crashes. I don't know the precise details but some things are handled on a thread specific basis, accesing directly from another thread means the information that MFC uses for the window to work is incorrect.
Just give this a quick try. Comment out the two lines in your thread starting with pWnd-> and replace with some dummy code eg. sleep(5000) - see if your app still crashes.
ps. If you code a thread properly then it will work without problems (same as coding an app really - do something wrong and it can crash).
>> Now problem is if I click on List1... the application crashes.
Indeed that is a common problem when secondary threads interact with the U/I in MFC (and actually most kinds of) programs. The specific reasons for the problem are hard to describe, but the symptoms are easy to recognize :-)
If you must use a secondary thread, then do as AndyAinscow suggested: Have it populate, say, a CStringArray or other in-memory array. When it is done, have it set a flag that the U/I handler (your dialog) can see. Then have a Window Timer check that flag and when the flag is set, populate the entire listbox from the CStringArray (and kill the timer).
>>>> I commented this code and application does not crash..
>>>> //pWnd->VFSWEbServiceCall()
In the pWnd->VFSWEbServiceCall(); instead of filling the listbox put the entries to a vector<CString>
void MyCtrl::VFSWEbServiceCall(
{
// your code until filling of the listctrl
...
// filling the listctrl
std::vector<CString> * ptrArr = new std::vector<CString>;
for (int i = 0; i < numEntriesOfListBox; ++i)
{
ptrArr->push_back(StringsTo
}
// send message to dialog which is the parent of the listbox control
GetParent(m_hWnd)->PostMess
(LPARAM)ptrArr,
(WPARAM)numEntriesOfListBo
}
// yourdialog.cpp
BEGIN_MESSAGE_MAP(YourDial
{
// existing message handlers
....
// message handler to fill the listbox from vector
ON_MESSAGE(WM_FILL_MY_LIST
}
void YourDialog::OnFillListBox(
{
m_lstBox.ResetContent();
std::vector<CString> * ptrArr = (std::vector<CString> *)lp;
for (int i; i < (int)ptrArr->size(); ++i)
{
m_lstBox.AddString((*ptrAr
}
delete ptrArr;
}
That way you would move the filling from worker thread to main thread.
I am trying to use CStringArray for filling the list control so I have 5 CStringarray for the five column values..
How do I pass the flag to dialog so that the listctrl gets filled on OnFillListbox ?
I tried using GetParent()->PostMessage(WM
function
--------------------------
For (i =0;i < 3;i++)
{
arrProgram.Add();
arrProgram.Setat(i, name);
.......................
..........................
}
GetParent()->PostMessage(WM
}
It looks like you are doing it right... however there is one thing you could be getting wrong:
Are you sure that GetParent() will return the dialog that contains the message handler?
In the example, the source code beginning on line 23 is rather anonymous -- we can't tell what object it belongs to. Since we have no information, it is impossible to say whether GetParent() will return the CDlgFinCoverageSearch object.
<pre id="codeSnippet439369" class="prettyprint">if the folllowing line is in the thread function after the values have been retrieved from the web service
GetParent()->PostMes
t
PostMessage(hWndOwner, WM_FILL_MY_LISTBOX,0,0);
wh
Business Accounts
Answer for Membership
by: AndyAinscowPosted on 2009-10-14 at 08:32:36ID: 25571724
This might not be relevant. VICEDONE,0 ,0);
t....) instead
MFC is not thread safe so
CDlgFinCoverageSearch* pWnd = (CDlgFinCoverageSearch*) pv;
pWnd->VFSWEbServiceCall();
pWnd->PostMessage(WM_WEBSER
is not especially good - it might crash or it might work without problems depending on luck.
It is better to pass the window handle (GetSafeHwnd()) and use ::PostMessage(hWndRecipien