|
OK. Got the picture almost fully.
Just one more part. Guess you have to look at how the message map entries for the UWM_PROGRESSDELETE look like in the project (not just in the Doc).
Since CDocument is not a CWnd (and also not a CWinThread), MFC will NOT find a way to directly route UWM_PROGRESSDELETE to CUserManagerDoc.
Therefore, you might have to add ON_THREAD_MESSAGE entries in CMyView or CMyFrameWnd where MFC will route these messages. Then, you could arrange for MFC to forward those to CDocument....
I would probably go for a more lengthy but a very straightforward and clean solution as following.
Derive a CMyWorkerThread from CWinThread class, and add ON_THREAD_MESSAGE entries for PROGRESSDELETE in CMyWorkerThread class, *not* in the CUserManagerDoc class.
Have a CMyWorkerThread* as m_pThread in Doc. Instantiate CMyWorkerThread on the heap in a doc function, and then call m_pThread->Create() to get the worker thread going. You can also cache the doc ptr as a member variable into the CMyWorkerThread class. If so, ensure you say
m_pThread->m_pCreatingDoc = this;
before calling m_pThread->Create().
To use: In CUserManagerDoc, call
m_pThread->PostThreadMessage( UWM_PROGRESSDELETE, 0, (LPARAM) pData );
To access Doc from CMyWorkerThread handler for ProgressDelete(), just use the cached ptr. (Some multi-threading synch object might be necessary to ensure m_pDoc in CMyWorkerThread is valid.)
This will always work. See if it is suitable for your needs.
-- Ajit
Your project today is your great-grandchildrens' "ancestor clock."
|
|
|
|
|
thanks for your detailed answer. i tried it with the first way you mentioned. I added the necessary message functions in my CMainFrame class. The message map handels the messages via ON_THREAD_MESSAGE(UWM_PROGRESSDELETE, OnProgressDelete) and ON_THREAD_MESSAGE(UWM_PROGRESSCREATE, OnProgressCreate). The CDocument function which stores the thread retrieves a window handle via m_pWnd = (HWND)AfxGetApp()->m_pMainWnd.
My thread function calls the function:
CString* s = new CString(cstrName);
PostMessage(m_pWnd,UWM_PROGRESSDELETE,0,(LPARAM)s);
but my message handlers are never called.
i think i don't really understand your second solution. creating the CMyWorkerThread is clear. caching the doc ptr too. but i'd like to send a message to something where i can savely make some changes on my user interface. i think you mentioned how to send messages into the running thread and not vice versa. or do i really understand nothing...
|
|
|
|
|
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 .....................
|
|
|
|
|