|
Hello all,
I was wondering about the scope of try...catch constructions over threads.
An example (very simplified example of existing code, where the Main_thread acts like a scheduler and the Worker_Threads are the 'processes'):
Main_thread
{
try {
}
catch( ... ) {
}
}
Worker_thread
{
try {
}
catch( condition ) {
}
catch( ... ) {
throw( something );
}
}
Is it possible to (or 'how to') catch the last throw from the Worker_thread into the Main_thread?
What is the nice way to do this?
Thanks in advance,
EiSl
|
|
|
|
|
Is it possible to (or 'how to') catch the last throw from the Worker_thread into the Main_thread?
The short answer is no you can't. Exceptions are intrinsically attached to the thread where they're produced, as are the mechanisms of stack unwinding involved.
An uncaught exception is simply swallowed by the system (though it is no good practice to let this happen).
An approach to having uncaught exceptions somewhat notified to the main thread is having a grand wrap-all try-catch in the worker thread that intercepts otherwise uncaught exceptions, translates this to a DWORD and return that result code, which the main thread can access with GetExitCodeThread() .
How do you translate exceptions to DWORD s? Depends on what kind of exceptions you're dealing with:
- If you don't have any a priori idea about the exceptions the worker thread can throw, then there's little you can do except perhaps returning 0 as "everything OK" and some other code for "some exception thrown".
- If the exceptions are derived from C++
std::exception , which provide a description with method what() , you can store this message in some global table and return a pointer to it.
- If the exceptions are copyable, you can store the exception objet itself in a table an return a pointer to it. Please note that this requires that the exceptions be of an exact known type, as derived types objects are not handled correctly (they're sliced).
- Some other ad hoc mechanism. You name it.
Hope this helped.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
IMHO it's not possible.
You have to do some thread syncronization between the Main_thread and Worker_thread.
----
Rui Lopes
|
|
|
|
|
Can I use CFileFind to search for a specific file on my computer?
I've seen that the class has to do with the CGopherFileFind and CFtpFileFind. So I guess it's perhaps impossible?
I've tried this but nothing appears:
void CFopiacjeDlg::OnButton1()
{
CFileFind finder;
BOOL bWorking = finder.FindFile("Fcc32.exe");
while (bWorking)
{
bWorking = finder.FindNextFile();
AfxMessageBox(finder.GetFilePath());
}
}
|
|
|
|
|
Findfile like that is only searching your current directory. (doth it search the PATH too? I can't remember.). But it isn't going to search your entire drive as you would want it to.
I use findfile in a self recursive function (it calls itself). Like this,
If you don't want the tree just chop those bits out.
This is a version that fills a tree with directories.
void CExIncView::FillDirTree()
{
char DRIVES[33];
DWORD DriveNumbers;
DWORD j;
int k,Image,ImageSelected;
CString lString;
HTREEITEM ParentList[33];
unsigned lDriveType;
m_Messages = " ";
UpdateData(FALSE);
//For each drive collect all directories
DriveNumbers = GetLogicalDrives();
j=1;
for(j=1,k=0;k<32;k++,j*=2)
{
if( DriveNumbers & j )
{
DRIVES[k] = 'A' + k;
}
else
{
DRIVES[k] = ' ';
}
}
//TEMP Put drives into tree
for(k=0;k<32;k++)
{
if( DRIVES[k] != ' ')
{
lString.Format("%c:",DRIVES[k]);
lDriveType = GetDriveType(lString);
switch(lDriveType)
{
case DRIVE_UNKNOWN ://The drive type cannot be determined.
Image = 2;
ImageSelected = 3;
break;
case DRIVE_NO_ROOT_DIR ://The root directory does not exist.
Image = 2;
ImageSelected = 3;
break;
case DRIVE_REMOVABLE ://The disk can be removed from the drive.
Image = 0;
ImageSelected = 1;
break;
case DRIVE_FIXED ://The disk cannot be removed from the drive.
Image = 2;
ImageSelected = 3;
break;
case DRIVE_REMOTE ://The drive is a remote (network) drive.
Image = 2;
ImageSelected = 3;
break;
case DRIVE_CDROM ://The drive is a CD-ROM drive.
Image = 4;
ImageSelected = 5;
break;
case DRIVE_RAMDISK ://The drive is a RAM disk.
Image = 2;
ImageSelected = 3;
break;
default:
Image = 2;
ImageSelected = 3;
break;
}
//ParentList[k] = m_pDirTree.InsertItem(lString,TVI_ROOT,TVI_SORT);
ParentList[k] = m_pDirTree.InsertItem(lString,Image,ImageSelected,TVI_ROOT,TVI_LAST);
m_pDirTree.SetItemData( ParentList[k], (DWORD) 50 );
}
else
{
ParentList[k] = NULL;
}
}
//For each parent traverse directory tree
for(k=0;k<32;k++)
{
if(ParentList[k] != NULL)
{
lString.Format("%c:",DRIVES[k]);
SearchDir(lString,ParentList[k]);
}
}
}
SearchDir is the self recursive function.
void CExIncView::SearchDir(CString sPath)
{
CString sFind,sTemp;
CString sBase,sBase2;
int Done;
WIN32_FIND_DATA lpFindFileData;
HANDLE SearchHandle;
sBase = sPath;
sFind.Format("%s\\*.*",sBase);
SearchHandle = FindFirstFile(sFind,&lpFindFileData);
if( SearchHandle != INVALID_HANDLE_VALUE)
Done = 0;
else
Done = 1;
while( !Done )
{
do
{
if( lpFindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY )
{
sTemp = lpFindFileData.cFileName;
if( (sTemp.Find("..") != -1) || (sTemp.GetLength()==1) )
;
else
{
sBase2.Format("%s\\%s",sBase,lpFindFileData.cFileName);
SearchDir(sBase2);
}
}
else
{
sTemp = lpFindFileData.cFileName;
if( sTemp.Find(m_FindThis ) != -1 )
{
if( m_iFind == 0 )
m_DatabaseLocation = sBase;
else
m_SourceFileDir = sBase;
}
}
}
while(FindNextFile(SearchHandle,&lpFindFileData));
Done = 1;
}
}
This FindFile can now be used to search for particular files (in this case on C
bool CExIncView::FindFile(CString &FName)
{
CString lString;
m_FindThis = FName;
lString.Format("C:");
SearchDir(lString);
return TRUE;
}
We do it for the joy of seeing the users struggle.
|
|
|
|
|
Hello All!
I have a custom draw list control, but it scroll bars has a standart look. Does anybody know how can I draw this scroll bars (I think it windows
style scroll bars, not controls) by my own? Or how can I replace this scroll bars by my custom draw controls without writing my own scrolling code?
|
|
|
|
|
I havn't done this myself, but I guess you can change color by overriding OnCtlColor. Check for CTLCOLOR_SCROLLBAR in msdn.
If you would like a more custom draw get the scrollbars from the window using GetScrollBarCtrl and subclass them to your own derivation of CScrollBar.
/moliate
|
|
|
|
|
Yes, I`m want a more custom draw, but GetScrollBarCtrl returns NULL for windows style scrollbars
I would like to know, how can I subclass this scrollbars?
|
|
|
|
|
Hello, I want to display a clock on a dialog box. I want to do both an "analog clock" as well as a "digital clock". If you can help me as to resources to help me do this I would appreciate it.
Thanks.
|
|
|
|
|
You'll probably have to code an analog clock yourself, but how you would go about doing this would be to create a TIMERPROC, using SetTimer, that is updated every 1000 milliseconds or 1 second, then you would draw the analog clock every time the timer is executed according to the time provided by GetSystemTime. If you wanted the digital equivlant you would of course, just use GetTimeFormat, and print it out to the screen.
For a digital type look try :
http://www.codeproject.com/staticctrl/cstatictime.asp
Try MSDN for information on the stuff listed above.
Nathan
modified on Sunday, April 19, 2009 12:26 AM
|
|
|
|
|
Thanks, that is what I figured I would have to do. Thanks for your input.
|
|
|
|
|
To draw the clock face you'll probably have to resort to trigonomentry. You might be able to precalculate some values, though I doubt it.
If you want something more complicated than simple lines you have to create a vector image then rotate the points. I think Joseph M Newcomer has written an article on this.. but it's late so I'll leave you to find it .
Remember -
sin x = OPP/HYP
cos x = ADJ/HYP
tan x = OPP/ADJ. You can use the radius of your clock minus some value (to get the correct size so you don't draw over the numbers of the clockface) for the length of the hypotenuse (sic?).
--
Andrew.
|
|
|
|
|
|
Migrating from VB, I could not find an easy way to trap a return keystroke and then act apon it on a CEDit control. Update and Change don't pass a parameter telling you which key has been pressed. Have I missed something?
- X
|
|
|
|
|
You will have to subclass the control to be able to catch keystrokes.
See SubclassDlgItemin MSDN. Then add a handler for the WM_CHAR message.
Cheers,
/Fredrik
|
|
|
|
|
hi all,
are there any ppls meet this situation? the same program but the outlook is different in 2 pcs. actually, the 2 pcs i used have identical configuration. the OS is newly installed.
would anyone give me any idea?
here is the link of 2 outlook.
http://mail.hkibrc.org/~chaze/question/question.html
regards,
jim
|
|
|
|
|
Well, the obvious difference is in the font. The 2 PCs must not be identical if the same dialog box has a different font on each one. It looks like one is using Large Fonts, which you can turn off in the Display Settings.
Regards,
Alvaro
|
|
|
|
|
I just added a function to a foo.cpp module which makes use of std::ifstream. This function was required for Project A. When I went back to compile another Project B, which also uses foo.cpp but not the new function, it grew in size by 80k.
I assume there are some extra libraries that are getting linked in. Is there anyway to prevent this from happening? How do I know which libraries are getting linked in?
Todd Smith
|
|
|
|
|
Hello,
Christian was so much help last time, I hope he is around for this post.
I got the open a new doc/.view working (thank you )
But now I need to create just a new doc. Can't just call it but its constructor (that'd be too easy).
The earlier example created a CDocument that was cast into my Doc type, but now since I am just pressing the File > New button, there is no file to load, etc...
A tad lost
Rob
|
|
|
|
|
Where you passed in a poiner to a path to the document to load, pass in NULL to create a new one.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
|
|
|
|
|
hrmmm... it crashes ?
I have mod the code so it looks like this:
if (OpenDocumentFile(NULL) == NULL) {
AfxMessageBox("Could not create new file !");
}
CDocument* CGuitarBot_ComposerApp::OpenDocumentFile(LPCTSTR lpszFileName)
{
CDocument* pReturn = CWinApp::OpenDocumentFile(lpszFileName);
CGuitarBot_ComposerDoc* pDoc = (CGuitarBot_ComposerDoc*)pReturn;
return pReturn;
}
So it still does the same thing for open & new, just when I call New, i pass a NULL. Dunno ?
I really appreciate the help.
Rob
|
|
|
|
|
I posted that from memory, it's obviously wrong. Loading the project now..... I must say I'm surprised, you've done something odd because by default all this stuff is plumbed in for you. I almost started a guitar tab program myself once....
I have multiple templates and in CWinApp I force use of the first one like this:
POSITION POS = GetFirstDocTemplatePosition();
CDocTemplate* pTemp = GetNextDocTemplate(POS);
CGrausPaintDoc* pDoc = (CGrausPaintDoc*) pTemp->OpenDocumentFile(NULL);
which is why I expected OpenDocumentFile(NULL) to work. How did you create this project so that clicking File/New doesn't do it for you ?
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
|
|
|
|
|
Yes, the project is pretty cool. Our team (myslef and 2 other beer drinking college guys) took it on from our client, a wireless data comm. guy who's done this in his spare time for the past 10 yrs.
We know nothing about music, and 1/2nothing about MFC.
I put my file open/new code in the GuitarBot_Composer.cpp class.
I wish I could answer why the open works and not the new ?
The open works fine but the new wont ? Shall I send some code your way ?
Many Thanks
Rob
|
|
|
|
|
robbied wrote:
The open works fine but the new wont ? Shall I send some code your way ?
Sounds like a good idea. I really don't see how this can be possible, because at the end of the day, MFC plumbs that stuff for you before you start.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
|
|
|
|
|
I've got this odd problem with SetDIBitsToDevice - its strange because the function has been working fine for the past year or so, but now it doesn't work. I pass it the correct parameters, and a valid set of bits, and it even returns a success response, yet all it paints is blackness. The function works fine 8bit images, but fails with 24bit images !?! even if I use a screen res of 24bit If any of you can help... YadaYada yuno.
I'll try and answer someone else's question to maintain my code project carma 'ask one question one question give one answer'!
With time we live, with money we spend!
Joel Holdsworth
|
|
|
|