问题
We have a VSTO addin for Outlook 2007 developed using .Net 4.0 WPF. One of the PC where it got deployed, is having an issue with UI. At some occasions UI becomes unresponsive for few moments. Clicks does not work. After few moments everything gets back to normal and user can click the buttons. Other addins installed on the PC are - SnagIT and Google Desktop Outlook Toolbar.
Please help us if you have any input on this kind of issue.
回答1:
I had the same issue with a Word Add-In -- on a very beefy developer box -- the UI would just lock up even though I was doing all my work in the background.
My theory is that Word was doing work on the UI thread even though I was invoking it from background threads, etc.
I tried an experiment -- instantiating the Window object on a new Thread (i.e. so that my UI thread is not the same thread as Word's worker thread) -- and it worked. My UI no longer locks up at random intervals.
Be careful to get rid of any instances of COM objects before your thread closes, and especially careful of cross-thread COM access (potentially using Dispatcher.Invoke back to the Office host app's thread when accessing any of its RCWs... -- Dispatchers are associated with the threads they're instantiated on, so you would just have to instantiate a dispatcher in the office host app's callback, etc...)
Note: If you Dispatcher.Invoke back to the calling thread, you can't use Thread.Join as shown below. (In that case, you would need to handle application events, Thread.Sleep, and Thread.Yield) in a busy loop. Otherwise, you'll get a deadlock.
Here's a genericized example version of what I'm doing:
// This approach makes WPF Windows an order of magnitude more responsive
Thread t = new Thread(() =>
{
try
{
// Implement the IDisposable pattern on your window to release
// any resources before the thread exits
using (var myWindow = new MyWindow())
{
// Do any other pre-display initialization with the myWindow
// object here...
myWindow.ShowDialog();
}
}
finally
{
// Strongly Recommended (not doing this may cause
// weird exceptions at application shutdown):
Dispatcher.CurrentDispatcher.InvokeShutdown();
}
});
t.SetApartmentState(ApartmentState.STA); // Required
t.IsBackground = false; // Recommended
t.Start(); // Kicks off the new UI thread
t.Join(); // Blocks execution until the new UI thread has finished executing...
回答2:
It sounds like you are performing work on the UI thread which does freeze the UI until the task is completed. You should be using the Tasks library to perform work that is not UI-related. If you are doing work using a Task
that requires the UI to be updated, you need to use the WPF Dispatcher.
来源:https://stackoverflow.com/questions/8237394/vsto-wpf-outlook-addin-unresponsive-ui-at-random-occasions