04-15-2009 02:49 PM
Application: 2 Threads
Thread 1 does something
Thread 2 does something but if a certain condition is met, locks thread, writes to excel, closes excel file, opens a new excel file, releases thread.
Issue:
After Thread2 is locked and after the following command is excecuted
ExcelRpt_WorkbookSave
Thread1 for some reason releases and continues to run
Does anyone know why this command is releasing threads ?
Thanks
Diego
04-16-2009 03:26 AM
How are you locking the threads? Could you post some sample code?
JR
04-16-2009 09:17 AM
JR,
This is the basic version of it. Highlighted bold command is the one that causes Thread1 to continue running.
Everything works fine if I remove the Excel commands out of the Lock section.
Main
{
//excel
ExcelRpt_ApplicationNew (VFALSE, &applicationHandle);
ExcelRpt_WorkbookNew (applicationHandle,&workbookHandle);
ExcelRpt_GetWorksheetFromIndex (workbookHandle, 1, &worksheetHandle);
ExcelRpt_ActivateWorksheet(worksheetHandle);
//multithreading
runThread = TRUE;
CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE,Thread1, &Beagle_thread, &Beagle_thread);
CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE,Thread2, &Host_thread, &Host_thread);
CmtNewLock (NULL, 0, &mylock);
}
Thread2
Do something
if(blah blah)
{
CmtGetLock(mylock);
//update tables
ExcelRpt_WorkbookSave (workbookHandle, Datalog_Dir,ExRConst_DefaultFileFormat);
if (worksheetHandle) CA_DiscardObjHandle(worksheetHandle);
if (workbookHandle)
{
ExcelRpt_WorkbookClose(workbookHandle, 0);
CA_DiscardObjHandle(workbookHandle);
}
if (applicationHandle)
{
ExcelRpt_ApplicationQuit(applicationHandle);
CA_DiscardObjHandle(applicationHandle);
}
CmtReleaseLock(mylock);
}
04-16-2009 09:47 AM
04-16-2009 10:38 AM
Roberto,
Sorry for the confusion.
I want to lock thread1( as in pause it) when blah blah condition is met in thread2, I then want to execute all the functions in the if statement ( while thread 1 is locked).
And then finally unlock/release thread1 when all the functions in the if statement in thread2 are completed.
Thanks
Diego
04-16-2009 11:19 AM
As Roberto says, you need to use CmtGetLock(mylock) in your Thread1 code as well. Since you did not show any code for Thread1, we cannot assess what the problem might be.
JR
04-16-2009 12:56 PM
additionally, i would strongly recommend creating the lock BEFORE creating the threads with CmtScheduleThreadPoolFunction().
as it is written now, the threads start while the lock still does not exists, with a risk to badly fail on lock acquisition...