|
Sorry about the delay, but here it goes...
Have a look at the example I sent earlier.
The inline function CMyClass::MyThreadFn( LPVOID pThis ) will not work unless I start the thread passing the this pointer according to
m_pMyThread = AfxBeginThread( MyThreadFn, this ) One of the things I want to avoid is type casting inside my thread controlling function. IMO the code will be more readable without it.
A few more tips if you like...
Usually when I spawn new threads there are two situations I want to handle before I unleash the new thread:
1. I want to be able to wait for the thread when it finishes and that requires that the CWinThread object has its m_bAutoDelete member set to FALSE. Otherwise the CWinThread object will free all resources including the thread handle which is used for waiting. I set the m_bAutoDelete member when I spawn the thread and that's why I create it suspended. In code it will look something like this:
m_pMyThread = AfxBeginThread( MyThreadFn, this, 0, 0, CREATE_SUSPENDED, NULL );
if( m_pMyThread )
{
m_pMyThread->m_bAutoDelete = FALSE;
m_pMyThread->ResumeThread();
} When I later stop the thread I wait for it to finish with some of the wait functions, preferrably MsgWaitForMultipleObjects since it allows me to pump messages preventing the GUI from being locked. Or when COM is involved, this thread usually needs to process COM related messages to avoid a deadlock situation. The waiting is accomplished by passing the thread handle as the "object" to wait for. The CWinThread object must also be freed with a call to delete. Something like this in code:
DWORD dwWaitResult;
BOOL bQuit = FALSE;
while( !bQuit )
{
dwWaitResult = ::MsgWaitForMultipleObjects( 1, &m_pMyThread->m_hThread, FALSE, INFINITE, QS_ALLINPUT );
switch( dwWaitResult )
{
case WAIT_OBJECT_0:
bQuit = TRUE;
break;
case WAIT_OBJECT_0 + 1:
{
MSG msg;
while( ::PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
::DispatchMessage( &msg );
}
}
break;
case WAIT_FAILED:
{
CString msg;
msg.Format( "Wait failed. Error code 0x%x.\n", GetLastError() );
::OutputDebugString( msg );
bQuit = TRUE;
}
break;
default:
{
CString msg;
msg.Format( "Unexpected wait result: 0x%x.\n", dwWaitResult );
::OutputDebugString( msg );
bQuit = TRUE;
}
break;
}
}
delete m_pMyThread;
m_pMyThread = NULL;
2. The thread might need some initialization such as getting a marshaled interface and if it fails I usually want to disable controls or similar before continuing. I do this by waiting for events that are signalled from the newly spawned thread informing the function that spawned the thread whether it has started successfully or not. Regarding these events my header file usually looks something like this:
enum
{
SuccessIndex,
ErrorIndex,
WinMsgIndex
};
HANDLE m_hThreadStartEvents[WinMsgIndex];
My constructor would contain something like this...
m_hThreadStartEvents[SuccessIndex] = ::CreateEvent( NULL, TRUE, FALSE, NULL );
m_hThreadStartEvents[ErrorIndex] = ::CreateEvent( NULL, TRUE, FALSE, NULL ); and my destructor contains cleanup code...
::CloseHandle( m_hThreadStartEvents[SuccessIndex] );
::CloseHandle( m_hThreadStartEvents[ErrorIndex] );
This will allow me to pass the event array for the "objects" to wait for when calling MsgWaitForMultipleObjects(). Hence the code after the row containing m_pMyThread->ResumeThread(), showing how I start new threads above, will look something like...
dwWaitResult = ::MsgWaitForMultipleObjects( WinMsgIndex, m_hThreadStartEvents, FALSE, INFINITE, QS_ALLINPUT ); followed by code similar to the one showing how I wait for the thread to finish and still processing messages with one little nice twist: in the switch statement you are able to use the values of the enum to test the return value from MsgWait... like this:
case SuccessIndex:
break;
case ErrorIndex:
break;
case WinMsgIndex:
break;
Quite often a worker thread's controlling function is built up with a loop similar to
while( !m_bStopThread )
{
....
....
} and sometimes the thread has the ability to terminate itself by simply returning from the controlling function. Your GUI thread has no way of knowing this unless it is polling the thread status. To inform your GUI thread if the spawned thread is about to finish and have a chance to free allocated resources, you simply post a message before returning from the threads controlling function. In the message handler you can wait for the thread to finish and free up allocated resources such as the CWinThread object (how is shown above).
I hope you find something useful in it.
--
Roger
with probably too much time to spare
|
|
|
|
|
Thanks Roger, this may solve many problems.
Sweep.
|
|
|
|
|
Sub : How to use Amazon Web Services..
Hi all...
Please let me know How to start with amazon web services..
I'm not finding any XML or REST Query format to send request to amazon..
I've purchased Seller account but How to use that to upload Items..I don't know...
Please help me getting start..
I hope some one definatly know about this
e-mail if attachment at sumit_kapoor1980@hotmail.com
Thanks...
Sumit Kapoor---
---Sumit Kapoor---
|
|
|
|
|
How about studying on how to contact a web service first?
Like this dilbert web service is not bad by getting dilbert comic strip automatically.
http://www.codeproject.com/soap/dilbertservice.asp
But it is in C# though.
Sonork 100.41263:Anthony_Yio
Life is about experiencing ...
|
|
|
|
|
If I fill an imagelist with partially transparent icons and bitmaps, set it as imagelist for the listctrl and create items with those icons, the transparent parts of the images will turn black. Why?
Thanks in advance.
|
|
|
|
|
Got it.
Allways use all frigging flags. This time, I had to use the flag IL_MASK (or something).
|
|
|
|
|
hi guys, I really need some help
I've been working a lot with VB 6. Lately I started using VC ++. I've got a small image processing project to do in VC++. I've got to load a picture (16X16 or 64X64 etc) and then extract its red, blue and green components and contruct a histogram as well. This is the 1st requirement of the project?
My question is which project type should I use:
MFC Appwizard/ win32 application ... I really can't make out. I need to build the interface (the form and buttons...) to load the picture 1st.
Any suggestion would be appreciated plus any piece code of code for that.
thanks
|
|
|
|
|
MFC type project makes your life easier!
Maxwell Chen
|
|
|
|
|
Yes you can use the MFC AppWizzard with the view derived from a form view.
|
|
|
|
|
hi,
I am realy interested with your project. please let me know the the requirement. we both work on that project.
Use MFC to Create the project.
use LoadImage function to load a image into application
display that loaded image using Onpaint message handler of the view class
|
|
|
|
|
thanks guys
hasansheik can u give some piece of code to start with at least the 1st requirement. (eventually I have to build a multimedia search engine - one in which u input an image and u get as output 'similar' images)
|
|
|
|
|
I am a beginner of C++ programming. Please help me
I came across this problem of creating a DVD class. The part I don't understand is in my handout for project, the instuctor asked us to assume
DVD consists of a number of audio/video chapters, How can I store data(like lyric of a song) into those Chapters?
Should I create a class named Chapters and use an array of Chapters in DVD class?
I mean we only pass data through like:
DVD* dvd = new DVD("show me the meaning of being lonly";
but all I can do is store one line into the class, how can I store the whole lyric into the class?
hu yu
|
|
|
|
|
foxele wrote:
Should I create a class named Chapters and use an array of Chapters in DVD class?
This sounds reasonable.
foxele wrote:
DVD* dvd = new DVD("show me the meaning of being lonly";
but all I can do is store one line into the class, how can I store the whole lyric into the class?
How about:
DVD dvd;
CHAPTER *chapter;
chapter = dvd.AddChapter();
chapter.AddLyrics("This is line 1");
chapter.AddLyrics("This is line 2");
chapter.AddLyrics("This is line 1\nThis is line 2");
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
How can I get the tooltips for CTreeCtrl to dsiplay some other info rather than the name of the tree nodes
|
|
|
|
|
Hi,
I want to communicate with USB device which is a Embeded Linux PDA. This devices are attached to Windows 2000 box. The requirement is that there may be more than one such device on the USB hub and the application on a windows box should be able to indentify and communicate with those devices. As the device is not yet ready the driver is not available to me.
I just wanted to know these functionality for detecting and communicating individualy with the device is provided by the driver, wether the USB drives provides some sort of call backs or events when ever a device is attached, detached or sends data. How the application using the driver should be able to know about these events and read and write data to the devixe.
I will be thank full if some one gives a clear Idea about the using USB drivers for detecting device attach, detach read and write to perticular device.
Reagrds
AbdulMunaf Chhatra
|
|
|
|
|
hi all!
does anyone know how to enable autorun of a program (.exe ) from an USB drive? autorun.inf as we know works for CD's and not for USB drives, but it's possible to autorun from an USB drive...an example is microtoolz.com....anybody knows how that can be done in the VC++ environment? thanks.
Kane
|
|
|
|
|
If you search @ msdn.microsoft.com for "autorun usb", you'll see that it is possible to associate apps to removable drives for autorun if you're using Autoplay V2 (embedded in WinXP).
I guess (!) that PenDrive Autorun associate itself with removable USB drives and if an USB drive is added to the system and XP is polling what application likes to autorun, PenDrive Autorun says 'yes' if autorun.inf is found. So WinXP gives the control to PenDrive Autorun and PenDrive Autorun executes the file specified in autorun.inf. But that is only a guess.
If it's true, than it's not possible to trigger an autorun from the USB, so only if PenDrive Autorun (or something like it) is installed on the computer, the trick will work.
|
|
|
|
|
Hi all;
I am a final-year university student and i am using SkinMagic to skin my application. However, the problem is that i do not have the registered version (student=no money!) and i keep getting the annoying message "you are using an unregistered version of SkinMagic" each time i start my application. This will not be very nice when i will present my application to the examiners. Could anybody possibly provide me with a copy of the SkinMagic.h file and the lib file. I have asked the authors but they seem reluctant to lend me a copy for educational purposes.
Thanks guys;
Krugger
Plan tomorrow today itself or else it will be too late..
|
|
|
|
|
there are many skinned demo source codes in the codetools, why not using those than SkinMagic lib?
like this one
http://www.codeproject.com/dialog/skindlg.asp
Sonork 100.41263:Anthony_Yio
Life is about experiencing ...
|
|
|
|
|
Using skinmagic you can skin the application with little affort rather than coding the logic to skin the app yourself.
I'll write a suicide note on a hundred dollar bill - Dire Straits
|
|
|
|
|
Yes, 3rd party library must be convinient. But as a student, should he did this kind of thing by himself? What the point of submiting something that you have not putting effort into it?
Sonork 100.41263:Anthony_Yio
Life is about experiencing ...
|
|
|
|
|
Anthony_Yio wrote:
What the point of submiting something that you have not putting effort into it?
true,
But i guess his application will work even without the skin too... As a college student he is more expected to implement the logic.
Developing skinmagic kind of component is a huge task for even a fairly experienced programmer.
I'll write a suicide note on a hundred dollar bill - Dire Straits
|
|
|
|
|
copy of skinmagic.h and .lib will not help you with removing the anoyying popup dialog... you need to enter a registration key while loading the skinmagic.dll
I do have one but alas i cant give it to you.
I'll write a suicide note on a hundred dollar bill - Dire Straits
|
|
|
|
|
Hello,
In a C++ program, I would like to change the string
"ty^Hypescript ^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H
^H^H ^H^H ^H^H ^H^G^G^G^G^Gty^Hypescript ^H ^H^H ^H^H ^H^H ^H^M"
into the string
"typescript".
The letters like '^H', '^H^H', and '^G' represents some of the ANSI control characters such as backspace. (the characters are shown on VI editor.)
So, I think it's possible to process control characters within a code, like a VT100 virtual terminal does.( In deed, I am not sure whether the process is done in a terminal, shell or whatever.)
If you have a sample code or a function to suggest, please post them. Thank you.
|
|
|
|
|
MFC:
CString::Remove
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|