|
About the second solution I proposed (CWinThread derived class). Gee, you got it perfectly right--it will post messages to worker but not in reverse. Sorry.
So now I get the problem. (i)The worker thread should (ii) post (iii) user-defined messages to (iv) one of the main app windows. (v) But, even CMainFrame doesn't trap user messages (vi) via ON_THREAD_MESSAGE. This is getting interesting by itself because the mistake is going to be obviously small and simple... Lemme give it a try on a real program rather than just think about it and get back to you later if something strikes.
-- Ajit
Your project today is your great-grandchildrens' "ancestor clock."
|
|
|
|
|
Hi,
The first solution does work. Graft the following code on the AppWizard-generated MDI app (accept all defaults).
Notes:
1. ON_THREAD_MESSAGE must appear exactly once for each User-Message in the whole project.
2. Synch support (not shown) is necessary. The simpler check on window handle, ::IsWindow( hWndAcceptingPost ), does not work. It leads to mem leaks.
In App.h
#define MYMESSAGE (WM_USER+7)
In Doc.cpp
static UINT MyThreadProc( void* pParam );
UINT MyThreadProc( void* pParam )
{
HWND hWnd = (HWND) pParam;
int nIters = 10;
for( int i = 0; i < nIters; i++ )
{
::Sleep( 500 );
CString* pS = new CString;
pS->Format( "Done %d of %d", i, nIters );
::PostMessage( pH->m_hWnd, MYMESSAGE, (WPARAM)i, (LPARAM) pS );
}
return 0;
}
void CMyDoc::OnLaunchWorkerThread()
{
CWnd* pWnd = ::AfxGetMainWnd();
HWND hWnd = pWnd->GetSafeHwnd();
::AfxBeginThread( MyThreadProc, (void*) hWnd );
}
In MainFrame.h
protected:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnMyMessage(WPARAM wParam, LPARAM lParam);
DECLARE_MESSAGE_MAP()
In MainFrame.cpp
BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
ON_WM_CREATE()
ON_THREAD_MESSAGE(MYMESSAGE, OnMyMessage)
END_MESSAGE_MAP()
void CMainFrame::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
CString* pS = (CString*) lParam;
TRACE( "Got (%d) %s\n", (int)wParam, *pS );
delete pS;
pS = 0;
}
Your project today is your great-grandchildrens' "ancestor clock."
|
|
|
|
|
bonjour! thanks for your answer. now it works. i think there was a little problem with my PostMessage function. i used PostMessage(...) instead of ::PostMessage(...). PostMessage didn't work.
I wanted to access my CDocument. Now i've moved the code for my thread in a new class. This class gets a pointer to my CDocument. The thread now launches a dialog which shows a litte progress bar. this dialog is initialized with my CDocument pointer and receives the messages of my thread. I hope the Dialog can now savely access my Cdocument data.
bb benedikt
|
|
|
|
|
hey der,
Is the message handler working properly if you call it from the main thread?
If it is, then I would run the Spy tool to make sure that the message is being sent and then you can look at the parameters in the message to see that they are correct. Nothing but fun fun fun!;)
|
|
|
|
|
thx for your reply. the message handler works but the spy tool doesn't receive my message. (i think)
|
|
|
|
|
Hi all,
I have this problem:
I need to make a program that, when runned for the first time, creates a shortcut icon on the desktop and a group in Start menu.
How it is possible to do this ? What are the api to call??
Thanks all for any help...
Carlo Comino
|
|
|
|
|
You want to use the method: SHGetNewLinkInfo
BOOL SHGetNewLinkInfo(
LPCTSTR pszLinkTo,
LPCTSTR pszDir,
LPTSTR pszName,
BOOL *pfMustCopy,
UINT uFlags
);
Cheers,
-Erik
|
|
|
|
|
I need to have two binary dll's in my application as resources. I then will need to be able to copy them out to the hard drive and save them.
How do i get a handle to the resource (not using MFC is best) and what should i then do with it?
I'm doing this so i can include some of the MFC dll's in an setup application that are needed to run a different app.
This would really help me,
any code/suggestions Please
|
|
|
|
|
Does anybody know how to change the height of a combo-box? The "MeasureItem" function can only change the height of the items inside the combo.
thanks,
Bruno
|
|
|
|
|
please can you explain me what are threads and what are processes and
what is the difference between the two ?
thank you a lot
|
|
|
|
|
This won't be a very in depth answer, for more information check out "Advanced Windows" by Jeffrey Richter.
A process is an instance of a running program. You start a program, it will run in a seperate "process". A process must own at least one thread to execute the code in its process.
A thread runs within a process. A process can hold many threads, but it has to have at least one. A thread is what actually executes the code in a process.
I know this is very brief, but I hope it helps a little.
Wayne
|
|
|
|
|
thanks, it helped a lot ...
so process is running program
each thread have its own registers like ax,bx,CS:IP. Right ?
Use threads the same memory - memory of process ???
can one process access threads of another process?
one more question: is it ok to create around 20 threads/process - for example each thread for each wininet download ???
|
|
|
|
|
A good source would be Joseph M. Newcomer's articles on this site, under General Category Threads Processes & IPC.
Scott!
Put the big rocks in the glass jar first!
|
|
|
|
|
"... so process is running program each thread have its own registers like ax,bx,CS:IP. Right ?"
Not my expertise, but I think so, yes.
"Use threads the same memory - memory of process ???"
Same EXE image, yes. Global variables/objects is also shared between threads within a process. Locally defined variables/objects within a thread are not shared between threads.
"can one process access threads of another process?"
Don't think so.
"one more question: is it ok to create around 20 threads/process - for example each thread for each wininet download ???"
Yes, web servers usually contain many more threads than 20. Our company has software running 50+ threads also, a lot being threads with different tasks. Word uses a separate thread to print documents too, for example.
|
|
|
|
|
A process is a thread container. The process defines the memory context in which 1 or more threads run. You can't have a process without at least one thread.
|
|
|
|
|
thanks a lot everybody, you really helped me!
|
|
|
|
|
I'm using VC++ to implement a program involving drawing.
CABCDoc* pDoc = GetDocument();
*
*
*
GetDocument()->UpdateAllViews(0); //I use this line as "clearscreen" in the "View" file
In the Drawing function (in seperate file):
void drawing()
for (;;)
{
::receive message from outside (peekmessage()....etc...)
CDCPointer->MoveTo(x,y);
CDCPointer->LineTo(x,y); //draw a dot
if (stop)
break;
}
I use a loop to let the program keep receive message and do drawing. If I use the UpdateAllViews(), nothing special. But after that when I want to terminate this drawing function, error messagebox shown:
"Debug Assertion Failure!
Program: c:\ABC......\ABC.exe
File: Wingdi.cpp
Line: 557
For information.........."
If I didn't use UpdateAllViews(), no problem at all......but if I exit the function and want to do it again....I have to clear the screen first, right?
I can use the UpdateAllViews() anywhere to do the clearscreen.......can anyone help me?
|
|
|
|
|
More information please, where exactly are you calling UpdateAllViews()...
Wayne
|
|
|
|
|
Actually, I do drawing by the touchscreen. So I have to receive the message from touchscreen, then do drawing. So I use;
First:
In CABCView.cpp
WindowsProc(...)
{
get the message and information from touchscreen (get x and y)
if (alignment finished)
GetDocument()->UpdateAllViews(); //if i finished doing the alignment, clear the screen and
//start doing the drawing....***problem is in here***....
}
Second:
OnDraw()
{
.....call the drawing function in drawing.cpp
}
In drawing.cpp
Third:
DrawDot()
{
for ( ; ; )
{
if (alignment == true) //doing the alignment first (print out 4 points to let user touch first)
{ start doing thing.........CDCPointer->MoveTo(x,y)..etc....
if (stop)
break;
}
}
}
So the process is liked that: Start DrawDot(), then do alignment, receive points info., clear screen, start drawing........
If I didn't clear screen after alignment, that's ok...and no error message shown after i leave the function. However, if i run the function again, nothing shown and i try hard to stop the function and then show the same error message...( i think it is because after i exit the function, i use UpdateAllViews() to clear the screen....then when i go back to this function.....same problem....)
If I did clear screen after alignment, error message will show right after i leave the function and terminate the whole program. Also, when i do the drawing, the output is different compared to those i didn't clear screen after alignment. I am drawing downward...but on the screen is going up....something liked that.......
Hope this information can help you.
|
|
|
|
|
If I search a file with:
CFileFind fFind;
find.FindFile("*.bmp");
find.GetFilePath();
the programm crashes.
MSDN says that I must call FindNextFile() at least once, but then I get the path of the second file and not of the first.
So, how can I get the path of the first file?
|
|
|
|
|
There seems to be a bug in the MFC class of CFileFind. Use instead FindNextFile() of the Win32 Platform SDK.
|
|
|
|
|
No, there is no bug. You have to call FindNextFile at least once just like the documentation states.
Basically this is what you do.
CFileFind fFind;
BOOL bFound = fFind.FindFile( "*.bmp" );
while ( bFound )
{
bFound = fFind.FindNextFile();
// Do what you want with fFind, it is pointing to the first found file.
}
Hope that helps
Wayne
|
|
|
|
|
Dear all,
I want to create button at run time, but, I don't know how many button I will create at
Run-Time. It decide by database. So please tell me how to do it ??
I can create it when I know how many button I will create. Just like :
add "#defint MyButton1 1111" , "#define MyButton2 1112", ect ....
and add "CButton m_Button1", "CButton m_Button2", .....
then m_Button1.Create(), m_Button2.Create(), ......
But that not I want, if at Run-Time, there have 100 button to create, then ..... >_<
Please help me, thanks .....................
|
|
|
|
|
Basically, you should have a CButton * in your class, and create it to be an array of buttons. Given them ID's starting from a specific value, then you can use PreTranslateMessage to catch which button has been pressed.
Christian
The content of this post is not necessarily the opinion of my yadda yadda yadda.
To understand recursion, we must first understand recursion.
|
|
|
|
|
Sorry,
I don't understand how to create it to be an array ....
can u teach me ??
thanks
|
|
|
|