|
Hello,
I am developing an MFC dialog which registers a callback with a certain library. This library will call back at intervals giving new insformation to the dialog. This information should be displayed on the dialog's controls.
The callback comes from a different thread than the thread of my dialog, so my question is:
What method should I use to update my dialog? Should I send messages to the main thread and it do the work, or is it OK to update my controls from within the callback?
I appreciate your input.
Jeremy Pullicino
C++ Developer
Homepage
|
|
|
|
|
Jeremy Pullicino wrote:
Should I send messages to the main thread and it do the work, or is it OK to update my controls from within the callback?
It's perfectly OK to update your controls from a second thread. However, you can't use MFC to do it. You have to use the straight Win32 API to do it. Luckily, updating controls generally uses MFC wrappers that are fairly thin wrappers over the API, so it's not much more work.
Sending messages to the main thread will work just as well, and will allow you to use MFC.
Basically, you can do what you like, depending on whether you want to be able to use MFC or not.
Hope this helps,
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Ryan Binns wrote:
It's perfectly OK to update your controls from a second thread. However, you can't use MFC to do it. You have to use the straight Win32 API to do it.
Thanks for the feedback.
Just out of curiosity - why can't I use MFC to update my dialog from the second thread?
Jeremy Pullicino
C++ Developer
Homepage
|
|
|
|
|
Jeremy Pullicino wrote:
why can't I use MFC to update my dialog from the second thread?
MFC uses thread-specific data internally. Using MFC objects created in one thread in another thread uses inconsistent internal data, and can cause catastrophic errors, hence MFC incorporates checks to prevent this.
Just for a test, create a window in one thread and do this in another thread:
ASSERT(CWnd::FromHandlePermanent(pWnd->m_hWnd) == pWnd) In a single-threaded application, this will work perfectly as expected, but if pWnd was created in a different thread, FromHandlePermanent() will return NULL because the window map is thread-specific data, and the ASSERT will be triggered.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
|
I want know how i can add highlight and change the text font in a DOC/VIEW application derived from a CRichEditView.I would be glad if anyone could help me out.
|
|
|
|
|
Hi all,
I have a list control with checkboxes and multi-select style.
when i select several items, and then check 1 fo the selected items, i expect the rest of the items to be checked as well...by default it doesn't happened
any1 knows a style of list to fix my problem?
thanks in advanced,
Yaron
Ask not what your application can do for you,
Ask what you can do for your application
|
|
|
|
|
YaronNir wrote:
any1 knows a style of list to fix my problem?
There isn't one. You will have to do this yourself whenever the user clicks on an item. Use GetSelectedCount() and GetNextItem() to loop through the selected items and set them all to the state you want.
If you're not using MFC, use the LVM_GETSELECTEDCOUNT and LVM_GETNEXTITEM mesages.
Hope this helps,
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
thanks for the reply!
i am not using MFC, so i will use the LVM_ messages you've mentioned.
you said to catch the user clicks on the item. but what if the user select the item via the keyboard???
is there a certain message that item was selected? no matter how (keyboard or mouse?)
thanks again
Yaron
Ask not what your application can do for you,
Ask what you can do for your application
|
|
|
|
|
YaronNir wrote:
is there a certain message that item was selected?
LVN_ITEMCHANGED , which will detect if an item has been selected/unselected by either mouse or keyboard, but I'm not sure if it works when an item is checked or unchecked. Perhaps you could test it
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Hi, thanks again for the reply.
i am using ATL/WTL, i have a dialog that correspond to the:
NOTIFY_HANDLER(IDC_MY_LIST_CTRL, LVN_ITEMCHANGED, OnListItemChanged)
and in the implementation:
LRESULT CAddContentDlg::OnListItemChanged(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pnmh;
return 0;
}
can u show me some code to handle the multiple selection and checking cause i'm kinda having trouble of writing it correctly.......
thanks again
Yaron
Ask not what your application can do for you,
Ask what you can do for your application
|
|
|
|
|
Hi, thanks again for the reply.
i am using ATL/WTL, i have a dialog that correspond to the:
NOTIFY_HANDLER(IDC_MY_LIST_CTRL, LVN_ITEMCHANGED, OnListItemChanged)
and in the implementation:
LRESULT CMyDlg::OnListItemChanged(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pnmh;
return 0;
}
can u show me some code to handle the multiple selection and checking cause i'm kinda having trouble of writing it correctly.......
thanks again
Yaron
Ask not what your application can do for you,
Ask what you can do for your application
|
|
|
|
|
I'll have a go
LRESULT CMyDlg::OnListItemChanged(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pnmh;
HWND hwndLV = pnmh->hwndFrom;
int iState = ListView_GetCheckState(hwndLV, pNMListView->iItem);
int iItem = ListView_GetNextItem(hwndLV, -1, LVNI_SELECTED);
while(iItem >= 0)
{
ListView_SetItemState(hwndLV, iItem, INDEXTOSTATEIMAGEMASK(iState+1), LVIS_STATEIMAGEMASK);
iItem = ListView_GetNextItem(hwndLV, iItem, LVNI_SELECTED);
}
return 0;
} Hope this helps,
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Excellent man! works perfectly! many thanks!
keep up the good help work....
cheers
Yaron
Ask not what your application can do for you,
Ask what you can do for your application
|
|
|
|
|
You're welcome
YaronNir wrote:
Excellent man! works perfectly!
Cool! I didn't actually test that - just wrote it straight from the docs, so I'm happy that it works that well
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
I'm trying to learn the alternative way of writing an IF statement. The code below returns a 0, it should be an 8 since 3<5, what am I doing wrong?
Thanks
<br />
<br />
#include <iostream><br />
<br />
using namespace std;<br />
<br />
int main()<br />
<br />
{<br />
int a = 3;<br />
int b (5);<br />
int x(0);<br />
<br />
a < b ? x == a + b : x == 9;<br />
<br />
cout<<x<<"\n\n";<br />
<br />
return 0;<br />
}<br />
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
|
|
|
|
|
You use the comparison operator (==) insted of the assignement operator.
Replace
a < b ? x == a + b : x == 9;<br />
<br /> by
a < b ? x = a + b : x = 9;<br />
<br />
|
|
|
|
|
DaveE9th wrote:
a < b ? x == a + b : x == 9;
[...] what am I doing wrong?
The result is false , whichever clause is executed:
x is neither equal to a+b (that is 8) nor is x equal to 9.
If you want to assign x a value, you need to write
a < b ? x = a + b : x = 9;
My opinions may have changed, but not the fact that I am right.
|
|
|
|
|
Thanks, your both right. I'm glad it's working now.
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
|
|
|
|
|
DaveE9th wrote:
a < b ? x == a + b : x == 9;
You could also do this:
x = ( a < b ? a + b : 9 );
There are 10 kinds of people - those that get binary and those that don't.
|
|
|
|
|
x = ( a < b ? a + b : 9 );
This is the right way.
|
|
|
|
|
hi
Is possible to use C++ to wirte a promgram to transfer the data from Pc to the ir then tramit to the reciever the RS232
|
|
|
|
|
Que?...
"After all it's just text at the end of the day. - Colin Davies
"For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Graus
|
|
|
|
|
Hi,
I have often seen concept of callback funtion used in MFC.Can any body please explain What is a callback function and what is its use ?
Thanks
Rohit
|
|
|
|
|
it is your function pointer to what you enter f.e. like argument to other function
that (other) function will call yours (call-back) if necessary
classic example is qsort - sorts array by by you defined criteria
you just need to enter data to sort and your callback function what will compare two items
the rest makes qsort not knowing about any data details
see qsort msdn example
t!
(http://www.codeproject.com/info/search.asp?target=callback&Submit1=+Search+&st=tt&qm=all[^])
|
|
|
|