|
Hi,
I am facing a peculiar problem. I am using Visual C++ for a program which reads the serial port continously. I am using a "while" loop for this. But after starting the program I am not able to do anything with the application window (lik minimize, maximize, or stop the program). Can anybody help me in tackling this problem.
With thanks in advance.
VKA
|
|
|
|
|
You need to have a separate thread for lengthy operations. Either that or you can pump window messages in the while loop, but that's not really recommended as it's a bad design.
|
|
|
|
|
Hi hfry,
Thanks for the reply. But Can youp please tell me how to use separate thread?
On the second point do you mean show message option with YES NO click buttons or some thing else?
I am new to programming. That is why I am not getting the terminology.
Thanks
vka
|
|
|
|
|
You create threads with the C function _beginthreadex, unless you are absolutely sure you are not using any CRT functions, in which case you can use CreateThread. You then need various ways of sending information from your main thread to the worker thread and vice versa. You can use events and have the thread wait for events to be signalled, or you could have the worker thread have its own message queue and pass messages using PostThreadMessage. I find the easiest way for the thread to send messages to the main thread is to just use SendMessage. This really all boils down to your requirements. Is there any particular reason why you aren't using asynchronous i/o? Did you check out any of the articles on Code Project?
Regarding sleeping in your loop: Sleep won't help your application respond. The reason it doesn't respond is because you are performing a lengthy operation in your main thread. The main thread never gets to get the next message in the message queue to handle. If you don't know what a message loop is or what windows messages are, it's time to go find a tutorial online.
You pump messages by having a function that looks something like this (this is not the recommended method of solving your problem)
<br />
void PumpMessages()<br />
{<br />
MSG msg;<br />
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))<br />
{<br />
TranslateMessage(&msg);<br />
DispatchMessage(&msg);<br />
}<br />
}<br />
<br />
while(1)<br />
{<br />
DoSomething();<br />
PumpMessages();<br />
}<br />
|
|
|
|
|
You should pool the serial port, but insert small pauses on your loop like:
Sleep(10)
... and only then read it continuously. If there aren't any pauses, other programs, threads and the system itself don't have the opportunity to run properly.
|
|
|
|
|
Hi God,
How does it help in exiting the application? It is not responding to the STOP button click. If I add sleep(10), will it respond?
Thanking you,
vka
|
|
|
|
|
Oh, I see. In fact you may need 2 threads.
Let's assume your function is called "SerialPortPoolFunction"
<br />
int SerialPortPoolFunction(void *Parameter)<br />
{<br />
HANDLE Event=(HANDLE)Parameter; <br />
for ( ;; ) <br />
{<br />
<br />
int i=WaitForSingleObject(Event,10);
if (i==WAIT_OBJECT_0)
return 0; <br />
}<br />
}<br />
<br />
<br />
<br />
<br />
<br />
HANDLE hEvent=CreateEvent(NULL,FALSE,FALSE,NULL);
<br />
DWORD id;<br />
HANDLE hThread=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)SerialPortPoolFunction, <br />
(void *)hEvent, 0, &id);<br />
}<br />
When the user presses the "Stop" button, you may execute: PulseEvent(hEvent);
So the WaitForSingleObject inside the SerialPortFunction is supposed to return WAIT_OBJECT_0, because you just signaled it. And the thread returns.
Hope that I could help w/o too many bugs. At least it gives the general idea.
|
|
|
|
|
PulseEvent is flawed, http://blogs.msdn.com/oldnewthing/archive/2005/01/05/346888.aspx[^].
As it assumes that there is a thread waiting for the event, and this is impossible to determine.
Anyway it looks like the event is simply just to tell the thread to stop polling, so a call to SetEvent(hEvent) is sufficient, and since bManualReset is FALSE, it automatically clears the event.
And you really should be matching the function of SerialPortPoolFunction to LPTHREAD_START_ROUTINE instead of casting it, most win32 projects default to cdecl and LPTHREAD_START_ROUTINE is stdcall.
|
|
|
|
|
Hi,Eveyone.
I want to load a bitmap into Button and PictureBox by Win32.Who know about this prolem,please help me.
Thanks !
|
|
|
|
|
HBITMAP bitmap;
HWND hButton;
HINSTANCE hInst;
...
...
bitmap=LoadBitmap (hInst,MAKEINTRESOURCE(IDB_BITMAP1));
hButton=CreateWindow("BUTTON","",WS_CHILD|WS_VISIBLE|BS_BITMAP,0,0,120,50,hWnd,NULL,hInst,NULL);
SendMessage(hButton,BM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)bitmap);
maybe it is some helpful to you
|
|
|
|
|
I need someone who can help me...
the problem states..
input word: Ruby
output: ybuR
using stack operation pls....
|
|
|
|
|
we don't do homeworks here
|
|
|
|
|
push(R)
push(u)
push(b)
push(y)
pop(y)
pop(b)
pop(u)
pop(r)
VuNic
|
|
|
|
|
Hello everyone!
Alright... I need someone to guide me here... I'm makig a networked game, and I need a cross-platform (Win32 and Linux mostly) socket library with an easy-to-follow tutorial for that library, because I never used sockets before, and I'm kind of afraid of them... I've been looking at one called DataReel[^], but I couldn't find any tutorials for it... Can anyone help me? Thanks!
Lord Kixdemp
www.SulfurMidis.com
www.SulfurSoft.tk
[ftp://][http://][hotline://]tsfc.ath.cx
|
|
|
|
|
Try to see CSocket on MSDN. It's very easy to use.
Good Luck
We Believe in Excellence
www.aqueelmirza.cjb.net
|
|
|
|
|
|
|
crossplatform... see Beej's Guide to Network Programming Using Internet Sockets, http://beej.us/guide/bgnet/
|
|
|
|
|
Hi all,
I have a requirement to create an app that displays itself similar to how Trillian displays itself --- docking itself to the right had side of the desktop. My environment is Visual C++ (Visual Studio 2005).
I have searched the site for articles and found some relating to appbars and MFC but have had problems compiling. having said this i don't wish to use MFC controls or methods to do this.
Are there any known components that i can purchase to do this ? Or is there a way i achieve this natively through VS 2005?
Any help is much apreciated
10x
|
|
|
|
|
problem of compliling quadprog.m to generate quadprog.lib
when i use mcc -t -L ........
i need to use quadprog.m in c++ builder borland 6.0
am
|
|
|
|
|
ss2006 wrote: problem of compliling quadprog.m to generate quadprog.lib
when i use mcc -t -L ........
i need to use quadprog.m in c++ builder borland 6.0
check the matlab compiler manuals. I have not personally used it, though work with a few who have. You should be able to choose the option to compile the matlab formulas into a DLL. That DLL then can be used in borland or microsoft or any other windows program using any tutorial for using a DLL function.
http://www.mathworks.com/products/compiler/description4.html[^]
_________________________
Asu no koto o ieba, tenjo de nezumi ga warau.
Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
|
|
|
|
|
Does any one know how to write a tftp client program using udp
socket programming?
Cheyi
|
|
|
|
|
|
Thanks, I did but there was nothing on tftp.
|
|
|
|
|
Hi to everyone, I connect locally to SQL Server 2000, using ODBC, without
problem. The problem is that I cannot connect to the SQL Server via a LAN.
How can I connect to SQL Server from a second PC via a network (with ODBC)?
Thank you!!!
|
|
|
|