LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Multithreading Issue

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

0 Kudos
Message 1 of 7
(5,141 Views)

How are you locking the threads? Could you post some sample code?

 

JR

0 Kudos
Message 2 of 7
(5,116 Views)

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);
}

0 Kudos
Message 3 of 7
(5,103 Views)
Just to make sure: are you trying to get the lock in thread1 too? If not, thread1 is not effectively locked and you only seem to have it blocked under normal condition; on special conditions, since report saving and activex stuff are relatively long lasting operations you can notice that thread1 is still running.


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 7
(5,098 Views)

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

0 Kudos
Message 5 of 7
(5,091 Views)

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

0 Kudos
Message 6 of 7
(5,087 Views)

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...

0 Kudos
Message 7 of 7
(5,075 Views)