Why Is This Crashing

G

Guest

Hi There

I have just completed my first experiment in VSTO 2.0, and the results are
not what I expected !

I have a worksheet with a named range (EgRange) and I wanted to experiment
with threading. I start three call backs from the ThreadPool and they update
their own cells in EgRange.

All goes fine if the user does not touch the spreadsheet while the code is
running, however, as soon as the user tries to do something ...

An unhandled exception of type 'System.Runtime.InteropServices.COMException'
occurred in mscorlib.dll
Additional information: Exception from HRESULT: 0x800AC472

Here is the code:

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;

namespace VSTO_EXPERIMENT
{
public partial class Sheet1
{
#region VSTO Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup ( )
{
this.button1.Click += new System.EventHandler ( this.button1_Click );
}

#endregion

private void button1_Click ( object sender, EventArgs e )
{
myRange = this.Range["egRange", "egRange"];

WaitCallback callBack = new WaitCallback ( CallBackProc );
ThreadPool.QueueUserWorkItem ( callBack,1);
Thread.Sleep ( sleepTime );
ThreadPool.QueueUserWorkItem ( callBack, 2);
Thread.Sleep ( sleepTime );
ThreadPool.QueueUserWorkItem ( callBack, 3);
Thread.Sleep ( sleepTime );
}

Excel.Range myRange;
int sleepTime = 10;

public void CallBackProc ( object obj )
{
int col = (int)obj;
Excel.Range thisCell = ((Excel.Range)myRange[1, col]);
for (int rw = 1; rw <= 50000; rw++)
{
thisCell.Value2 = Thread.CurrentThread.Name + " (" + rw.ToString ( ) +
"," + col.ToString ( ) + ")";
Thread.Sleep ( sleepTime );
}
}
}
}

Thanks
Ben
 
G

Guest

Only thing I can think of is Excel's Interactive property that will block
user input while macros are running.

Application.Interactive = False
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top