C# 从另一个线程更新excel电子表格

C# 从另一个线程更新excel电子表格,c#,multithreading,excel,com-server,C#,Multithreading,Excel,Com Server,我使用C#创建了COM服务器,我的客户端可以在其中接收实时更新。 更新通常从不同的线程触发。 但我注意到,当回调方法更新电子表格时,Excel崩溃。 有没有办法在UI线程中调用更新 另外,我知道Excel的RTD功能。但它不适合我的需要,因为我需要在一次更新中使用多个参数。您要查找的是ISyncronizedInvoke的Invoke/BeginInvoke方法 在UI线程上,获取任意控件并保留该引用 从要触发更新的线程,使用要在UI线程上执行的委托在该控件上调用Invoke或BeginInvo

我使用C#创建了COM服务器,我的客户端可以在其中接收实时更新。 更新通常从不同的线程触发。 但我注意到,当回调方法更新电子表格时,Excel崩溃。 有没有办法在UI线程中调用更新


另外,我知道Excel的RTD功能。但它不适合我的需要,因为我需要在一次更新中使用多个参数。

您要查找的是ISyncronizedInvoke的Invoke/BeginInvoke方法

在UI线程上,获取任意控件并保留该引用

从要触发更新的线程,使用要在UI线程上执行的委托在该控件上调用Invoke或BeginInvoke(控件实现ISyncronizedInvoke)。。。您可以通过该代理调用COM服务器

//编辑:示例代码

using System;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Thread someWorkerThread;

        Microsoft.Office.Interop.Excel.Application ExApp;
        Worksheet wrkSheet;

        public Form1()
        {
            InitializeComponent();

            ExApp = new Microsoft.Office.Interop.Excel.Application();

            ExApp.Visible = true; // or else we won't see the window

            var books = ExApp.Workbooks;
            var wrkBook = books.Add();
            var sheets = wrkBook.Worksheets;
            wrkSheet = sheets.get_Item(1);

            Marshal.ReleaseComObject(sheets);
            Marshal.ReleaseComObject(wrkBook);
            Marshal.ReleaseComObject(books);

            someWorkerThread = new Thread(new ParameterizedThreadStart(threadHandler));
            someWorkerThread.Start(this);
        }

        private void threadHandler(object obj)
        {// this will be executed on a seperate worker thread
            Control mainFrm = obj as Control;
            if (mainFrm == null)
                throw new ArgumentException("Need to have a Control as parameter");
            for (int i = 1; i < 50;i++ )
            {
                Thread.Sleep(2500);
                mainFrm.Invoke(new Action<int>(doStuff), i); // this will invoke the main UI thread
            }
        }

        private void doStuff(int i)
        {// this will be executed on the main UI thread
            var range = wrkSheet.Range[string.Format("A{0}", i)];
            range.Value = "Hello World!";
            Marshal.ReleaseComObject(range);
        }


        #region designer stuff
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(76, 84);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(149, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "I am an ordinary windows form";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;

        #endregion
    }
}
使用系统;
使用System.Windows.Forms;
使用系统线程;
使用Microsoft.Office.Interop.Excel;
使用System.Runtime.InteropServices;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
线程;线程;线程;
Microsoft.Office.Interop.Excel.Application ExApp;
工作表工作表;
公共表格1()
{
初始化组件();
ExApp=新的Microsoft.Office.Interop.Excel.Application();
ExApp.Visible=true;//否则我们将看不到窗口
var books=ExApp.工作簿;
var wrkBook=books.Add();
var表=wrkBook.工作表;
wrkSheet=工作表。获取项目(1);
元帅发布对象(张);
Marshal.ReleaseComObject(wrkBook);
发布对象(书籍);
someWorkerThread=新线程(新参数化的ThreadStart(threadHandler));
someWorkerThread.Start(this);
}
私有void线程处理程序(对象obj)
{//这将在一个单独的工作线程上执行
控制mainFrm=obj作为控制;
如果(mainFrm==null)
抛出新ArgumentException(“需要将控件作为参数”);
对于(int i=1;i<50;i++)
{
睡眠(2500);
Invoke(新操作(doStuff),i);//这将调用主UI线程
}
}
私人住宅(内部一)
{//这将在主UI线程上执行
var range=wrkSheet.range[string.Format(“A{0}”,i)];
range.Value=“你好,世界!”;
Marshal.ReleaseComObject(范围);
}
#区域设计师
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.label1=new System.Windows.Forms.Label();
这个.SuspendLayout();
// 
//标签1
// 
this.label1.AutoSize=true;
this.label1.Location=新系统图纸点(76,84);
this.label1.Name=“label1”;
this.label1.Size=新系统图纸尺寸(149,13);
this.label1.TabIndex=0;
this.label1.Text=“我是一个普通的windows窗体”;
// 
//表格1
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(284262);
this.Controls.Add(this.label1);
this.Name=“Form1”;
this.Text=“Form1”;
此选项为.resume布局(false);
这个。执行布局();
}
#端区
private System.Windows.Forms.label1;
#端区
}
}

