Updating UI from a different thread

Recently we came across a situation where we had to update the UI from a thread other than the UI thread in a WPF application. In windows forms this can be easily handled by some thing like
(where this is a Form object or a control object owned by the UI thread)

void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
      this.Invoke(new System.Windows.Forms.MethodInvoker(delegate
()
      {
           this
.ProgressBar.Value = e.ProgressPercentage;
      }));
}

After some digging I figured out that the WPF equivalent of the above is as follow. this here is the Window.

void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
      this.Dispatcher.Invoke(DispatcherPriority.Normal, new System.Windows.Forms.MethodInvoker(delegate()
      {
           this.ProgressBar.Value = e.ProgressPercentage;
      }));
}

Although this works perfectly I’m still using the Windows.Forms.MethodInvoker, need to find an equivalent in the WPF world.

Update: just found this works and doesn’t need the winforms reference anymore.

void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
      this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)( ()=>
      {
           this.ProgressBar.Value = e.ProgressPercentage;
      }));
}

Advertisements
Posted in WPF | 83 Comments

IDataObject WPF Winforms Interop

I encountered  a strange problem today when writing an App which involved WPF and Winform interop. While drag dropping from a winforms control into a WPF control,  the WPF control gives you System.Windows.IDataObject in its drag event args. But I needed the dataObject as a System.Windows.Forms.IDataObject object. After looking for solutions to convert , I figured out this tiny bit of code will takee care of it 🙂
 
System.Windows.Forms.DataObject winformDataobject = new System.Windows.Forms.DataObject();
foreach (string format in wpfDataObject.GetFormats())
{
         winformDataobject.SetData(format, wpfDataObject.GetData(format));
}
Posted in WPF | 56 Comments

Flowchart workflow – FreeFormActivityDesigner sample

Here is the long due sample for writing custom activity designers utilizing the freeform activitydesigner. This is still a very crude sample, so we will try to post improved versions from time to time. Thanks to Arjun for the Executors and Validators. The validators show a false positive sometimes because of a bug that we will be fixing soon.

http://www.thispointer.com/workflows/FlowChart.exe

here is a preview of the flowchart workflowtype get http://www.thispointer.com/workflows/FlowChart.jpg

Posted in Windows Workflow Foundation | 20 Comments

Test Entry

Now that msn spaces expanded the storage limit to 500Mb Im considering making this my main blog
Posted in Uncategorized | 6 Comments