|
memcpy() is slightly quicker because it doesn't do buffer overlap checks, and the compiler is more likely to inline it because the code is a fair bit smaller.
Ryan "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"
|
|
|
|
|
OH! I'm stuck!
I have no idea what to do !!!
I created a dialog based application (as a matter of fact, my first REAL application).
from the main window I created another option window, over there the user can change the variables (just strings...) to custom his work.
the thing is that when the user will close the application all of his settings in the option window, obviously, will be deleted!
now, what I want to do is to let the user somehow the ability to save his
settings (the variables), and load them into a default file(for the example: "settings.dat" (without using an "open/save file" dialog from the CFileDialog class). in my quest to find the answer I read about serialization, but it really didn't do the job very well... I kept on getting errors and stuff (even by only declaring serialize in COption)
so my plan is: in the OnInit I will check IF the "settings.dat" exist (and I don't really know how to do that), if not, it will be created with default settings (I guess that is just saving, and by that the file will be created),
and if it does exist, it will be loaded.
and in the option window, using:
if(clsOption.DoModal() == IDOK)
in the main dialog, I will save the settings of the options.
so if all I wrote in the above is not a complete bullshit (sorry for the language), technically what I don't know what to do is how to check if the "settings.dat" exists or not, and HOW DO I SAVE AND LOAD???
I know these are big questions, but at least some clue.
I'm REALLY STUCK!
THANKS EVERYBODY!!!
|
|
|
|
|
For lots of different settings, you should use serialization, xml, registry, or something else.
However, in your case, I think you probably just want to save and load a few settings and you are probably just starting the whole GUI business (thats what I understand you mean by "REAL application" ). In that case I would use simple text based input/output to a text file, separating the different settings by some kind of delimeter (ie. a comma, a semi-column, or something like ;mysupercustomdelimiter). When you save to a text file settings.dat for ex., you separate all your settings by a comma. When you load you look for a comma and separate them into different settings. For actual text input/output you could use fstream's (ifstream/ofstream) I think.
This is not a very good solution in general, but it is probably enough to get you rolling.
If somebody feels this is a terrible advice, feel free to correct me and point out a better, SIMPLE way of storing settings.
Get used to getting stuck! It happens on daily basis when programming, at least to me.
|
|
|
|
|
sounds like a simple idea that I can deal with
but how do I read from within the file (the text itself?) and write to that text?
|
|
|
|
|
|
I read about CFile, CArchive and CFileDialog.
I tried what I read about serializing objects (MSDN), but I keep on getting errors on the declaration.
I'm trying to change my approch to this problem and try and use a text file (what the other guy who helped me suggested).
the only thing I can't find is ow to make my app read the text from within the text file.
|
|
|
|
|
Is it safe to have one thread periodically writing to a variable on the heap while another thread is constanly reading that variable?
I have two threads, a worker and GUI.
The worker HAS to be in a loop writing data. Now I need some way of stopping that loop from withing the GUI thread.
I thought, have a common variable "state". Constantly check that variable from withing the worker to make sure it(the worker) should still be working. Whenever I need it to stop, the GUI thread can change the "state" variable to "STOP_STATE" or something among those lines and the next time the worker checks that variable, it stops.
Something tells me this is a very wrong way of syncronizing two threads. Could someone please point me in the right direction.
Thanks in advance,
Nick Z.
|
|
|
|
|
Your situation is a simple one, and the simple solution you propose should be fine. Just make sure that only ever one thread writes to the variable. As soon as multiple threads write to the variable, you'll have to add synchronisation (mutex, critical section etc...) to make sure that multiple writes don't happen concurrently.
Ryan "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"
|
|
|
|
|
For once something simple is the solution!
Thank you for clearing this up.
|
|
|
|
|
Additionally, the shared variable must be declared volatile , so that the compiler doesn't optimize away any reads/writes of the variable.
|
|
|
|
|
You should try using an event for this.
in the GUI thread:
<br />
m_hCancelEvent = CreateEvent(NULL, TRUE, FALSE, NULL);<br />
then pass the event handle to the worker thread in the AfxBeginThread call.
The GUI can then signal the worker (say via a Stop button) using:
<br />
SetEvent(m_hCancelEvent);<br />
and the worker can test for that signal in the thread procedure using WaitForSingleObject (or WaitForMultipleObjects if you're tracking multiple events)
<br />
if (::WaitForSingleObject(pYourThreadData->hCancelEvent, 0) == WAIT_OBJECT_0))<br />
else<br />
<br />
You need to close the event when your dialog closes:
<br />
CloseHandle(m_hCancelEvent);<br />
II)
If you need to read/write shared data between threads, it's best practice to use the appropriate sync object. This is a very simplified example.
<br />
static CCriticalSection g_criticalSection;<br />
static int* g_pSharedValue;<br />
In Thread 1:
<br />
CSingleLock singleLock(&g_criticalSection);<br />
<br />
singleLock.Lock();<br />
<br />
if (g_pSharedValue != NULL)<br />
{<br />
*g_pSharedValue += 8;<br />
<br />
*g_pSharedValue *= 2;<br />
}<br />
<br />
singleLock.Unlock();<br />
<br />
In Thread 2:
<br />
CSingleLock singleLock(&g_criticalSection);<br />
<br />
singleLock.Lock();<br />
<br />
delete g_pSharedValue;<br />
g_pSharedValue = NULL;<br />
<br />
singleLock.Unlock();<br />
<br />
Without the criticial section here, Thread 2 may execute the statement "delete g_pSharedValue", and then pass control to Thread 1. g_pSharedValue is corrupt, and causes Thread 1 to barf.
Pssst. You see that little light on your monitor? That's actually a government installed spy camera. Smile and wave to big brother!
|
|
|
|
|
Thank you for the excelent explanation.
I only need threading for a small portion of my program, and I didnt want to focus on the topic too much.
Your examples make my life so much easier. Thanks!
|
|
|
|
|
Hi !
I am compiling for UNICODE, a RichEditView in VC++ 6.0
A private variable prchars is defined in RichEditView Class.
In wordpad.h file
private:
char prchars[6];
********************************************************
In WordPadView.cpp in the constructor method I initialised the value
char prchars[0]='\0';
Now I am using
GetRichEditCtrl().ReplaceSel(prchars);
is giving an error
'ReplaceSel' : cannot convert parameter 1 from 'char [6]' to 'const unsigned short *'
even if i use
GetRichEditCtrl().ReplaceSel(_T(prchars)); it is not working. But it is working for constants in _T() ie.,
GetRichEditCtrl().ReplaceSel(_T("D"));
How can I use a character array variable prchars in the place of "D" ?
Thanks in advance.
Jai Srimannarayana
|
|
|
|
|
You have to replace the char declarations with TCHAR , like:
TCHAR prchars[0]='\0';
|
|
|
|
|
Thanks a lot. It worked fine.
" To distribute True knowledge to the right seeker is the best charity one can ever do is one's life. Blessed are those, for, they are specially loved by God "
Jai Srimannarayana
|
|
|
|
|
I Keep getting the following error when I try to compile my multithread code:
error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'UINT (LPVOID)'
Here is what I did:
I Declared my worker thread in my header file as such:
UINT Read(LPVOID Param);
This is the actual function as I have it in my code:
UINT CMyClass::Read(LPVOID Param)
{
CMyClass* pAppView= (CMyClass*)Param;
/*
*
*Code Here
*
*/
return 0;
}
And here is how I am calling the new Worker Thread from within my OnInitDialog() Function;
AfxBeginThread(Read,this);
I gathered all of this code from various articles on MFC Multithreading, but regardless I keep getting the error above. Can someone please tell me why The 'AfxBeginThread' statement is wrong? I have seen it done this way in the other articles.
Thanks
|
|
|
|
|
Hello,
You need to declare your worker thread function like at below;
UINT Read(LPVOID Param)
{
/*
*
*Code Here
*
*/
return 0;
}
Regards
Orkun GEDiK
SAP R/3 Software & System Support Specialist
ASTRON
|
|
|
|
|
Do you mean Declaring it in my header file like that?
If so, why? Wouldn't a simply deaclaration like such be enough:
UINT Read(LPVOID Param);
And if it does need to be a full function declaration like you said please explain to me why.
Thank you.
|
|
|
|
|
You can not call or pass the non-static member function of a class to AfxBeginThread.
You defined this:
UINT CMyClass::Read(LPVOID Param)
You need to make Read a stand-alone function.
Otherwise, in your header file, within the class, you need to declare it like this:
static UINT Read(LPVOID Param)
If it is declared as static, then
UINT CMyClass::Read(LPVOID Param)
is valid code.
|
|
|
|
|
did you ever heard of this pointer ?
when you write
UINT CMyClass::Read(LPVOID Param); the compiler understands
UINT CMyClass::Read(CMyClass* this, LPVOID Param);
so, the function header is not what you were expecting...
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
thank you all for the replies. The "static" keyword worked
|
|
|
|
|
If you convert your function in the header to be static, eg:
class CMyClass
{
....
static UINT Read (LPVOID Param);
....
BOOL StartMyThread ();
}
You can then do:
BOOL CMyClass:StartMyThread ()
{
AfxBeginThread (Read, this);
}
The trick is the static keyword.
If you don't put in the static, then your function is equivalent to:
UINT CMyClass_Read (CMyClass *this, LPVOID Param)
which is why the compiler is complaining.
Iain.
|
|
|
|
|
Hi!
I would like resize my Image. But I have a little problem, when I would like a bigger image:
My code:
//Bitmap* bitmap;-->in the Doc
//szel=original width, mag=original height
Bitmap* clone = pDoc->bitmap->Clone(0, 0, szel, mag, PixelFormat32bppRGB);
delete pDoc->bitmap;
pDoc->bitmap = clone->Clone(0, 0, Dialog.m_szelesseg, Dialog.m_magassag, PixelFormat32bppRGB);
Graphics graphics(pDoc->bitmap);
float a1=(float)Dialog.m_szelesseg/(float)szel;
float a2=(float)Dialog.m_magassag/(float)mag;
graphics.ScaleTransform(arany1, arany2, MatrixOrderAppend);
graphics.DrawImage(clone, 0, 0);
delete clone;
I have an error message when after this resize (bigger) I would like Save the bitmap, or redraw. What's the matter?
Please help!
The error message don't say nothing!
|
|
|
|
|
I have been working with my debug version of an ATL based COM server.
Unfortunately I cannot find a
Win32 Release configuration
In the the other configurations , file operations(fopen , fclose etc ) are invalid .
Is there a way I can specify a configuration similar to debug , but without the opimizations ?
Engineering is the effort !
|
|
|
|
|
My MSVC 6.0 MFC application crashes for a few people when they come out of StandBy mode when its running. I've never been able to reproduce this myself but I'm wondering what areas to look at that could cause a crash at this time. Here's the Dr. Watson log from the thread that crashes below. It looks at though the code just didn't get loaded again. Any ideas or suggestions on how to debug something like this?
*----> State Dump for Thread Id 0xec4 <----*<br />
<br />
eax=00000001 ebx=00ca2d00 ecx=0000000a edx=7c90eb94 esi=0669daa0 edi=00000000<br />
eip=0124b3c7 esp=0189ff60 ebp=0189ff80 iopl=0 nv up ei pl zr na po nc<br />
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000246<br />
<br />
function: <nosymbols><br />
No prior disassembly possible<br />
0124b3c7 ?? ???<br />
0124b3c9 ?? ???<br />
0124b3cb ?? ???<br />
0124b3cd ?? ???<br />
0124b3cf ?? ???<br />
0124b3d1 ?? ???<br />
0124b3d3 ?? ???<br />
0124b3d5 ?? ???<br />
0124b3d7 ?? ???<br />
FAULT ->0124b3c7 ?? ???<br />
Error 0x00000001<br />
0124b3c9 ?? ???<br />
0124b3cb ?? ???<br />
0124b3cd ?? ???<br />
0124b3cf ?? ???<br />
0124b3d1 ?? ???<br />
0124b3d3 ?? ???<br />
0124b3d5 ?? ???<br />
0124b3d7 ?? ???<br />
0124b3d9 ?? ???<br />
0124b3db ?? ???<br />
<br />
*----> Stack Back Trace <----*<br />
WARNING: Stack unwind information not available. Following frames may be wrong.<br />
ChildEBP RetAddr Args to Child <br />
0189ff5c 000d0670 00000218 0000000a 00000000 0x124b3c7<br />
0189ff80 77c3a3b0 00bda558 00000000 7c9105c8 0xd0670<br />
0189ffb4 7c80b50b 00ca2d00 00000000 7c9105c8 msvcrt!endthreadex+0xa9<br />
0189ffec 00000000 77c3a341 00ca2d00 00000000 kernel32!GetModuleFileNameA+0x1b4<br />
<br />
*----> Raw Stack Dump <----*<br />
000000000189ff60 70 06 0d 00 18 02 00 00 - 0a 00 00 00 00 00 00 00 p...............<br />
000000000189ff70 69 79 6d 0d 79 04 00 00 - 8b 01 00 00 58 a5 bd 00 iym.y.......X...<br />
000000000189ff80 b4 ff 89 01 b0 a3 c3 77 - 58 a5 bd 00 00 00 00 00 .......wX.......<br />
000000000189ff90 c8 05 91 7c 00 2d ca 00 - 05 00 00 c0 8c ff 89 01 ...|.-..........<br />
000000000189ffa0 84 fb 89 01 dc ff 89 01 - 94 5c c3 77 d8 40 c1 77 .........\.w.@.w<br />
000000000189ffb0 00 00 00 00 ec ff 89 01 - 0b b5 80 7c 00 2d ca 00 ...........|.-..<br />
000000000189ffc0 00 00 00 00 c8 05 91 7c - 00 2d ca 00 00 a0 fd 7f .......|.-......<br />
000000000189ffd0 05 00 00 c0 c0 ff 89 01 - 84 fb 89 01 ff ff ff ff ................<br />
000000000189ffe0 f3 99 83 7c 18 b5 80 7c - 00 00 00 00 00 00 00 00 ...|...|........<br />
000000000189fff0 00 00 00 00 41 a3 c3 77 - 00 2d ca 00 00 00 00 00 ....A..w.-......<br />
00000000018a0000 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................<br />
00000000018a0010 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................<br />
00000000018a0020 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................<br />
00000000018a0030 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................<br />
00000000018a0040 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................<br />
00000000018a0050 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................<br />
00000000018a0060 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................<br />
00000000018a0070 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................<br />
00000000018a0080 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................<br />
00000000018a0090 00 00 00 00 00 00 00 00 - 00 00 00 00 00 00 00 00 ................
|
|
|
|