English 中文(简体)
Updating a checkbox in Excel 2003 from a background thread in C# fails sometimes
原标题:

I have created an Add-In in C# that implements user defined functions for Excel. These UDF s return immediately, but they control background asynchronous procedures.

These procedures have status that needs to be monitored and presented to Excel. I have chosen to display the status using checkboxes.

A background thread then updates the checkbox linked to a particular background procedure.

I have wrapped the calls to the Excel model with a try/catch block and retry the call after a delay in case an exception is thrown by Excel not being ready. This mechanism works correctly as well.

The problem is that sometimes, even though the code to modify a checkbox runs successfully, the checkbox itself is not modified in Excel.

I have done a sanity check on the checkbox attributes that I am changing and can see they haven t changed in the logs:

                checkBox.Value = false;
                checkBox.Interior.Color = 0x00DDDDDD; // light grey in AABBGGRR format
                checkBox.Caption = "Off";

                // this sometimes prints the values the checkbox had before the above line
                LogDebug( "Color: " + checkBox.Interior.Color + " Value:" + checkBox.Value + " caption:" + checkBox.Caption );

This only happens in Excel 2003. In Excel 2007 the problem as never ocurred.

I ve tried fiddling with Application.ScreenUpdating, Application.Interactive, modifying the COM Apartment thread attributes without any luck. This is not a concurrency issue because I am not changing the checkbox attributes anywhere else.

As anybody had a similar experience?

Thanks!

问题回答

Well, I am hacking a quick fix that works OK for now, though I still don t understand why this happens.

For now, if the set values are not correct after setting them, I treat that as an exception and put the request in a background thread for a future try.

The code then iterates until it manages to update the checkbox.

Don t like it, but it gives out correct behaviour.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签