|
I was experimenting on a worker thread to do a timer which posts a message each second and the thread is created in the CView constructor
(MY APP is SDI)
class CMyView:public CView
{
//consturctor ...
public:
static UINT RunTimer(LPVOID p);
void Run();
public:
volatile BOOL m_bRunTimer;
//other stuff
};
CMyView::CMyView()
{
m_bRunTimer=TRUE;
//Initialize Stuff
AfxBeginThread(RunTimer,this); //Get A Thread To Post A Message Every Second
}
the Run Timer Function is used in This Way
/////////////////////////////////////////////////
UINT CMyView::RunTimer(LPVOID p)
{
((CMyView*)p)->Run ();
return 0;
}
void CMyView::Run ()
{
while (m_bRunTimer)
{
::Sleep (1000);
PostMessage (UWM_TIMER_SECOND);
/*Note Here that UWM_TIMER_MESSAGE IS user Defined Message*/
}
}
every thing works great till this point and the thread posts a message each second and the message handling is doin great the problem arises when the window(program) is closed then a debug assertion failure dialog box is displayed saying the when we are Posting a message we are posting a message to a window that no longer exists.
I tried adding The line m_bRunTimer=FALSE; in ~CMyView() but no change then i wrote some code in the
void CMyView::OnDestroy()
{
m_bRunTimer=FALSE;
::Sleep(4000);//Simply Added to Elapse the Time Slice of Main Thread
CView::OnDestroy();
}
After Doing i finally got safe with removing the thread and returns value i used TRACE() that my thread is safley removed and everything but i think my way of getting rid of the thread is kind of not portable and clean is there any other way that i can do this safley.
|
|
|
|
|
IMO, after signalling the thread to stop you should use WaitForSingleObject.
|
|
|
|
|
I was thinking that the problem could be solved with another safety check.
Make sure the window exists before posting the message!
if( ::IsWindow(m_hWnd) ){
PostMessage (UWM_TIMER_SECOND);
}
Note that you want to invoke the GLOBAL IsWindow and not the class-specific override, that is whyt he :: is in front of the function name.
Now, you avoid your crash, and some other issue you might not have discovered yet.
I always make sure the target window still exists before posting or sending a mesage from a different thread.
Now, if you waited on an event and sets its timeout to 1000 milliseconds, you could signal the event from your main thread, and the timer thread could exit sooner (instead of always being asleep for 1 second).
If you see WAIT_OBJECT_0 you exit the thread, otherwise, if it gets WAIT_TIMEOUT, you just post your message as normal. This way, your main app would not necessarily need to wait 4 seconds, it could exit immediately.
Also, a s the other person suggests, you can wait on the timer thread handle in the main thread to know when the timer thread was done.
|
|
|
|
|
Hello,
I am not sure how this works I am just beginning to learn this.
A and B are objects of the same class named TestClass
in my main i have:
...
if (A != B)
...
.cpp file has the definitions:
this class has one data member:
char* m_Str
//comparison operator !=
bool MGString::operator!=(const char* Src) const
{
bool value;
value = Compare(Src,m_Sensitive);
return value;
}
//compare function
int MGString::Compare(const char* Src, bool m_Sensitive) const
{
int result;
if (! m_Sensitive)
{
result = strcmp(m_Str,Src);
}
else
{
result = strncmp(m_Str,Src,strlen(lino));
}
if (result !=0)
return false;
else
return true;
}
//conversion operator
MGString::operator const char*()
{
return m_Str;
}
this will not compile because strlen can not accept an argument (m_Str) of type TestClass it needs a char*.
I am guessing that the way I have it set up now my conversion operator returns type char* for the data memeber m_Str for object B. However the m_Str for object A is still of type TestClass so in main when the conversion operator is called for B i think it returns a convertred value for m_Str from TestClass to char*. How do I fix this?
thanks,
Lino
|
|
|
|
|
bool operator!=(const TestClass& s1, const TestClass& s2)
{
return Compare(s1.m_Str, s2.m_Str);
}
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
can u elaborate? why is this not a class member?
and i do want to use a conversion operator...are u telling that it would be incorrect of me to use a conversion operator?
can u show me how to use the conversion operator?
thanks,
Lino
|
|
|
|
|
lino_i wrote:
can u elaborate? why is this not a class member?
That was just an example. You could also make it a member, like:
class TestClass
{
operator LPCTSTR() const
{
return m_Str;
}
bool operator!=(TestClass& s1) const
{
return Compare(m_Str, s1.m_Str);
}
};
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
thanks for your patience...
again i am a beginner at all of this but...
looking at this again why do i even need a conversion operator if m_Str is declared a char* private data member when i build my class.
why doesn't this line of code:
if (A != B) call:
bool operator!=(TestClass& sl) const
{
return Compare(m_Str, sl.m_Str);
}
what's the point of the conversion operator?
and in your scenario above what is actually getting converted (the data member of which object?)
thanks,
Lino
|
|
|
|
|
lino_i wrote:
why doesn't this line of code:
if (A != B) call:
bool operator!=(TestClass& sl) const
{
return Compare(m_Str, sl.m_Str);
}
This works fine for me:
class TestClass
{
char *m_Str;
bool Compare(const char *str1, const char *str2) const
{
return (strcmp(str1, str2) == 0);
}
public:
TestClass(){}
~TestClass(){}
operator LPCTSTR() const
{
return m_Str;
}
bool operator!=(TestClass& s1) const
{
return Compare(m_Str, s1.m_Str);
}
}; lino_i wrote:
what's the point of the conversion operator?
If you needed to call a function or method that knew nothing about a TestClass object, but it did understand what a const char* was, it would be used in that context.
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
you have a conversion operator as well...why is it necessary? m_Str is already a pointer?
|
|
|
|
|
lino_i wrote:
why is it necessary?
I didn't say that it was. I was just showing you how to implement one. If there is never a need for users of your class to access m_Str , it can be removed. However, if you did want users of your class to be able to access m_Str , it would be needed. In your case, it would not be considered a "conversion" operator, but simply an "access" operator.
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
There is lots of stuff around on skinning dialogs and dialog controls, but has anyone done anything with skinned CPropertySheet and skinned CPropertyPage ???
|
|
|
|
|
Hello,
I have a function that calls a compare function which does a simple strcmp() comparison. Then in this function I return to the calling function the value of result.
this is my compare function:
bool m_Sensitive = false;
int result;
char lino[] = "value";
if (! m_Sensitive)
{
result = strcmp(lino,Src);
}
else
{
result = strncmp(lino,Src,strlen(lino));
}
return result;
Back in the calling function which returns a bool I need to analyze the return value.
I have tried
if (return !=0)
return false;
else
return true;
this is not working any thoughts?
thanks,
Lino
|
|
|
|
|
return m_Sensitive ? strncmp(lino, Src, strlen(lino) ) == 0 : strcmp(lino,Src)== 0 ;
returns TRUE if the strings match else FALSE
|
|
|
|
|
You've not indicated what is not working. The code snippet you've shown is syntactically correct.
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
In MS Visual C++, how do I gray out a dialog's radio button *IN CODE* as opposed to using the resource editor?
|
|
|
|
|
Call EnableWindow(FALSE) .
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Thanks David! I used the following code...
GetDlgItem(IDC_RADIO1)->EnableWindow(FALSE);
|
|
|
|
|
Like this ...
<br />
void CMyDlg::EnableField(UINT nID, BOOL bEnable)<br />
{<br />
CWnd *pWnd = GetDlgItem( nID );<br />
ASSERT( pWnd );<br />
pWnd->EnableWindow( bEnable) ;<br />
}<br />
<br />
EnableField( IDC_RADIOxxx, FALSE);
Uncomment the ShowWindow statement to re-inforce the fact that a dialog window is disabled by hiding it.
|
|
|
|
|
Thanks mtzlplyk! Useful code there!
|
|
|
|
|
I am having trouble getting my Picking code to work in collaboration with glUnProject. The picking will work with the use of glUnProject but I need it to work for me. anybody else run into that?
|
|
|
|
|
Hi.
I’m a VC++ developer that is going to distribute exactly 10 licenses of an ActiveX control I’ve done. I would like to make the ActiveX itself freely available on for e.g. a public ftp-server. So, about 100 users have may installed my ActiveX but because my customer just bought license for 10, only 10 users should bee able to use it at the same time.
I’ve figured of this a lot and have found out that all controls may have some connection to a server that “allow” them to run. If that’s a good thought, how should I design that “server-program”?
Would be very pleased of tips of where to find different ways to handle license and login techniques!
In which way is preferred to handle my problem? All thoughts are helpful!
_____________________________
...and justice for all
APe
|
|
|
|
|
I have written an MFC App that will display a hard coded menu item CString to the DC (the screen) in 3 diffetrnt RGB values using 'Set' and 'Get' functions to randomize the color. I have a random number generator that passes the value to a switch statement with RGB colors.
I now want to change the code to rendomize the color of EACH INDIVIDUAL CHARACTER in the string, as opposed to having the whole string the same color.
If some one could send a code fragment to access each character in s CString.
I'm using DrawText to display.
- thanks for your help
|
|
|
|
|
SkunkedWorks wrote:
If some one could send a code fragment to access each character in s CString.
No special code required. Just use the [] operator or the GetAt() method.
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Thanks.
So... say I have this:
CString str("abc");
What would the syntax look like if i used GetAt()?
|
|
|
|