Multithreaded callback.

callBackServer1.zip
  1. Encapsulate the Fire_... function into an interface method.

    Add the method Fire_actionMethod() to the interface main and provide the implementation:
    STDMETHODIMP CTheCoClass::Fire_actionMethod()
    {
    	Fire_action();
    	return S_OK;
    }
    
  2. Open callBackServer project and change the mainMethod...

    as follows:
    
    DWORD WINAPI timerThreadProcedure( void* p )
    {
    	CoInitialize(NULL);
    	IMain* i=(IMain *)p;
    	for( int j=1; j<=10; ++j )
    	{
    		Sleep(1000);
    		i->Fire_actionMethod();
    	}
    	i->Release();
    	CoUninitialize();
    	return 0;
    }
    
    STDMETHODIMP CTheCoClass::mainMethod()
    {
    	IMain* ptr;
    	QueryInterface(IID_IMain,(void**)&ptr);
    	CreateThread(NULL,0,timerThreadProcedure,ptr,0,0);
    	return S_OK;
    }
    
    

  3. Test client.

    		
    Dim WithEvents server As CALLBACKSERVERLib.TheCoClass
    Dim i As Integer
    
    Private Sub server_action()
        i = i + 1
    End Sub
    
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
        If server Is Nothing Then
            Set server = New CALLBACKSERVERLib.TheCoClass
        End If
        server.mainMethod
    End Sub
    
    Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
        ActiveCell.Value = i
    End Sub
    
    
  4. Important note

    You cannot modify cells directly in the callback function.
    If user opens a modal dialog box then the messages will be lost. One really have to store them in some intermediate layer and ask the layer repeatedly.

Hosted by www.Geocities.ws

1