Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Ben Ben is offline
external usenet poster
 
Posts: 509
Default Why Is This Crashing

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default Why Is This Crashing

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

Application.Interactive = False



"Ben" wrote:

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

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 Crashing SJMP Setting up and Configuration of Excel 3 March 6th 09 12:27 PM
Excel Crashing [email protected] Excel Discussion (Misc queries) 1 December 10th 07 02:23 PM
vb exe crashing kasi Excel Programming 1 October 15th 05 03:33 AM
Is this crashing for you ? Alex T Excel Programming 6 September 5th 04 05:17 PM
vba crashing scottwilsonx[_15_] Excel Programming 2 August 6th 04 01:16 PM


All times are GMT +1. The time now is 08:11 PM.

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

About Us

"It's about Microsoft Excel"