SpreadsheetGear The Performance Spreadsheet Component Company 
GetLock Method
See Also  Example
SpreadsheetGear Namespace > IWorkbookSet Interface : GetLock Method
Interrupts background calculation if necessary and acquires an exclusive lock on the workbook set.

Syntax

C# 
virtual void GetLock()

Example

Demonstrate using a workbook set in a thread safe manner.
C#Copy Code
void DoSomeThreadSafeWork(IWorkbookSet workbookSet) 
        { 
            // Interrupt background calculation if necessary and acquire an 
            // exclusive lock on the workbook set. 
            workbookSet.GetLock(); 
  
            try { 
                // Do some work... 
            } 
            finally { 
                // Release the lock on the workbook set and start background 
                // calculation if appropriate. 
                workbookSet.ReleaseLock(); 
            } 
        } 
    
Visual BasicCopy Code
Sub DoSomeThreadSafeWork(ByVal workbookSet As IWorkbookSet)
            ' Interrupt background calculation if necessary and acquire an
            ' exclusive lock on the workbook set.
            workbookSet.GetLock()

            Try
                ' Do some work...
            Finally
                ' Release the lock on the workbook set and start background
                ' calculation if appropriate.
                workbookSet.ReleaseLock()
            End Try
        End Sub 'DoSomeThreadSafeWork

Remarks

This method is thread safe.

A lock must be acquired on a workbook set before using the workbook set from multiple threads. This includes any access to any properties or methods of any object belonging to the workbook set, unless the property or method is explicitly marked as thread safe. Since all Windows Forms components from SpreadsheetGear use GetLock and ReleaseLock whenever they access a workbook set, and since BackgroundCalculation is on by default, GetLock and ReleaseLock must be used to access a workbook set which is attached to any of these Windows Forms components.

To eliminate the need to use GetLock and ReleaseLock when using Windows Forms components from SpreadsheetGear, set BackgroundCalculation to false or set Calculation to Calculation.Manual, and limit access to the workbook set to the thread which created the Windows Forms component(s). The WorkbookView control allows the BackgroundCalculation property to be set in the WorkbookView Smart Tag and in the WorkbookView property list, although doing so will typically hurt performance significantly.

There is no need to use GetLock and ReleaseLock when using a workbook set which is not attached to any Windows Forms components from SpreadsheetGear. However, these methods may still be useful to synchronize access to a workbook set, particularly in applications which would benefit from interruptible background calculation.

All components in the SpreadsheetGear.Windows.Forms namespace adhere to the standard Windows Forms threading model. Members of any object in the SpreadsheetGear.Windows.Forms namespace must not be accessed from any thread other than the thread which created the object. Calling System.Windows.Forms.Control.BeginInvoke is valid when holding a lock on a workbook set. However, calling System.Windows.Forms.Control.Invoke while holding a lock on a workbook set is likely to result in a deadlock.

Calls to GetLock may be nested. ReleaseLock must be called once for each call to GetLock. The recommended best practice is to call GetLock followed by a try block where work is done, and then call ReleaseLock in a finally block.

Requirements

Platforms: Windows Vista, Windows XP, Windows Server 2008, Windows Server 2003, Windows 2000, Windows Me and Windows 98, including 32 bit and 64 bit editions where applicable. SpreadsheetGear for .NET 1.x requires the Microsoft .NET Framework 1.1 or .NET 2.0 (works with .NET 3.x). SpreadsheetGear for .NET 2007 requires the Microsoft .NET Framework 2.0 (works with .NET 3.x).

See Also