|
You shouldn't use polling for inter-thread communications. You should use kernel synchronization objects (like events) and WaitForSingleObject/WaitForMultipleObjects APIs instead.
Tomasz Sowinski -- http://www.shooltz.com ** If you're going to rape, pillage and burn, be sure to do things in that order. **
|
|
|
|
|
There are two ways of stopping a thread:- Telling it to stop (somehow),
- calling
TerminateThread . The last solution is almost guaranteed to generate memory and resource leaks and leave your program in a bad shape, so I'd suggest you don't even consider it as a viable alternative. So, basically if you want to stop a thread the only way is signal this wish in the hope that the thread notices and properly stops itself. How can you signal this?- Set a boolean variable thath the thread checks frequently. Make this variable
volatile to prevent certain erroneous conditions caused by optimizations in the code by the compiler.
- Signal an event with
SetEvent . This is specially fit if the thread spends most of its time waiting for an array of objects with WaitForMultipleObjects . The approach is not as good for threads devoted to computation. - If the thread has a message pump, send it a
WM_QUIT message. Not an option in your particular scenario, I guess.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
I would suggest that the client thread post a message to thread1, passing it an event handle that thread1 will set when it thinks that the client should resume execution. Ex:
while (GetMessage(&msg, 0, 0, 0)) {
switch (msg.message) {
case WM_USER:
HANDLE hEvent = (HANDLE)msg.wParam;
::SetEvent(hEvent);
break;
}
}
The code above is part of the thread1's message pump. Now the client thead:
HANDLE hEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
::PostThreadMessage(dwThread1ID, WM_USER, (HANDLE)hEvent, 0);
DWORD dwWaitResult = ::WaitForSingleObject(hEvent, dwMyTimeOutInMillisecond);
if (WAIT_OBJECT_0) {
} else {
}
As you can see, thread1 receives messages from client threads, and client threads wait for thread1 to set the event, but can eventually give up waiting.
Error handling is missing for clarity.
Hope this helps
Michel
If I am wrong or said something stupid, I apologize in advance ![Wink | ;)](https://www.codeproject.com/script/Forums/Images/smiley_wink.gif)
|
|
|
|
|
I have encouraged a problem when trying to connect many clients (a thread for each client) to a single server (another thread). After the socket is received from the first client to the server, no other clients can get into the server. They are blocked!!! I've tried to use CasyncSocket. But still doesn't work. Can anyone please help me??
Thank you!![Roll eyes | :rolleyes:](https://www.codeproject.com/script/Forums/Images/smiley_rolleyes.gif)
|
|
|
|
|
Ok! I've found that different threads can get access to the server. However, another question comes up. In the same single thread (one client), how come when I try to send something to the server many times in a for loop, only the first time can access to the server (socket is received by the server), other times the access is blocked?? So, it's no longer a multithread problem. It's a single(client) thread communicate with another single thread(server).
Can someone please explain to me??
thank you!![Roll eyes | :rolleyes:](https://www.codeproject.com/script/Forums/Images/smiley_rolleyes.gif)
|
|
|
|
|
Hi,
I have a problem:
1. Thread (A) for users (1 to 100 users, so can have max 100 threads here)
2. Thread (B) for array_function
3. Global array - store all input value and work liked a queue for those inputs....(line-up)
Thread (B) keeps retreiving the item from array for input data. Thread (A) keeps sending new inputs to the array. If Thread (B) processed one input data in the queue, it will return a signal back to thread (A). Otherwise, those input data is waiting...... if the waiting time is too long, thread (A) will abort that input data.
Is it possible to implement? Can I use WaitForSingleObject(ThreadB, 5000) to do the time-out stuff?
How to return value (let say boolean flag) from thread B to thread A?
Thanks!!!!
|
|
|
|
|
Ok, here is it:
You cant write WaitForSingleObject(ThreadB, 5000), because wait functions for thread objects wait for thread termination.
Instead, you can create event object(s) with CreateEvent function prior to creating threads and give them handle (that CreateEvent returned) or name of the event(s), or write a name stringently into your code. Then if some thread alter shared data (array, etc.) it calls PulseEvent, all others WaitForSingleObject or WaitForMultipleObjects for the event(s).
Name or handle of the event(s), as well as array pointer can be determined in thread procedures by lpParameter parameter of CreateThread and CreateRemoteThread functions. This parameter is the only one of ThreadProc. Just convert it from LPVOID to LPYOUR_SHARED_MEMORY.
Hope, I explained it well
P.S.There are many other ways for interprocess signalling - mutexes, semaphores, messages, just take a look into 'Syncronization Objects' page of MSDN.
|
|
|
|
|
How to change the cursor to IDC_HAND only when the mouse is on a button ?
thanx![Rose | [Rose]](https://www.codeproject.com/script/Forums/Images/rose.gif)
|
|
|
|
|
subclass the button, then handle the WM_SETCURSOR message for the button. You can set the cursor to the hand in that message.
Or in your parent window you could simply handle the WM_SETCURSOR message, and if the cursor is over the rectangle where the button is located, you could set the hand icon.
|
|
|
|
|
Thanx![Rose | [Rose]](https://codeproject.global.ssl.fastly.net/script/Forums/Images/rose.gif)
|
|
|
|
|
I need to write a sotware to be placed on one machine and can run some of its functions on an other computer knowing the ip address
for example ( shut down an other computer using its IP address)
I need Details because I'm new to this field
thanks in advaced
MR.Byte
|
|
|
|
|
DCOM i think is what you'd be interested in reading about.
"An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
|
|
|
|
|
Hi,
I have created an ActiveX control. Now i want to register this control in another machine. I copied the .ocx file to that machine and used the regsvr32 for registering the control. But i'm getting an error "LoadLibrary("control.ocx") failed. Invalid Access to memory Location."
Why is this. I have the local admin power to register the control in that machine.
Pls help
Vini
|
|
|
|
|
Could it be your .ocx is dependant on some DLLs not present in the target system? Check for dependencies from the C run-time library and MFC.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Thanks for the reply. But i'm new to this area. So can you tell me how to check for the dependencies?
Please...
|
|
|
|
|
Sure. Go to Project->Settings->General tab. The "Microsoft Foundation Classes" combobox sets whether MFC is statically or dynamically linked ("Shared DLL"). If the latter, try switching to the former to see if your problem disappears.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
It worked!!!!!
Thanks a lot....
Vini
|
|
|
|
|
Dear friends
I am new to VC++ and I need to display a bitmap file which i stored as an resource file or u can consider it as an input file. I want to do it using Win32 API.
Will you please give me provide me any such file's complete .dsw file. It will be very helpful to me. I don't have much time, charles petzold book have so many such files but at present i don't have that book and i have to submit this project in 1-2 days. Please help me and do the needful.
Thanking you in advance.
Binay
![Cry | :((](https://codeproject.global.ssl.fastly.net/script/Forums/Images/smiley_cry.gif)
|
|
|
|
|
HBITMAP hBitmap;
CClientDC dc(NULL);
CString szBmpFile(_T("..."));
hBitmap = (HBITMAP)LoadImage(AfxGetInstanceHandle(),szBmpFile,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
BITMAP bm;
CDC dcMem;
::GetObject(hBitmap,sizeof(bm),&bm);
dcMem.CreateCompatibleDC(pDC);
HBITMAP pOldBitmap = (HBITMAP)(dcMem.SelectObject(hBitmap));
CRect rect
pDC->BitBlt(Rect.left,Rect.top,bm.bmWidth,bm.bmHeight,&dcMem,0,0,SRCCOPY);
|
|
|
|
|
Dear Wangyiming
Thanks a lot for ur response.But I want to get a complete source code or complete .dsw file. I always refer Charles Petzold for basic programming code. here I don't have that book. So please give me such file which is a reads one .bmp file and displays it by getting hdc. It will be a great help to me at this time. I don't have time and have so much tension. Kindly help me.
Thanking you in advance.
Binay![Frown | :(](https://codeproject.global.ssl.fastly.net/script/Forums/Images/smiley_frown.gif)
|
|
|
|
|
DEar Wangyiming
Thanks for ur code. I want to a complete .dsw file. I always refer Charles Petzold book fo basic programming code. So please give or send me any such file which reads a bitmap file and then display it after getting hdc.
Thanking you in advance.
Binay![Frown | :(](https://codeproject.global.ssl.fastly.net/script/Forums/Images/smiley_frown.gif)
|
|
|
|
|
Dear friends
I am new to VC++ and I need to display a bitmap file which i stored as an resource file or u can consider it as an input file. I want to do it using Win32 API.
Will you please give me provide me any such file's complete .dsw file. It will be very helpful to me. I don't have much time, charles petzold book have so many such files but at present i don't have that book and i have to submit this project in 1-2 days. Please help me and do the needful.
Thanking you in advance.
Binay
![Frown | :(](https://codeproject.global.ssl.fastly.net/script/Forums/Images/smiley_frown.gif)
|
|
|
|
|
Hi,
#define GetRandom(min, max) ((rand() % (int)(((max) + 1) - (min))) + (min))
I found this algorithm from this discussion forum. However, i found that every time it produces the same "sequence" of output. For example, GetRandom(2, 100), the sequence is 43, 55, 99, 69, 64, 84, 95, 56...............
Since i'm doing multithread, my threads get the same result when they call it.......... so...............can anyone help me???
Thanks!
|
|
|
|
|
rand is a psuedo-random number generator. That means that the same sequence of numbers will be generated each time you use it in an application, however, the sequence of numbers that it generates are uniformly distrubuted. Which means that they are essentially random.
You can make the sequence become random each time you run your app if you seed the number generator with the srand() function when your app or thread starts up. The current time may be a good value to use to seed the number.
|
|
|
|
|
Anonymous wrote:
i found that every time it produces the same "sequence" of output.
If you use Srand() before your random function,then this won't happend.
Mazy
"So,so you think you can tell,
Heaven from Hell,
Blue skies from pain,...
How I wish,how I wish you were here." Wish You Were Here-Pink Floyd-1975
|
|
|
|