tags:

views:

23

answers:

0

I have created an interface to define a RangeSelectionTarget as follows:

public interface IRangeSelectionTarget
{
    void OnRangeSelection(Excel.Range target);
}

In another class (ExcelController, encapsulating excel interop behavior), I have a method to pipe the worksheet range selections (using the mouse to select cells) to the target:

    public void SelectCaptureOn(IRangeSelectionTarget target)
    {
        _selectionCallback += new SelectionCallback(target.OnRangeSelection);
        _selectionCallback += new SelectionCallback(SelectionCallback2);

        int limit = _workbook.Worksheets.Count;

        for (int i = 1; i <= limit; i++)
        {
            Excel.Worksheet worksheet = _workbook.Worksheets.get_Item(i);
            string worksheetName = worksheet.Name;
            worksheet.SelectionChange
                += new Excel.DocEvents_SelectionChangeEventHandler(OnXlSelectionCallback);
        }
    }

The target, a form, implements OnRangeSelection() as follows in order to display the cell addresses:

    public void OnRangeSelection(Excel.Range target)
    {
        if (this.InvokeRequired)
        {
            RangeSelectionDelegate rsDel = new RangeSelectionDelegate(OnRangeSelection);
            this.Invoke(rsDel, new object[] { target });
        }
        else
        {
            txtRange.Text = target.AddressLocal;
            txtRange.Tag = target;
        }
    }

This works great, except when it doesn't. After calling SelectCaptureOn(), OnRangeSelection() starts getting called on the IRangeSelectionTarget, that then displays the addresses of the selected cells. OnRangeSelection() checks for InvokeRequired in order to avoid threading issues....or so I hope.

The problem is that after a few successful selection displays, the behavior stops completely dead.

Any ideas why, or where to start investigating?

Thanks!