|
Does this help Application desktop toolbars[^] ?
Ant.
I'm hard, yet soft. I'm coloured, yet clear. I'm fruity and sweet. I'm jelly, what am I? Muse on it further, I shall return! - David Williams (Little Britain)
|
|
|
|
|
|
Um, Interseting. If I come across some other examples I will let you know. Good luck with that.
Ant.
I'm hard, yet soft. I'm coloured, yet clear. I'm fruity and sweet. I'm jelly, what am I? Muse on it further, I shall return! - David Williams (Little Britain)
|
|
|
|
|
I now how can I make a VC++ program that using Access table but I want to create a program that can save data in database mode and dont use Access by reading data from a textsource please help me.
Thank you alot
|
|
|
|
|
|
Hello,
In my project, I have to check if a particular condition is satisfied and take some action accordingly. Hence I have started a thread which continously checks for the condition as shown below:
UINT ThreadProc(LPVOID lp)
{
while(TRUE)
{
DWORD dwWaitResult = WaitForSingleObject(MonitorEvent,INFINITE);
switch (dwWaitResult)
{
case WAIT_OBJECT_0:
//do the required action and ...reset event
ResetEvent(MonitorEvent);
break;
}
}
MonitorEvent will be set in the part of the code where the required condition is met. Once the the event is fired, ThreadProc acts and does the required task and again has to wait till the event is fired again. This has to continue till application exists.
The result of the above code is that CPU time is getting consumed 100%.
Can anyone please suggest a better solution?
Thanks in anticipation of information.
madhavi.
|
|
|
|
|
I had a similar problem with a ISDN Monitor. I had to wait for messages from the driver of the ISDN controller signaling an incoming call. The situation was exactly the same, a workerthread with a while loop resulting in 100% CPU usage. Adding a
Sleep(200);
in the while loop solved the problem for me. If some mseconds of "absence" doesn't affect your application it could also work for you.
MS
|
|
|
|
|
Had I been at your place, I would have used a different approach and that is to use SendMessage.
Write a class of your own, and write a message handler for your own defined message. In that message handler write the code which you have written above in thread in case of MonitorEvent being successful. From the class where Monitor event is fired, use SendMessage, this will send the Message to the other class and MFC will handle the rest.
I hope that is a better and more efficient solution.
Regards,
Imagine that you are creating a fabric of human destiny with the object of making men happy in the end, giving them peace and rest at last, but that it was essential and inevitable to torture to death only one tiny creature..and to found that edifice on its unavenged tears, would you consent to be the architect on those conditions? Tell me, and tell me the truth!
-Fyodor Dostoevsky, The Brothers Karamazov
|
|
|
|
|
And anywhere your application might be enhanced and then encounter a MsgWaitforMultipleObjects, your 'synchronization' scheme would be broken.
You REALLY need to wait on events or on other waitable objects in a properly desiged multithreaded application. Waiting for sendmessages is the old, and not thread safe, style of Windows programming
The fact that your CPU was at 100% means something else was wrong.
For example, you might have created an event that was Manual Reset type, and after your thread got signaled, and executed, you forgot to call ResetEvent before waiting for it again...
|
|
|
|
|
Hi Blake Miller,
Thanks for your inputs.
I have declared my monitor event as below first.
volatile HANDLE m_MonitorEvent;
m_MonitorEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
ResetEvent(m_MonitorEvent);
//This event is set in the part of code which triggers this event.
SetEvent(m_MonitorEvent);
//Thread function
BOOL b_EventRunning = TRUE;
UINT ThreadProc(LPVOID lpVoid)
{
while(b_EventRunning == TRUE)
{
DWORD dwWaitResult = WaitForSingleObject(m_MonitorEvent, INFINITE);
switch (dwWaitResult)
{
case WAIT_OBJECT_0:
//Do the work
ResetEvent(m_MonitorEvent);
break;
default:
Sleep(1);
break;
}
}//end of while
}//end of ThraedProc
I used INFINITE in WaitForSingleObject because my this thraed has to run till the application is exited.
When this thread is doing the job, the CPU usage is shown as 100%. Please let me know if I'm missing out something here which is making my CPU consume 100%?
Thanks
Madhavi
|
|
|
|
|
ledallam wrote:
The result of the above code is that CPU time is getting consumed 100%.
Does that mean that WaitForSingleObject() is returning something other than WAIT_OBJECT_0 ?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
Could you show how you declare and create MonitorEvent ? WaitForSingleObject can return WAIT_FAILED . For your example it means it is something wrong with MonitorEvent . Also add TRACE("dwWaitResult(%d)\n",dwWaitResult) to check the return value from WaitForSingleObject .
|
|
|
|
|
Hi Andrzej Markowski,
Thanks for your inputs.
I have declared my monitor event as below first.
volatile HANDLE m_MonitorEvent;
m_MonitorEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
ResetEvent(m_MonitorEvent);
//This event is set in the part of code which triggers this event.
SetEvent(m_MonitorEvent);
//Thread function
BOOL b_EventRunning = TRUE;
UINT ThreadProc(LPVOID lpVoid)
{
while(b_EventRunning == TRUE)
{
DWORD dwWaitResult = WaitForSingleObject(m_MonitorEvent, INFINITE);
switch (dwWaitResult)
{
case WAIT_OBJECT_0:
//Do the work
ResetEvent(m_MonitorEvent);
break;
default:
Sleep(1);
break;
}
}//end of while
}//end of ThraedProc
I used INFINITE in WaitForSingleObject because my this thraed has to run till the application is exited.
When this thread is doing the job, the CPU usage is shown as 100%. Please let me know if I'm missing out something here which is making my CPU consume 100%?
Thanks
Madhavi
|
|
|
|
|
First of all you need to find out few things:
1. The return value from WaitForSingleObject . Add TRACE to your code:
DWORD dwWaitResult = WaitForSingleObject(m_MonitorEvent, INFINITE);
TRACE("dwWaitResult(%d)",dwWaitResult);
2. How much time the "work" takes (possible infinite loop). Modify your code like this:
DWORD dwLastTickCount = ::GetTickCount();
DWORD dwWaitResult = WaitForSingleObject(m_MonitorEvent, INFINITE);
TRACE("dwWaitResult(%d)",dwWaitResult);
switch (dwWaitResult)
{
case WAIT_OBJECT_0:
ResetEvent(m_MonitorEvent);
break;
default:
break;
}
TRACE("Time of the job in ms: %d\n",::GetTickCount()-dwLastTickCount);
dwLastTickCount = ::GetTickCount();
Run your application in debug mode and post the debug output.
|
|
|
|
|
After modifying the code like this,
DWORD dwLastTickCount = ::GetTickCount();
DWORD dwWaitResult = WaitForSingleObject(m_MonitorEvent, INFINITE);
TRACE("dwWaitResult(%d)",dwWaitResult);
// print return value from WaitForSingleObject
switch (dwWaitResult)
{
case WAIT_OBJECT_0://Do the work
ResetEvent(m_MonitorEvent);
break;
default
break;
}
TRACE("Time of the job in ms: %d\n",::GetTickCount()-dwLastTickCount);
dwLastTickCount = ::GetTickCount();
my debug output is
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 16
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 15
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 16
dwWaitResult(0)Time of the job in ms: 16
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 15
dwWaitResult(0)Time of the job in ms: 16
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 16
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 15
dwWaitResult(0)Time of the job in ms: 16
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 15
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 16
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 16
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 15
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 16
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 15
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 0
dwWaitResult(0)Time of the job in ms: 15
|
|
|
|
|
Everything looks fine. What is the CPU usage now? If still 100% how often do you triger m_MonitorEvent ? Try to comment out SetEvent(m_MonitorEvent) line and check the CPU usage again.
|
|
|
|
|
If comment out the line SetEvent(m_MonitorEvent), CPU usage is not 100%. Monitor event gets triggered very very fast. One thraed reads data from file which has say minimum 10,000 records, it reads each line, and triggers Monitor event which displays the line on UI. Hence as soon as a line is read from file, Moinitor event gets triggered. So I can say monitor event gets triggered 10,000 times if 10,000 lines are there in the file. During this raeding and displaying operation CPU usage is shown as 100%, after the reading of file is over, it is not 100%.
Can you please let me know how to proceed further?
Thanks
Madhavi
|
|
|
|
|
Could you post your source code? I will have look at it.
|
|
|
|
|
Can you please give me your email id so that I can post the code.
Thanks
madhavi
|
|
|
|
|
|
ok thanks.. i have sent the code
|
|
|
|
|
It's not unusual for the CPU to go to 100% for an intensive operation like reading every line from a file and updating the UI for every line of text. If the CPU usage drops back to normal levels after the file is processed, I would think your code is fine.
The only change I would make is to perhaps consider resetting the event REGARDLESS of the result of the wait operation. As long as it is not critical that EVERY line get updated to the display, because under normal circumstanes, you are not going to get WAIT_FAILED. You will probably never see WAIT_TIMEOUT since you specified INFINITE.
|
|
|
|
|
Hi
I have created a simple vb .net dll and tested it, it's working fine. My problem is, i would like to call that dll in a console c++ application but i dont know how to go about doing that. Please help me. Thank you in advance.
|
|
|
|
|
hi,
first u register the dll using regasm utility.
regasm "yourdll.dll";
it will create registry entry .[u can import type library
using tlbimp.exe ]
u can use this dll in by
#import "yourdll.dll"
by
baijumax
|
|
|
|
|
Hi all,
I've a problem about reading a file. I opened the file using CFile. when I asked for the size of file using CFile::GetLength() it's returing zero. Please help me for the solution.
Code here
-----
CFile pFile;
CFileException e;
if(!pFile.Open("img_1.bmp", CFile::modeRead | CFile::shareDenyWrite, &e))
{
printf("\n Error in opening File\n");
exit(1);
}
DWORD dwStart = pFile->GetPosition();
DWORD dwBitsSize = pFile->GetLength();
printf("\n Size %d\n", dwBitsSize);
BITMAPFILEHEADER fileHeader;
memset(&fileHeader, 0, sizeof(fileHeader));
UINT count = pFile->Read(&fileHeader, sizeof(fileHeader));
printf("\nByte Read %d %d\n", count, pFile->GetPosition());
----
Byte read and the current positon also give me the 'zero' values.
Thanks in Advance
Ardhendu
|
|
|
|