VSTO:在主 Excel 线程上调用
我在 Excel 工作表上有一个按钮,可以启动一个新线程来进行一些处理。如果我想对 Excel 进行任何更改(例如使用 Worksheet.Range("A1").Value = "info"; 将数据写入单元格),我想我必须使用主 UI 线程。
这怎么能做到呢?
通常在 Winforms 中,我会在控件上调用 Invoke
,但 Excel.Application
或 Worksheet
或 Range
对象不会没有 Invoke
方法。
I have a button on an Excel sheet which starts a new thread to do some processing. If I want to make any changes to Excel (e.g. write data to a cell using Worksheet.Range("A1").Value = "info";
), I think I must use the main UI thread.
How can this be done?
Typically in Winforms I would call Invoke
on a control, but the Excel.Application
or Worksheet
or Range
objects don't have an Invoke
method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这项工作并不“需要”在 UI 线程上完成,.net 将为您整理调用,但如果您从后台线程重复调用,您可能会遇到性能问题。
但要具体回答您的问题,如果您有 .net 3.5,请在加载项加载事件中添加以下内容:
然后添加:
然后您可以通过以下方式分派到 UI 线程
如果您没有 .net 3.5,则有还有一些其他线程同步技术,例如使用 SynchronizationContext.Current 而不是 Dispatcher。
That work doesn't 'need' to be done on the UI thread, .net will marshal the call for you, but if you make repeated calls from a background thread you may hit performance issues.
But to answer your question specifically, if you have .net 3.5, in your add-in load event add this:
And then add:
Then you can dispatch to the UI thread by going
If you don't have .net 3.5, then there are a few other thread synchronisation techniques, like using SynchronizationContext.Current instead of the Dispatcher.
这是我使用 WindowsForms 的 VSTO AddIn 解决方案。
您不需要任何 System.Windows.Forms.Control 即可使用它:
在 ThisAddIn 类中初始化:
将此行添加到“ThisAddIn_Startup”函数:
添加此新属性:
然后使用工作线程是:
第二个解决方案(未测试):您也可以使用:
希望它有帮助,Jörg
This is my solution for a VSTO AddIn using WindowsForms.
You don't need any System.Windows.Forms.Control to use it:
Initialization in class ThisAddIn:
Add this line to "ThisAddIn_Startup" function:
Add this new property:
Then the usage in the worker thread is:
A second solution (Not tested): You could maybe also use:
Hope it helps, Jörg
您是否尝试过从按钮启动BackgroundWorker?这使得事情变得非常容易,因为 ProgressChanged 和 RunWorkerCompleted 事件将在主线程上触发。
我还没有在 Excel/VSTO 环境中尝试过这个,但我不明白为什么它不起作用。
Have you tried starting a BackgroundWorker from your button? This makes it very easy as the ProgressChanged and RunWorkerCompleted events will fire on the main thread.
I haven't tried this in an Excel/VSTO environment but I don't see why it wouldn't work.