|
lol,seriously, i think my thread loop didn't exit even i made STOp=true; what's going here?
|
|
|
|
|
alberthyc wrote: what's going here?
Sadly the same thing that goes on here everyday
|
|
|
|
|
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I am guessing that you might have to make your STOP boolean a volatile.
I am guessing that the compiler has optimised it out of the while condition because it is not changed by the thread.
|
|
|
|
|
|
try...
//in header
volatile long m_iLock;
//global on top of cpp file
static volatile long *piLock;
//in OnBnClickedButtonStart
m_iLock =1;
piLock = &m_iLock;
AfxBeginThread(...
in the thread..
while(InterlockedCompareExchange(piLock,1,1))
{
//work
}
AfxMessageBox("Ending"...
And change m_bAutoDelete to true.
Tell me the results.
-Randor
|
|
|
|
|
sorry, what does this do??
|
|
|
|
|
I don't really use CWinThread but using my own thread class this works
class TestThread : public base_w32thread
{
private:
bool _brun;
protected:
virtual void run()
{
while(_brun)
{
TRACE("Thread is running\r\n");
Sleep(2000);
}
}
public:
void stop()
{
_brun = false;
}
};
// from the CMainDlg.h class declaration
protected:
HICON m_hIcon;
TestThread _thread;
// from the CMainDlg.cpp implementation
void CMainDlg::OnBnClickedButtonstart()
{
_thread.start();
}
void CMainDlg::OnBnClickedButtonstop()
{
_thread.stop();
if( WAIT_OBJECT_0 == ::WaitForSingleObject( _thread, 3000))
AfxMessageBox(_T("Thread exited"));
else
AfxMessageBox(_T("This did not happen"));
}
|
|
|
|
|
|
Mark Salsbery wrote: I had a filet-o-fish for lunch!
The down side to my adaptation is I don't think I could eat one now
|
|
|
|
|
interesting.....
if i really can't get the thread working, i'll try this method
|
|
|
|
|
I tried your code in a simple dialog project and it works.
In your original post you state that it is looping with no explanation as to what it is doing.
I put an intensive processor loop in there and tried and got a termination using the TerminateThread which is what you describe.
Because your WaitSingleObject specifies that you should only wait 2 seconds for the object to be signalled, it may be because your thread function is not terminating within 2 seconds it is returning a WAIT_TIMEOUT and as you only check for a WAIT_OBJECT_0 you assume that something has failed which is incorrect.
All that has happened in effect is the you got tired of waiting for it to complete and decide to kill it prematurely by incorrectly assuming that something must be wrong.
If your thread is not being signalled within the 2 seconds you specify I would assume that the problem lies in your thread function in that it takes too long to complete each pass through the while loop.
However without knowing what goes on in the loop it would be difficult to provide further guidance.
|
|
|
|
|
i tired for 10 seconds and i have a counter in my loop which starts from zero and with in few seconds, the loop looped about 1000 times, so i guess i shouldn't take that long to terminate
|
|
|
|
|
I think if he tried the code I posted, the wait timeout would not occur. Essentially what is probably happening is that the value of the BOOL is being retrieved from L1 or L2 cache because the thread is doing nothing else to the stack/registers. The atomic operation would prevent the cache hit.
-Randor (David Delaune)
|
|
|
|
|
tried your code,but i don't really understand this part:
while(InterlockedCompareExchange(piLock,1,1))
what does it do? and I need to manually terminate the thread, but this while loop doesn't seem to do that...
|
|
|
|
|
See this article for shutdown/pause examples.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Hello every one!
I've written a program to collect some information about currently opened processes in the system; When i executed it within IDE(Ctrl+F5), there was nothing wrong with it; But when i tried to execute it alone(within Windows Explorer, we had got some problem with function OpenProcess, that's it'd returned NULL and when i tried to access last error code using GetLastError() function, it said: ERROR_ACCESS_DENIED!
What kind of diffrence is there between theses two scenario (execuing via IDE and executing alone (via OS))? And what can i do to access information about various processes with diffrent access rights?
|
|
|
|
|
try enabling debug privileges.
Something like:
<br />
HANDLE hToken=INVALID_HANDLE_VALUE;<br />
<br />
if (hToken || OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))<br />
{<br />
TOKEN_PRIVILEGES tp;<br />
tp.PrivilegeCount = 1;<br />
SetLastError(0);<br />
LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tp.Privileges[0].Luid);<br />
tp.Privileges[0].Attributes = E_PRIVILEGE_ENABLED;<br />
SetLastError(0);<br />
AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL);<br />
}<br />
-Randor (David Delaune)
|
|
|
|
|
Usef Marzbani wrote: ...we had got some problem with function OpenProcess...
What does the call to that function look like?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I recently added a manifest file to the resource for a program to enable it to be drawn with WinXP themes. One thing I have noticed is that the data in a report view list control is being drawn 2 pixels higher and 2 pixels to the right. I have no objection with this, but I usually use custom draw facilities to set the background colour of a line and I paint the background myself the same colour as the first line's background colour. I now find I've got an ugly vertical white line 2 pixels wide to the left of the data. Anybody any idea how to get rid of it?
I've put sample images at good.jpg for the program before I added the manifest and
bad.jpg for after I added it.
|
|
|
|
|
Hello,
I'm following this tutorial for MFC/C++: http://www.codeproject.com/listctrl/creportctrl.asp . This concerns implementing a CListCtrl-derived class with certain "out of the box" functionality that the MFC "report style" CListCtrl lacks. For example, the user can re-order items by clicking on a column header, and, optionally, the programmer can include a checkbox beside nodes.
I created a dialog that uses this CReportCtrl, but that didn't work - I could see entries, but there were no headings, and no lines, even though I'd called the SetGridLines() member function.
I then called ModifyStyle(0,LVS_REPORT), which is meant to be used with the CListCtrl base class. Now, CReportCtrl was mostly fully functional. The column headers were visible. When I called SetGridLines(TRUE), gridlines appeared. However, one last important piece of functionality was still missing: I couldn't re-order column entries by clicking on a column header, in the style of windows explorer.
I can confirm that the function(OnColumnClick) is called when I click on a column header. I can confirm that _CompareFunction is *not* being called.
Most recently, I changed the List dialog item properties view from "Icon" to "Report". This had the effect of causing all sorts of errors such as "Unhandled exception at 0x78144580 in Lustre.exe: 0xC0000005: Access violation reading location 0x0aa30324" when I debugged. Apparently, this was the offending code:
int CReportCtrl::InsertItem(int nIndex, LPCTSTR pszText, ...)<br />
{<br />
const int iIndex = CListCtrl::InsertItem(nIndex, pszText);<br />
<br />
if (!_IsValidIndex(iIndex))<br />
return iIndex;<br />
<br />
CStringArray arr;<br />
arr.Add(pszText);<br />
<br />
va_list list;<br />
va_start(list, pszText);<br />
<br />
for(int iColumn = 1; iColumn < GetColumnCount(); iColumn++)<br />
{<br />
LPCTSTR lpsz = va_arg(list, LPCTSTR);<br />
CString str = (lpsz == NULL) ? _T("") : lpsz;<br />
arr.Add(str);
CListCtrl::SetItemText(iIndex, iColumn, str);<br />
}<br />
<br />
va_end(list);<br />
<br />
_AssignNewItemData(iIndex, arr.GetData(), arr.GetSize());<br />
<br />
return iIndex;<br />
}
I'd be very grateful if someone could point me in the right direction. I suspect this has something to do with the fact that I am using visual studio 2005, and this tutorial was created with visual studio 6,
Thanks a lot,
Sternocera ( ( (
|
|
|
|
|
Sternocera wrote: arr.Add(str); // This particular line was flagged by visual studio
As what?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
You know when you're debugging and you get the little green arrow that highlights the offending code? Thats what I meant by flagged.
|
|
|
|
|
So what is the value of str at that point?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
At that juncture, the value of str is "‹ÿU‹ìV‹u÷Æ"
Hope that helps
|
|
|
|