请注意:此示例不会清理wrkSheet引用和ExApp引用。。。您必须在退出应用程序之前释放它们

您要寻找的是ISyncronizedInvoke的Invoke/BeginInvoke方法

在UI线程上,获取任意控件并保留该引用

从要触发更新的线程,使用要在UI线程上执行的委托在该控件上调用Invoke或BeginInvoke(控件实现ISyncronizedInvoke)。。。您可以通过该代理调用COM服务器

//编辑:示例代码

using System;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Thread someWorkerThread;

        Microsoft.Office.Interop.Excel.Application ExApp;
        Worksheet wrkSheet;

        public Form1()
        {
            InitializeComponent();

            ExApp = new Microsoft.Office.Interop.Excel.Application();

            ExApp.Visible = true; // or else we won't see the window

            var books = ExApp.Workbooks;
            var wrkBook = books.Add();
            var sheets = wrkBook.Worksheets;
            wrkSheet = sheets.get_Item(1);

            Marshal.ReleaseComObject(sheets);
            Marshal.ReleaseComObject(wrkBook);
            Marshal.ReleaseComObject(books);

            someWorkerThread = new Thread(new ParameterizedThreadStart(threadHandler));
            someWorkerThread.Start(this);
        }

        private void threadHandler(object obj)
        {// this will be executed on a seperate worker thread
            Control mainFrm = obj as Control;
            if (mainFrm == null)
                throw new ArgumentException("Need to have a Control as parameter");
            for (int i = 1; i < 50;i++ )
            {
                Thread.Sleep(2500);
                mainFrm.Invoke(new Action<int>(doStuff), i); // this will invoke the main UI thread
            }
        }

        private void doStuff(int i)
        {// this will be executed on the main UI thread
            var range = wrkSheet.Range[string.Format("A{0}", i)];
            range.Value = "Hello World!";
            Marshal.ReleaseComObject(range);
        }


        #region designer stuff
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(76, 84);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(149, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "I am an ordinary windows form";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;

        #endregion
    }
}
使用系统;
使用System.Windows.Forms;
使用系统线程;
使用Microsoft.Office.Interop.Excel;
使用System.Runtime.InteropServices;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
线程;线程;线程;
Microsoft.Office.Interop.Excel.Application ExApp;
工作表工作表;
公共表格1()
{
初始化组件();
ExApp=新的Microsoft.Office.Interop.Excel.Application();
ExApp.Visible=true;//否则我们将看不到窗口
var books=ExApp.工作簿;
var wrkBook=books.Add();
var表=wrkBook.工作表;
wrkSheet=工作表。获取项目(1);
元帅发布对象(张);
Marshal.ReleaseComObject(wrkBook);
发布对象(书籍);
someWorkerThread=新线程(新参数化的ThreadStart(threadHand