Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Excel crashes on exit

Does anyone know why Excel 2003 crashes on exit after using a .NET
object through COM interop with events? To reproduce the problem, see
the example below...

The C# project and Excel Workbook are available for download at:
http://www.shapescape.com/AClassLibrary.zip

// C# code in Ticker.cs -------------------------------

using System;
using System.Threading;
using System.Runtime.InteropServices;

namespace AClassLibrary
{
public delegate void TickEventHandler();

#region Events raised by COM class
[Guid("B846A796-34E3-4B7E-BDF5-114B875CDCCE")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatc h)]
public interface ITickerEvents
{
[DispId(1)] void TickEvent();
}
#endregion

#region Interface published by COM class
[Guid("42EBAA0E-F2F3-499B-9143-6AE919C00F92")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface ITicker
{
[DispId(1)] int GetTick();
[DispId(2)] void Dispose();
}
#endregion

[Guid("436E5AB1-ED28-4E4E-B675-98D7473703C7")]
[ProgId("AClassLibrary.Ticker")]
[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(ITickerEvents))]
public class Ticker : ITicker, IDisposable
{
private bool _disposed = false;
private int _tick = 0;
private Timer _timer;
private TimerCallback _timerDelegate;

public event TickEventHandler TickEvent;

public Ticker() : base()
{
_timerDelegate =
new TimerCallback(processTimeEvent);
_timer = new Timer(_timerDelegate, null, 0, 1000);
}

~Ticker()
{
Dispose(false);
}

private void processTimeEvent(Object stateInfo)
{
_tick++;
if (TickEvent != null)
{
TickEvent();
}
}

public int GetTick()
{
return _tick;
}

#region IDisposable Members

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

private void Dispose(bool disposing)
{
if(!_disposed)
{
if(disposing)
{
_timer.Dispose();
_timer = null;
_timerDelegate = null;
}
}
_disposed = true;
}

#endregion
}
}

‘ VBA code in ThisWorkbook module ------------------------

Private WithEvents ticker As AClassLibrary.Ticker

Private Sub Workbook_Open()
Set ticker = New AClassLibrary.Ticker
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Cancel = False Then
ticker.Dispose
Set ticker = Nothing
End If
End Sub

Private Sub ticker_TickEvent()
Dim rng As Range
Set rng = Application.Worksheets("Sheet1").Cells(1, 1)
rng.Value = ticker.GetTick
End Sub
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel 07 won't exit Elle Setting up and Configuration of Excel 0 July 27th 07 12:40 AM
exit excel welshwizzard Excel Discussion (Misc queries) 1 January 18th 05 12:02 AM
Excel 97 crashes Tom Excel Programming 1 June 28th 04 11:04 AM
Excel 97 crashes Tom Excel Programming 1 June 20th 04 10:15 PM
If a called sub exit, how to the caller exit right away? luvgreen[_4_] Excel Programming 4 February 24th 04 05:06 PM


All times are GMT +1. The time now is 06:09 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"