|
Hi All.
I start to learn multiThread and i wrote some simple program.
I dont know why i crash - i try to look into by using debug and i dont see nothing wrong.
IF someone can tell me what i doing in wrong way .... ?
Thanks for any help.
Here the code :
<br />
...<br />
...<br />
<br />
using namespace std;<br />
<br />
CRITICAL_SECTION sc;<br />
<br />
void __stdcall func(LPVOID param)<br />
{<br />
char szBuffer[256];<br />
{<br />
EnterCriticalSection(&sc);<br />
long lVal = *((long*)param);<br />
<br />
<br />
while(lVal > 0)<br />
{<br />
sprintf(szBuffer, "lVal is : %ld \n", lVal--);<br />
printf(szBuffer);<br />
}<br />
LeaveCriticalSection(&sc);<br />
}<br />
}<br />
<br />
<br />
void main(void)<br />
{<br />
HANDLE thread1,thread2;<br />
<br />
long lVal = 10;<br />
<br />
thread1 = CreateThread(NULL,<br />
5,<br />
(LPTHREAD_START_ROUTINE)func,<br />
&lVal,<br />
CREATE_SUSPENDED,<br />
NULL);<br />
<br />
SetThreadPriority(thread1, THREAD_PRIORITY_HIGHEST);<br />
<br />
lVal = 1000;<br />
<br />
thread2 = CreateThread(NULL,<br />
5,<br />
(LPTHREAD_START_ROUTINE)func,<br />
&lVal,<br />
CREATE_SUSPENDED,<br />
NULL);<br />
<br />
SetThreadPriority(thread2, THREAD_PRIORITY_HIGHEST);<br />
<br />
ResumeThread(thread2);<br />
ResumeThread(thread1);<br />
<br />
WaitForSingleObject(thread2, INFINITE);<br />
WaitForSingleObject(thread1, INFINITE);<br />
}<br />
|
|
|
|
|
maybe you have a deadlock, plz check ur algorithm before u check ur code..
And ever has it been that love knows not its own depth until the hour of separation
|
|
|
|
|
I don't have deadlock. and the algorithm is fine.
In case deadlock the application will not crash -
If i init the critical section ( in first line of the main i do InitializeCriticalSection(&sc); ) then i avoid the crash but i still see that the second thread get into the critical area even that the first thread is in it.
What wrong in the code ?
|
|
|
|
|
I realy need some help here.
|
|
|
|
|
It seems your problem is EnterCriticalSection
|
|
|
|
|
You missed this one buddy :
InitializeCriticalSection(&cs);
Dont you need to initialize it before you could enter into it?
Add it here:
<br />
char szBuffer[256];<br />
{<br />
InitializeCriticalSection(&cs)------->Here<br />
EnterCriticalSection(&sc);<br />
long lVal = *((long*)param);<br />
Good Luck
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[My Current Status]
Link2006 wrote:Let's take it outside of CP
Jeremy : Please don't.I would love to see this.I'm making the popcorn already.
|
|
|
|
|
First of all ... the init of he critical section is need to be in the main and not in the function ( i dont want each of the thread the init it each time it start to run).
Second ... even if i call init in the main i have problem to run the second thread ( its just dont run ).
|
|
|
|
|
Oops didn't read the program fully.CS should be initialized once. Try this.
<br />
#include <conio.h><br />
<br />
using namespace std;<br />
<br />
CRITICAL_SECTION sc;<br />
<br />
void __stdcall func(LPVOID param)<br />
{<br />
AfxMessageBox("in thread");<br />
char szBuffer[256];<br />
long lVal = (*(long*)param);<br />
EnterCriticalSection(&sc);<br />
<br />
CString cs;<br />
cs.Format("%d",lVal);<br />
AfxMessageBox(cs);<br />
<br />
int testVal=lVal;<br />
while(lVal > 0)<br />
{<br />
sprintf(szBuffer, "I'm %d lVal is : %ld \n",testVal,lVal--);<br />
printf(szBuffer);<br />
}<br />
<br />
LeaveCriticalSection(&sc);<br />
AfxMessageBox("done"); <br />
}<br />
<br />
<br />
void main(void)<br />
{<br />
HANDLE thread1,thread2;<br />
<br />
long lVal = 5000;
InitializeCriticalSection(&sc);<br />
thread1 = CreateThread(NULL,5,(LPTHREAD_START_ROUTINE)func,&lVal,CREATE_SUSPENDED,NULL);<br />
<br />
<br />
SetThreadPriority(thread1, THREAD_PRIORITY_HIGHEST);<br />
<br />
long lVal2 = 1000;
<br />
thread2 = CreateThread(NULL,5,(LPTHREAD_START_ROUTINE)func,&lVal2,CREATE_SUSPENDED,NULL);<br />
<br />
SetThreadPriority(thread2, THREAD_PRIORITY_HIGHEST);<br />
<br />
ResumeThread(thread1); Sleep(3000);
ResumeThread(thread2);<br />
<br />
WaitForSingleObject(thread2, 500);
WaitForSingleObject(thread1, 500);
while(getch()!=27);
}<br />
Just did this one for test. Try it and tell me if it's useful or not. It's too late here .. see u tomorrow.. goodnight
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[My Current Status]
Link2006 wrote:Let's take it outside of CP
Jeremy : Please don't.I would love to see this.I'm making the popcorn already.
|
|
|
|
|
Hi all,
I have a CListCtrl object in my programme. I want to unload or destroy it programmatically. How it is possible.
Please Help me,
Thanks in advance
George
|
|
|
|
|
georgekjolly wrote: have a CListCtrl object in my programme. I want to unload or destroy it programmatically. How it is possible.
have you created that programmatically?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief And you
|
|
|
|
|
CListCtrl* pCtrl;
pCtrl->DestroyWindow();
delete pCtrl;
--
Touch eyeballs to screen for cheap laser surgery
|
|
|
|
|
Jörgen Sigvardsson wrote: CListCtrl* pCtrl;
pCtrl->DestroyWindow();
delete pCtrl;
problem if it created statically!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief And you
|
|
|
|
|
Then you'll just need to call DestroyWindow(). The CListCtrl object is so small, that it doesn't matter much. We're talking 4-8 bytes.
|
|
|
|
|
Jörgen Sigvardsson wrote:
Then you'll just need to call DestroyWindow(). The CListCtrl object is so small, that it doesn't matter much. We're talking 4-8 bytes.
let have a try..
int i=10;
int *ptr=&i;
delete ptr; // program will crash here
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief And you
|
|
|
|
|
What does that have to do with anything? What I'm saying is that if you have a statically allocated object such as CListCtrl m_list , then that object will occupy around 4 to 8 bytes of memory (window handle + vtable). If you kill the window, which the object is a handle for, then 4 to 8 bytes is what'll be left.
DestroyWindow != delete
--
Nominated For Three Glemmys
|
|
|
|
|
Jörgen Sigvardsson wrote: DestroyWindow != delete
he he i got confused with delete operator=delete ...
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief And you
|
|
|
|
|
|
If you had meant to clear the content, m_List->ResetContent()
If you had meant to close it, m_List->CloseWindow()
If you had meant to destroy it, m_List->DestroyWindow()
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[My Current Status]
Link2006 wrote:Let's take it outside of CP
Jeremy : Please don't.I would love to see this.I'm making the popcorn already.
|
|
|
|
|
it is pisibile to hide the CListCtrl ?
Bravoone
|
|
|
|
|
Yup, m_list->ShowWindow(false);
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[My Current Status]
Link2006 wrote:Let's take it outside of CP
Jeremy : Please don't.I would love to see this.I'm making the popcorn already.
|
|
|
|
|
I have the following problem. My C++ application creates WebBrowser Control object (CLSID_GFWebBrowser) and displays and URL there. The URL is to an authorization form (“enter user name and password”). When username/password are invalid, the response will be HTML page with message like “invalid username or password”. When the authorization passed, the response will be not-HTML document (PDF, DOC, XLS, PPT).
My application has to recognize the not-HTML response and close the browser window immediately. The problem is: how to recognize it? I tried to handle event DWebBrowserEvents2::DownloadComplete, but in this moment we cannot understand what the document type is. It will be possible later, when event DWebBrowserEvents2::DocumentComplete happens. But when the document is MS Office’s (DOC, for instance), the dialog with “Open, Save or Cancel?” will appear first – I do not need this. I tries to suppress the dialog appearance, but could not.
|
|
|
|
|
i require basic knowledge on Collaboration Data Objects (1.x)and Outlook Object Model.
|
|
|
|
|
Buy a textbook on it then - also try this link:
Google
A cynic is a man who, when he smells flowers, looks around for a coffin. -H.L. Mencken
|
|
|
|
|
JWood wrote: Buy a textbook on it then - also try this link:
Google
he he he!.. programmer paradise
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief And you
|
|
|
|
|
And Codeproject?
|
|
|
|