I am having a little bit of a concept problem having to do with MFC (and I hope this is the right part of the forum). I am writing a program with MFC / C++ to control a robot.
I did do some other research on this and it seems that the radio buttons cannot return a value. Initially I thought I could have return values for the joint sent from the radio button that would tell the SEND command which joint to move, but I am not really sure how I can do this now. Does anyone else have an idea of how this could be done?
Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.
- Brian W. Kernighan
I am having a little bit of a concept problem having to do with MFC (and I hope this is the right part of the forum).
If your program really is MFC, then no, this is not the right part. The correct section to post about it would then be http://www.codeguru.com/forum/forumdisplay.php?f=7. And in this case the link posted by S_M_A is probably a good place to start.
Sorry about the wrong section. If either of you guys can move it then go ahead.
@S_M_A
I am using a worker thread to run this part of the application because otherwise the GUI freezes.
Code:
UINT WorkerThreadProc( LPVOID pParent )
{
CString command;
CXdemDlg* p = ((CXdemDlg*)pParent);
do
{
if (p->RadioWaist.Checked)
(p->m_edresp.SetWindowText("True"));
} while ( ((CXdemDlg*)pParent)->isStepping() );
return TRUE;
}
RadioWaist is the member variable (an int) for the array of radio buttons. So obviously if I try to compile this I will get a type mismatch, however, it won't allow me to access Radio_Waist (the radio button). I am a bit new to multi-threading, so is there something that I am missing that I need to do in order to be able to check the status of the radio button and not just the variable?
A better way in my opinion is to add code that handles the radio buttons (in the GUI code) and let that code refine the information into an enum or similar to be accessed in the worker thread. Don't forget to make it thread safe http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx
Doing it that way makes it easier if you later on decide to replace the radio buttons with for instance a combox.
Last edited by S_M_A; October 1st, 2011 at 10:23 AM.
Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.
- Brian W. Kernighan
I think we might have a bit of a misunderstanding here.
I did write a switch function that returned a value for when one of the radio buttons was clicked (just to test to make sure I did it correctly).
Code:
int CXdemDlg::Joint()
{
UpdateData(true);
switch(RadioWaist)
{
case 0:
m_edresp.SetWindowText("Waist Selected");
return(1);
break;
case 1:
m_edresp.SetWindowText("Shoulder Selected");
return(2);
break;
case 2:
m_edresp.SetWindowText("Elbow Selected");
break;
case 3:
m_edresp.SetWindowText("Hand Selected");
break;
case 4:
m_edresp.SetWindowText("Wrist Selected");
break;
}
}
But I guess this can't be directly applied to the thread? I get an assert saying that if this fired I am mostly trying to pass objects from one thread to another. I guess that is what you mean by making it safe?
I think we might have a bit of a misunderstanding here.
I did write a switch function that returned a value for when one of the radio buttons was clicked (just to test to make sure I did it correctly).
Code:
int CXdemDlg::Joint()
{
UpdateData(true);
switch(RadioWaist)
{
case 0:
m_edresp.SetWindowText("Waist Selected");
return(1);
break;
case 1:
m_edresp.SetWindowText("Shoulder Selected");
return(2);
break;
case 2:
m_edresp.SetWindowText("Elbow Selected");
break;
case 3:
m_edresp.SetWindowText("Hand Selected");
break;
case 4:
m_edresp.SetWindowText("Wrist Selected");
break;
}
}
But I guess this can't be directly applied to the thread? I get an assert saying that if this fired I am mostly trying to pass objects from one thread to another. I guess that is what you mean by making it safe?
This is where I am a bit confused....
The simple way to deal with this is to let MFC help you.
If you set the group style to the first radio button, and then create an INT DDX variable using the "Add variable" option, MFC will track the selected radio item in that group.
Then you can pass the index of the selected radio button to the thread when you start the thread.
The simple way to deal with this is to let MFC help you.
If you set the group style to the first radio button, and then create an INT DDX variable using the "Add variable" option, MFC will track the selected radio item in that group.
Then you can pass the index of the selected radio button to the thread when you start the thread.
I have actually done this. Like I said I wanted to test to make sure that I set up the group right and everything so I created the function Joint to reply in an edit box which joint radio button was clicked. The issue I am apparently having is passing the index for the radio button to the thread.
Here are my DDX controls. I set the group of radio buttons as Radio because thats what I read somewhere else. Is that what should be changed?
My radio buttons are:
Radio_Waist
Radio_Shoulder
Radio_Elbow
Radio_Wrist
Radio_Hand
And like I said I tried to make sure I set that up right first by having a confirmation print out to an edit box saying "Waist Selected" or "Shoulder Selected"...etc. So I know they are assigned the way they should be with indexes correct.
My issue seems to be with the thread accessing them correctly?
P.S. The returns in the Joint function were just as a test for something. But is that how I should pass it to the thread?
Last edited by SpartanGuy07; October 3rd, 2011 at 03:20 PM.
What you can do instead is to let the dialog code catch the message that the radio button sends when being selected and set a member variable to specific value. Probably an enum like Waist, Shoulder ... and so on. That member variable can safely be accessed by the thread if protected by for instance a CriticalSection see http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx
Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.
- Brian W. Kernighan
Bookmarks