index > Visual Studio Tools for Office > Multi-threading in VSTO

Multi-threading in VSTO


I have read throught many texts and i am not able to get to a conclusion.

I have an excel application. The application queries various tables. After data is obtained through each table it is processes. Doing everything serially takes a lot of time.

Is there any way i can put each task on a separate thread. None of the threads will be calling the Excel COM server. The tasks will perform data access/processing operations and once all the threads are done, the MAIN thread will do the writing on the workbook.

In other words "Is is possible to do multi threading in VSTO solutions, assuming that threads won't interact with the office object" ??

Thanks1!!
TkNeo

Hi,

It sounds like you might benefit from reviewing the COM threading documentation which can be found here: http://windowssdk.msdn.microsoft.com/en-us/library/ms693344.aspx. Hopefully, that will fill in any of the gaps in your reading of other threads.

It is possible to do multi-threading in VSTO solutions regardless of whether your threads interact with the Office OM. However, in the latter case, you need to implement IMessageFilter (the COM version, not System.Windows.Forms version) and you need to understand that all calls into the Office OM will be serialized so while one thread is calling into Office, the remaining threads would be blocked from doing so.

All of this is because the Office object model isn't thread safe. Since COM can't control the fact that a client might try to call a non-thread safe server with multiple threads, it does the next best thing which is to serialize the calls. On the other hand, if you are calling into a component that is thread-safe, then COM doesn't need to get in the way and concurrency is allowed.

As I said, it is always possible to use multiple threads in VSTO. The issue that you need to understand is how much time your threads will spend being blocked and whether that will invalidate the performance gain you are hoping for. If your tasks are truly self-contained (or call into Free Threaded components) then you won't blocked by COM if you run them on background threads.

Sincerely,

Geoff Darst

Microsoft VSTO Team

Geoff Darst - MSFT

Geoff,

That was very helpful and i have been able to test some multi threading in VSTO code too. I also was able to see how Office objects reject calls from outside threads if they are busy themself. I did that by starting to edit some cell right when my code made a COM call. To be able to handle this problem gracefully i guess i will need to implement the IMessageFilter interface as you mentioned.

I am a bit confused on what code i need to add in the preFilterMessage method and whats it purpose. And then i guess i also have to implement the RetryRejectedCall method as i want to try calling the COM again until my call gets lucky and excel is able to respond. Can you please post some sample code as i was unable to find it elsewhere in the VSTO perspective of things? Either VB.NET or C# is fine. There is no such code at all in the entire forum and i am sure many people will find it useful.

If all this goes through successfully i am sure i will be able to convince my manager to start using VSTO as compared to the VBA we do right now.

Thanks!!

TkNeo

Hi,

The Windows Forms team did the world no favors when they reused the IMessageFilter name for their interface. System.Windows.Forms.IMessageFilter is a way to hook the message pump for a Windows Forms application; it has nothing to do with the COM IMessageFilter. What you actually need to do is the following:

1. Implement the COM version of IMessageFilter: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/html/7e31b518-ef4f-4bdd-b5c7-e1b16383a5be.asp. Since this is a native interface, you will need to create the runtime callable wrapper for this yourself. The only method that you actually need to implement is RetryRejectedCall. The other methods apply only if you are functioning as a COM server (which your thread will not be). You can just throw a NotImplementedException for those methods. The typical implementation of RetryRejected call would be to retry the call if less than some amount of time has elapsed; otherwise show the OLEUIBUSY dialog by calling OleUIBusy.

2. Register your IMessageFilter for the thread by calling CoRegisterMessageFilter. This (as well as OleUIBusy) are Win32 calls and you will need to construct your own P/Invoke declarations.

I'm sorry that I don't have a sample to offer you. Unfortunately, this isn't something I have the time to put together right now--perhaps someone else on the forum has some code to share.

Sincerely,

Geoff Darst

Microsoft VSTO Team

Geoff Darst - MSFT
I think i will be able to figure out on how the make the CoRegisterMessageFilter Win32 call. I will just to import the DLL that has the declaration for
TkNeo

A good place to discuss this (and search for past posts on the topic) would be the Interop newsgroup:

http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.dotnet.framework.interop&lang=en&cr=US

Currently, there are no "interop" forums, but I understand some are planned :-) But for the moment there's only this newsgroup, with a pretty good "base".




-- Cindy Meister (Word MVP)
Cindy Meister

Currently, there are no "interop" forums, but I understand some are planned :-)

<g>

That is when there will be an agreement for the definition of "interop". Is it the "interop" used as in this context, or in the context of iteroperability between all kinds of "systems" ?

Let's wait and see, for now the "interop" newsgroup is ok in this context I guess ... ;-D

-= Maarten =-




Software Engineer * MVP-Visual Developer-VSTO
Maarten van Stam
ok i did post it on that forum. Thanks Cindy. Lets see how it goes. I find myself as the first person EVER to be implementing anything like this :)
TkNeo
reply 8

You can use google to search for other answers

 

More Articles

problems with dialogs loaded from ms office.
Open Workbook
VSTO-2 (2003) Word Template code-behind assembly doesn't run
Loop through all host control
VSTO 2005 SE Beta, Actions Pane, and Office 2003
project inclusion( assemblies) in vsto
Invalid Characters for Excel Sheet Names
VSTO 2.0 Excel Project Recurring Compile Error
Access Runtime
A Few Questions re WinFX/VSTO capabilities.
Welcome to Bokebb   New Update   Joins the collection  
 

New Articles

Creating a shortcut on the outlook short…
multiple windows
How do you obtain the paragraph numbering?
Excel Smart document variable passing
Problem regarding the add ins for outlook
Grant full trust using setup project
MS Office Automation Question?
VSTO 2005 Infopath project template miss…
automating Excel
Inserting hyperlink programmatically int…
Catastrophic Failure with Outlook Redemp…
Runtime Library
API request: WM_INITMENU event
Office Pro Small Business Edition & …
VSTO 2005 - Problem loading excel file f…

Hot Articles

ViewAdd event does not work on manually …
Visual Studio 2005 Word and Outlook 2003
Excel errors
Outlook Addin to Interact With External …
How to handle Event of Command Bar Button
Using VSTO to Process data in a word doc…
HtmlBody problem's
multiple windows again and again
[VSTO2005] Excel app. with Data Base
Install problems
outlook 2007 categories property
Install issue - ServerDocument cannot wr…
How to group the shapes within Excel Wor…
Avoiding security alert in Outlook while…
How to Convert an Excel Sheet to XML for…

Recommend Articles

Installed OutLook 2003 Add-in not availa…
smart Document (XML Expansion Pack is in…
Office 2007 Custom Ribbon
Wrap VSTO Projects in other Projects?
Can I use Actions pane control in my sha…
Setting image on a CommandBarButton in M…
Question re Cypress
Error: QueryInterface for interface Micr…
in a web brouser navigation control i ne…
VSTO 2005 Outlook Add-In Security Access…
Moving text within a document without us…
ContentControls & VSTO(v.3 aka 2007)
Bullets = "?" with InsertXML
Measure the width of a string contained …
runtime errors - Outlook add in