|
How about the following:
AfxGetMainWnd()->PostMessage(WM_COMMAND,ID_FILE_NEW);
|
|
|
|
|
|
I have a problem at the middle of my work. I don't want to restart from the beginning.
Right now, I am writting a software in C++ with MFC. I use the Message MAP builded by the Wizard. I don't want to tell what kind of software I do because it is confidential. This is a tool used on PC, which communicate with an embedded system.
I don't need a real time software, the time is not critical. We also want something friendly. That is why my interface is Window. At this time, everything work, for the futur, I see a problem.
The problem is the following:
-----------------------------
When a process is currently running (not finish), I want to exit from my function, do something else, and come back to see if I have the result of that process or exit if the timeout is elapsed. To do so, I need to Dispatch the Message
by myself like the old method with the Windows Procedure and the switch case with different messages. I know very well how to do it but now, I have a software with a message map. Bad or good decision, I am not sure at this time.
My solution is to restart a new project with old fashion and control all message with the switch case (no message map). I can also recuperate all my files.
Other problem, the old method use HANDLE, MFC doesn't.
Is there something I don't understand ????
Please, help !
Danny
|
|
|
|
|
MFC map is the switch statment just add your message to it
|
|
|
|
|
I am not sure what the problem is but there should be an easy solution with mfc. Can you execute your function in a seperate thread?
If you want the old switch / case all you have to do is override WindowProc(). I did this in a directdraw application.
Danny Gilbert wrote:
Other problem, the old method use HANDLE, MFC doesn't.
What do you mean here. In most cases in MFC you have access to the handle.
John
|
|
|
|
|
You can make your own message pump wherever you want with this code:
if(::PeekMessage(&msg,m_hWnd,0,0,PM_REMOVE))<br />
{<br />
::TranslateMessage(&msg);<br />
::DispatchMessage(&msg);<br />
}
to check for an event or message inside your function and the come back to your process.
Daniel Cespedes
"Santa Cruz de la Sierra Paraiso Terrenal!"
daniel.cespedes@ieee.org
|
|
|
|
|
If I understand, you says that I can stay in my process and call (for exemple at each loop) the PeekMessage, Translate and Dispatch Message functions !!!
So, Windows will execute the function (like refreshing the screen, etc.) and always come back at the caller, in my process.
Is it what you explain ?
|
|
|
|
|
exactly!!
Daniel Cespedes
"Santa Cruz de la Sierra Paraiso Terrenal!"
daniel.cespedes@ieee.org
|
|
|
|
|
God, you reminded me of doing background processing in Win16. Shame on you!! It took me a long time to forget!!
onwards and upwards...
|
|
|
|
|
well it works fine in my codes, and I have taken the code from Programming Visual C++ fifth Edition from Kruglinsky, a rather new book.
Is a method that sometimes makes multithreaded programming unnecessary.
greetings.
Daniel Cespedes
"Santa Cruz de la Sierra Paraiso Terrenal!"
daniel.cespedes@ieee.org
|
|
|
|
|
Multithreading does have its problems besides the difficulty with synchronizing shared objects, its harder to debug, and MFC doc / view does not support multithreading. However after you get over a few hurdles it is usually better to use unless you only have one background task.
John
|
|
|
|
|
I didn´t know that MFC doc/view doesn´t support multithreading, so if I want to use it I have to make a Dialog based app?
cheers!
Daniel Cespedes
"Santa Cruz de la Sierra Paraiso Terrenal!"
daniel.cespedes@ieee.org
|
|
|
|
|
John M. Drescher wrote:
MFC doc / view does not support multithreading
I didn´t know that MFC doc/view doesn´t support multithreading, so if I want to use it I have to make a Dialog based app?
cheers!
Daniel Cespedes
"Santa Cruz de la Sierra Paraiso Terrenal!"
daniel.cespedes@ieee.org
|
|
|
|
|
No you just have to make sure that you create your views in the same thread as the doc and the main application otherwise you will see a lot of asserts...
John
|
|
|
|
|
I was thinking the same...
John
|
|
|
|
|
How would I go about to solve this problem?
I have to write a text analysis program that will calculate and output the overall number of words, characters, and lines within a text file. Also, have to calculate how many characters are alphabetic, numeric, and punctuation.
It has to be in C++. Thanks.
Sample Output:
The text file consisted of 2 lines
The text file consisted of 27 words
The text file consisted of 128 characters
Alphabetic: 100
Numeric: 12
Punctuation: 8
Other: 8
Mark J Jackson
|
|
|
|
|
Use the following functions from <ctype.h>
isalpha
isdigit
ispunct
isspace
|
|
|
|
|
CString strLine;
CStdioFile file(...);
int nLineCount = 0,
nAlphaCount = 0;
while (file.ReadString(strLine) == TRUE)
{
nLineCount++;
for (x = 0; x < strLine.GetLength(); x++)
{
if (isalpha(strLine[x]))
{
nAlphaCount++;
}
else if (isdigit(strLine[x]))
{
nDigitCount++;
}
...
}
}
// to count characters (or count them above as each character is analyzed)
int nCharCount = file.GetLength();
|
|
|
|
|
Hi everyone,
I have a little question about thread. I have read the faq and managed to program threads but now I'm facing a newbie challenge : I need to access to my dialog controls and the Document. I copy this code from this forum to access the Document from anywhere in my program.
//Declaration
static CTestSDIDoc * GetDoc();
//Implementation
CTestSDIDoc * CTestSDIDoc::GetDoc()
{
CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd);
return (CTestSDIDoc *) pFrame->GetActiveDocument();
}
In my dialog (my own progress window with static text, a progress bar, 6 bitmaps and an OK button), I want to show a green check mark when a process has done is job. The process is created with CreateProcess method. The CreateProcess is writed in the TreadFunc of my dialog class. Before calling createprocess method, I must do some initialization in the Document so I must do this :
metprof *metProfPtr = static_cast<metprof *="">(pvParam);
CTestSDIDoc *pDoc = static_cast<ctestsdidoc *="">(pvParam);
pDoc=CTestSDIDoc::GetDoc();
The bold text code works everywhere except in the TreadFunc method (I've got an assertion when running in debug mode). I want to know what Am I doing wrong ?
Here my complete ThreadFunc function:
UINT metprof::ThreadFunc(LPVOID pvParam)
{
metprof *metProfPtr = static_cast<metprof *="">(pvParam);
CTestSDIDoc *pDoc = static_cast<ctestsdidoc *="">(pvParam);
CString workingDirectory,strTemp,appArguments,strTemp2;
pDoc=CTestSDIDoc::GetDoc();
TRACE("vis: %d\n",pDoc->m_VIS);
workingDirectory=pDoc->m_modulesDirectory;
workingDirectory.Insert(workingDirectory.GetLength(),"ATMOSPHERIC\\\\");
pDoc->m_SL_COND=1;
appArguments=pDoc->m_dosPrompt;
pDoc->createMetDataFile();
pDoc->createMetProfInputFiles();
STARTUPINFO siStartupInfo;
PROCESS_INFORMATION piProcessInfo;
memset(&siStartupInfo, 0, sizeof(siStartupInfo));
memset(&piProcessInfo, 0, sizeof(piProcessInfo));
siStartupInfo.cb = sizeof(siStartupInfo);
strTemp2=pDoc->m_binDirectory;
strTemp2.Replace("\\\\","\\");
strTemp2.Delete(strTemp2.GetLength()-1,1);
strTemp.Format(" /c \"metprof.exe < metprof.rep -L -P \"%s\"\"",strTemp2);
appArguments.Insert(appArguments.GetLength(),strTemp);
char *str = appArguments.GetBuffer(appArguments.GetLength());
if(CreateProcess(NULL, // Application name
str,//"cmd /c \"metprof.exe < metprof.rep -L\"",// Application arguments
0,
0,
FALSE,
CREATE_NO_WINDOW,
0,
workingDirectory,//"C:\\Program Files\\Irblempkg\\IRBLEM4.1\\modules\\atmospheric\\", // Working directory
&siStartupInfo,
&piProcessInfo)){}
::WaitForSingleObject(piProcessInfo.hProcess,INFINITE);
metProfPtr->GetDlgItem(IDC_CHECKMETEO)->ShowWindow(TRUE);
pDoc->moveMetProfOutputFiles();
return 0;
}
I think I'm missing something with my code but I don't know what.
Thanks for your help,
Rene
|
|
|
|
|
Yeah, you are missing the bold text . Which part of the code does not work?
|
|
|
|
|
Could you explain little bit more what is the following code does?
metprof *metProfPtr = static_cast(pvParam);
CTestSDIDoc *pDoc = static_cast(pvParam);
P.S. I assume "<" and ">" are lost due to the editor.
|
|
|
|
|
Hi AlexO,
metprof *metProfPtr = static_cast<<metprof*>>(pvParam);
CTestSDIDoc *pDoc = static_cast<<ctestsdidoc*>>(pvParam);
These are pointers to metProf dialog class to access dialog controls like editBox etc...
The second one is a pointer too but to the Document.
If I run my program, I got an assertion but if I click on ignore button the program continues to work correctly.
Thanks for your help,
Rene
|
|
|
|
|
that is what I thought ... But it is the same pointer is it not?
As for pointers, just an idea, would it not be safer to pass HWND of the dialog and use PostMessage for notification from the worker thread?
|
|
|
|
|
You can't use MFC windows in multiple threads. Since CFrameWnd::GetActiveDocument() is MFC specific (not a Win32 function) you're pretty much screwed. You'll need to either pass the document as a parameter to your thread function, or find another way of doing it. MFC is just no good for applications where multiple threads manipulate the GUI.
Ryan
Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"
|
|
|
|
|
Pass all the data the thread will need as the parameter:
struct ThreadData
{
CTestSDIDoc *pDoc;
metprof *metProfPtr;
.
.
.
HWND hWnd;
};
.
.
.
ThreadData *td = new ThreadData
td.pDoc = CTestSDIDoc::GetDoc();
td.metProfPtr = ...
CWinThread *pThread = AfxBeginThread(ThreadProc, (LPVOID)td);
.
.
.
UINT ThreadProc(LPVOID lpParam)
{
ThreadData *td = (ThreadData *)lpParam;
.
.
.
delete td;
return 0;
} Also, you don't need a thread just to create a proccess.
Hope this helps...
Jonathan Craig
www.mcw-tech.com
|
|
|
|