|
Your app needs to be multithreaded, so the UI updates while your code is taking up lots of CPU. Otherwise, the paint messages are not handled, and your window is not drawn.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Can I look your code! May be I can make it better! and then sent it to you.
---++Xu Shanghua
|
|
|
|
|
You send a thread to Mr Christian Graus you can answer directly to sender question
|
|
|
|
|
I have not done multi-threading before but looking at the reference books and online tutorial, they are all related to classes or objects. I am only calling a C procedure with a parameter in it.
The function needs to be in this form. Do I have to convert my C-procedure to this format? Mine is of the format void decode(char *).
UINT MyThreadProc( LPVOID pParam ) // taken from MSDN
{
CMyObject* pObject = (CMyObject*)pParam;
if (pObject == NULL ||
!pObject->IsKindOf(RUNTIME_CLASS(CMyObject)))
return 1; // if pObject is not valid
// do something with 'pObject'
return 0; // thread completed successfully
}
Next will be to begin the thread:
pNewObject = new CMyObject;
AfxBeginThread(MyThreadProc, pNewObject);
Where do I put my C-procedure in the above code?
How do I know that the thread has ended?
Thank you.
searcher08
|
|
|
|
|
Any function that starts with Afx is part of MFC. You can write threaded code without using MFC, and therefore without this format.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Calling a function like this periodically (but not too often) during your computations should help:
<br />
int DoMessagePump()<br />
{<br />
BOOL bCompleteFlag=FALSE;<br />
MSG msg;<br />
BOOL bDoingBackgroundProcessing=TRUE;<br />
<br />
Sleep(1);<br />
<br />
while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {<br />
if (msg.message == WM_QUIT) {<br />
bDoingBackgroundProcessing=FALSE;<br />
::PostQuitMessage(0);<br />
break;<br />
}<br />
<br />
if (!AfxGetApp()->PreTranslateMessage(&msg)) {<br />
::TranslateMessage(&msg);<br />
::DispatchMessage(&msg);<br />
}<br />
}<br />
<br />
AfxGetApp()->OnIdle(0);
AfxGetApp()->OnIdle(1);
<br />
if (bDoingBackgroundProcessing == FALSE) return -1;<br />
if (bCompleteFlag == TRUE) return -1;<br />
<br />
return 0;<br />
}<br />
This code implements basic message passing (the Windows GUI is based on a cooperative multitasking approach, so your code has to "cooperate" LOL).
Christian is of course correct - The best way to do this is put your code into a different thread, but that sometimes adds a layer of complexity you don't always need.
Beware the calls to AfxGetApp()->OnIdle() - MFC needs this to "breathe" but it can result in lots of overhead in your computation. The more often you call DoMessagePump() the better the GUI response will be, but the longer your computation will take.
|
|
|
|
|
I would like to use your method instead of creating another thread. However, the computation procedures are in C and not in C++. I am afraid that the above function may not work in the C code as they are all in MFC. I am calling an extern C procedure from a C++ GUI code.
Thanks.
searcher08
|
|
|
|
|
can someone describe the simplest way to monitor a program, and whenever a function such as loadlibrary is called, have my program show a messagebox (or do something)
|
|
|
|
|
Do you need to see what files use in a program?
|
|
|
|
|
I'm trying to add more text to a field in a binary file, but I can't manage to make it work. I was hoping someone could point me to a tutorial on how to do this or give me tips, because I really can't find any infos on this.
|
|
|
|
|
It is impossible to append data to the middle of a file. The workaround however is to create a new file, write the data from the start of the old file to the point you want to append the data into the new file, write the new data into the new file, and then write the rest of the old file into the new file. Then delete the old file and rename the new file with the old file's file name.
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
That's what i tought, but how can I write the binary data to the new file without having the value of each field(what i mean is rewriting the hex value directly instead of writing the value of a string or an integer). I hope you understand what i'm saying here.
|
|
|
|
|
You need to read the entire file into memory and manipulate it there. Then you have all the values. You can store the file as a BYTE array, that seems to be what you're after.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Could you give me an example of how you do this. I tried to write it byte by byte, but i never get the same result as the original file. And is it possible using CFile.
|
|
|
|
|
What are you trying ? I'm sure CFile can do it, but I have no idea how, I don't use it, ever. I use standard C++ where-ever possible ( so, I use fstreams )
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
I think maybe i know why it never worked, I never managed to put it in a byte, only an unsigned char. What should i do to put it in a byte directly?
Also, I used CFile only because i never managed to copy japanese correctly with fstreams.
|
|
|
|
|
samkook wrote: What should i do to put it in a byte directly?
BYTE is a typedef for unsigned char, so, makes no difference.
samkook wrote: Also, I used CFile only because i never managed to copy japanese correctly with fstreams.
OK, well, sounds like you weren't using fstream properly either. A stream should not care if the bytes are japanese text, or a picture of the pope.
I think you need to post code, and wait for someone who uses CFile to step in.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
It would look something like this. It's only a test i made to read the first 10 bytes and them write it.
This code in particular doesn't work, because the second Cfile doesn't want to open and i really don't know why, but it's close enough to what i would do to write it.
private: System::Void mnuSave2_Click(System::Object * sender, System::EventArgs * e)
{
CFile fVampFile;
CFile fVampFileSav;
CString csFilename;
CString csFilenameSav;
unsigned char cByteBuff[1];
int iByteMove = 1;
unsigned char cByteArray[10];
if(odOuvrir->ShowDialog() == DialogResult::OK)
{
csFilename = odOuvrir->FileName;
if( !fVampFile.Open(csFilename, CFile::modeRead | CFile::shareDenyNone | CFile::typeBinary) )
{
MessageBox::Show(S"File probably in use");
}
try
{
fVampFile.SeekToBegin();
for (int iCtr = 0; iCtr < 10; iCtr++)
{
fVampFile.Read(cByteBuff,iByteMove);
cByteArray[iCtr] = *cByteBuff;
}
fVampFile.Close();
if(odSave->ShowDialog() == DialogResult::OK)
{
csFilenameSav = odSave->FileName;
}
if(fVampFileSav.Open(csFilenameSav,CFile::modeWrite | CFile::shareDenyNone | CFile::typeBinary ) )
{
fVampFileSav.Write(cByteArray,10);
}
}catch(...)
{
MessageBox::Show(S"Erreur lors de la lecture du fichier");
}__finally
{
fVampFileSav.Close();
eTexte->Text = "Done!";
}
}
}
};
|
|
|
|
|
You're using managed C++ AND MFC together ?
So long as you're using managed code, have you tried using a filestream ? If this is .NET 2.0, System.IO.File has static methods that will read and write an entire file in one line of code.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Yeah, i'm using both because i wanted to learn .net, but i'm too used to the good old ways.
And i also tried using a filestream(it was the first thing i tried), but i ended with the same result as the fstream.
|
|
|
|
|
samkook wrote: Yeah, i'm using both because i wanted to learn .net, but i'm too used to the good old ways.
IMO the end result is a disaster. Don't worry about the 'new ways', I don't think there's a real future for .NET in C++, certainly not mixed with MFC.
samkook wrote: And i also tried using a filestream(it was the first thing i tried), but i ended with the same result as the fstream.
if(fVampFileSav.Open(csFilenameSav, CFile::modeCreate|CFile::modeWrite | CFile::shareDenyNone | CFile::typeBinary ) )
I had a hard time making your code compile ( it relies on objects that were not there ) but adding CFile::modeCreate caused it to work for me. Otherwise, the file open for write failed altogether.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
samkook wrote: if( !fVampFile.Open(csFilename, CFile::modeRead | CFile::shareDenyNone | CFile::typeBinary) )
Your code is not wrong, but CFile objects open in binary mode by default.
samkook wrote: fVampFile.SeekToBegin();
This is redundant as the file pointer is already positioned at the beginning.
samkook wrote: for (int iCtr = 0; iCtr < 10; iCtr++)
{
fVampFile.Read(cByteBuff,iByteMove);
cByteArray[iCtr] = *cByteBuff;
}
Reading one byte at a time, disk I/O is going to be your enemy here. Try:
int nLength = fVampFile.GetLength();
LPBYTE pBuffer = new BYTE[nLength];
fVampFile.Read(pBuffer, nLength);
fVampFile.Close();
fVampFileSav.Write(pBuffer, nLength);
fVampFileSav.Close();
delete [] pBuffer; Once you get this working correctly, then you can add the code that inserts the new text;
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
hmmm, looks like it's working. I don't know what I was doing wrong before.
Thanks for your help guys.
-- modified at 12:52 Friday 13th October, 2006
Oh and btw, I don't have a choice(maybe i do, but it's easyer that way i think) to read it byte by byte because I need to identify some tags in the file.
-- modified at 12:54 Friday 13th October, 2006
|
|
|
|
|
samkook wrote: I don't have a choice(maybe i do, but it's easyer that way i think) to read it byte by byte because I need to identify some tags in the file.
This makes no sense. Using the code snippet I showed, you can look at each byte of lpBuffer . RAM is a whole lot faster than the disk.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Now that i think of it, your right. I shoudn't reply to message that need thinking when I wake up, it doesn't get good results.
|
|
|
|