|
I have a memory leak and think I have found it. So my question is whats the best way to terminate this thread.
The final tidy-up section of code is never reached.
The thread reads UDP data packets and updates a database; but on my main program exit routine should I kill this thread? As could be waiting on a read.
while(g_bUDP_Socket_Created)<br />
{<br />
int len = sizeof(RxBuffer);<br />
bytesRead = recvfrom (conn_socket, (char *)&RxBuffer, sizeof(Data), 0, (struct sockaddr *)&from, &length);<br />
if (bytesRead == SOCKET_ERROR) <br />
{<br />
g_sFormatting.Format("recvfrom() failed: %d\n",WSAGetLastError());<br />
AfxMessageBox(g_sFormatting);<br />
WSACleanup();<br />
return -1;<br />
}<br />
::EnterCriticalSection(&g_csRxLock);
memcpy(&g_RxData, &RxBuffer, sizeof(Data));<br />
UpdateDataBase();<br />
::LeaveCriticalSection(&g_csRxLock);
g_nUDPCounter++;<br />
}<br />
closesocket(conn_socket);<br />
WSACleanup();<br />
return 0;
|
|
|
|
|
Seems to me you should be using an asynchronous 'unblocked' socket. Your thread should wait on an exit event and a socket event (2 events) with WaitForMultipleObejct, and only issue the socket read if socket data is ready. Otherwisee you WILL just sit there until data arrives.
See WSAEventSelect:
"As in the case of the select and WSAAsyncSelect functions, WSAEventSelect will frequently be used to determine when a data transfer operation (send or recv) can be issued with the expectation of immediate success. Nevertheless, a robust application must be prepared for the possibility that the event object is set and it issues a Windows Sockets call that returns WSAEWOULDBLOCK immediately. For example, the following sequence of operations is possible:"
Then, your application main thread can set the event for the thread to exit, and it should not be otherwise 'blocked' waiting for data reception on the socket.
|
|
|
|
|
Thanks for your post.
My application is already using events and will use more in the future. I am lead to think that you have a total of 32 events.
So do not want to use 2 events for this UDP activity.
Are they a crude way of ending this thread when the application ends? I will sort it out later if they are any events spare.
I do not have a handle for this thread, created via the call:-
m_UDPReceiverThread = ::AfxBeginThread(ExampleDlg::UDPReceiverThread, NULL);
|
|
|
|
|
1. You can have more than 32 events in your progrma, I just don't think you can WAIT for more than 32 in any particular call to WaitForMultipleObjects.
2. The receiving thread should wait on an event from the sockets AND an event that the main applciation thread would set when it is time to exit. If the soket event is set, then read data, if the exit event is set, then leave your thread function by returning or exiiting out of bottom of function. That is the preferred mechanism for ending the thread, not terminating it.
There is another message in this forum a little older than yours asking how to end the thread safely, please read it. I was not trying to give guidance on existing the thread so much as how to avoid the blocking socket call.
|
|
|
|
|
Hi,
I'm trying to create a little program, wich basicaly reads a file. I already have written some code in the serializing-fuction, wich reads the data. I now would like to add a progress bar and some info-text in my window (CFormView). I don't know how to get data from the doc class to the view class. Has it something to do with messages? I'm new at all of this stuff, so its very confusing...
Thank's in advance!
|
|
|
|
|
You can simply use CDocument's GetFirstViewPosition() and GetNextView() to access the desired view.
An alternative for frame windos is calling AfxGetMainWnd()->GetActiveView() , which gives you access to the current view.
We are men. We are different. We have only one word for soap. We do not own candles. We have never seen anything of any value in a craft shop. We do not own magazines full of photographs of celebrities with their clothes on. - Steve
|
|
|
|
|
Plons wrote:
I don't know how to get data from the doc class to the view class.
After the document has successfully loaded the file, simply call UpdateAllViews() to notify the view that it needs to display new information. In the view's OnUpdate() method, it will ask the document (usually via GetDocument() and one other method) for the data to be displayed.
Check out the Scribble tutorial on MSDN. It goes over serialization, and doc/view in great detail.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
Hello Friends,
How can we retrieve an Handle of a View. I am using Splitter Window where my window is splitted into 5 parts. and I want to retrieve an handle for any of the view which ever I need. Please help me
Thank you,
Neelesh Jain.
|
|
|
|
|
I'm assuming you've made five calls to CreateView() . To access one of the views, just call GetPane() .
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
Hi Jain
Sorry for the delay
Here i do consider that i am having two panes ie two views, first the CCallManagerView(0,0) and the other CUserView(1,0). So when i do need to set some value to the CCallManagerView's control from CUserView, i'll be using the following code
void CUserView::SetValue2Manager(void)
{
CMainFrame *pMainFrame = (CMainFrame *) GetParentFrame();
CCallManagerView *pView = (CCallManagerView *)pMainFrame->m_wndSplitter.GetPane(0,0);
if (pView)
{
pView->myEdit.SetWindowText("Hai From Call Manager");
}
}
Hope this may help you. If you want the CUserView then use GetPane(1,0)
Sujan
|
|
|
|
|
I need to print out some image that I have in my MFC program. I can print text byt NOT images is there anyone that now's how do make that happend?.
|
|
|
|
|
|
I need help on writing a function that would implement the reliefing effect and the bathroom mirror effect of an image.
|
|
|
|
|
I've a problem , I would like know how can I hidden my app from tal list Windows? For example: when a user to press ctr+alt+del it's not show my app in tag list? Thanks
|
|
|
|
|
Gledston Reis wrote:
...when a user to press ctr+alt+del it's not show my app in tag list?
Do you mean Task Manager's list of processes? Just out of curiosity, why would you not want your application to show up in this list?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
DavidCrow wrote:
Just out of curiosity, why would you not want your application to show up in this list?
He's writing spyware that he does not want the user to know about. If the user knows about it, the user can kill it.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04
Within you lies the power for good - Use it!
|
|
|
|
|
I know that. I just wanted to see if GR would admit to it.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
I´m sorry because I dont answer before I was too busy;). Yeah you can show it(hidden in tag list and process list). I written a spyware and my app need stay hidden in system.
thanks!
PS:Sorry my bad english, because it´s not my natural language
|
|
|
|
|
i want to add a menu in cd rom's context menu.
using vc++.
plese help me.
very urgent.
|
|
|
|
|
Just as an example, you can add the following to a .reg file and merge it into your registry. Then from Explorer, right-click on your CD drive containing an audio CD and you'll see the newly added option.
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\AudioCD\shell\calc]
@="&Calc"
[HKEY_CLASSES_ROOT\AudioCD\shell\calc\command]
@="calc.exe" For non-audio CDs, a minor change:
[HKEY_CLASSES_ROOT\Drive\shell\paint]
@="&Paint"
[HKEY_CLASSES_ROOT\Drive\shell\paint\command]
@="mspaint.exe"
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
i simply typed following in file and named it with .reg extention.and i runed it. it showed an error.
why?
what is format for registry file?
i maked entry directly in registry in following path.
[HKEY_CLASSES_ROOT\Drive\shell\paint]@="&Paint" [HKEY_CLASSES_ROOT\Drive\shell\paint\command]@="mspaint.exe
but the menu also appears when right click a drive ( for example c . i want only in cd-rom's context menu.
|
|
|
|
|
anandforu wrote:
i simply typed following in file and named it with .reg extention.and i runed it. it showed an error.
why?
what is format for registry file?
You missed the following at the top of the file:
Windows Registry Editor Version 5.00
anandforu wrote:
but the menu also appears when right click a drive ( for example c. i want only in cd-rom's context menu.
I currently do not know of a solution for this. Perhaps Michael Dunn can help.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
i want to add windows media player activex control in vc++.
how to do it?
i added the windows media player activex control.But when i calling a method in it ,an runtime error occurs.No details in that error.
please help me .very urgent.
Anand
|
|
|
|
|
I am launching off a console app (by pressing a button )using CreateProcess. I save the handle to this process and when I press the button again, I want to terminate the process if its already running and relaunch it. I put in debugging statements and indeed I get the expected correct output, but the console window is nowhere to be seen this second time around!
if(m_serverProcessInfo.hProcess)
{
TerminateProcess(m_serverProcessInfo.hProcess, 0);
if(fpLog)fprintf(fpLog, "Terminated server\n");
m_serverProcessInfo.hProcess = 0;
}
int res = StartServer();
and
int CPMA2000Dlg::StartServer()
{
int res = 0;
STARTUPINFO StartupInfo;
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
StartupInfo.cb = sizeof StartupInfo ;
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow = SW_SHOW;
if(CreateProcess( m_sPath + "fileserver.exe", NULL,
NULL,NULL,FALSE,0,NULL,
NULL,&StartupInfo,&m_serverProcessInfo))
{
if(fpLog)fprintf(fpLog, "Started server\n");
}
My logs:
Started server<br />
Terminated server<br />
Started server<br />
The first time I do see the console window (server.exe) but the second time though CreateProcess() succeeds again, I dont see the console window which disappeared at the TerminateProcess().
How shall I proceed to figure out the problem?
Thanks,
sb
|
|
|
|
|
Are you doing this in order to have a single instance of the application?
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)
|
|
|
|