Asynchronous tasks on Winform

Posted on : 11-05-2009 | By : manitra | In : C#, Developpement, WinForm

2

The problem

When you create winform applications, doing tasks in the background is essential to avoid user frustation. Unfortunatly, it could make you write a lot more code. Here are some utility methods that reduce the complexity of asynchronous calls within windows forms.

Within you base class

You probably have a common base class for all your UI components. Add these methods :

  1. namespace TestWinForm
  2. {
  3.     public class BaseForm : Form
  4.     {
  5.         // Execute some code in async mode.
  6.         // When it's done, it calls the nextStep delegate, eventually with
  7.         // an exception catched during the main action.
  8.         protected virtual void Async(Action action, Action nextStep)
  9.         {
  10.             new Thread(delegate()
  11.             {
  12.                 Exception exception = null;
  13.                 try
  14.                 {
  15.                     action();
  16.                 }
  17.                 catch (Exception ex)
  18.                 {
  19.                     exception = ex;
  20.                 }
  21.                 ThreadSafe(() => { nextStep(exception ); });
  22.             }).Start();
  23.         }
  24.  
  25.         // This allows a sub class to easily run a method within
  26.         // an UI thread without the need of creating multiple
  27.         // delegate signatures for each method signatures
  28.         protected virtual void ThreadSafe(MethodInvoker method)
  29.         {
  30.             if (InvokeRequired)
  31.                 Invoke(method);
  32.             else
  33.                 method();
  34.         }
  35.     }
  36. }

Within your UI classes

Now the only thing you need to do is to encapsulate the methode content with the Async() method :

namespace TestWinForm
  1. {
  2.     public partial class MainForm : BaseForm
  3.     {
  4.         public MainForm()
  5.         {
  6.             InitializeComponent();
  7.         }
  8.  
  9.         // Here is the async trick :
  10.         // – UI will NOT freeze,
  11.         // – you can add beautifull animated gifs
  12.         private void button1_Click(object sender, EventArgs e)
  13.         {
  14.             DateTime? result = null;
  15.             Async(
  16.                 () =>
  17.                 {
  18.                     result = GetComplexDate();
  19.                 },
  20.                 (ex) =>
  21.                 {
  22.                     if (ex == null)
  23.                         textBox1.Text = result.Value.ToShortDateString();
  24.                     else
  25.                         textBox1.Text = ex.Message;
  26.                 }
  27.             );
  28.         }
  29.  
  30.         // This is the slow, data-intensive task :p
  31.         private DateTime? GetComplexDate()
  32.         {
  33.             Thread.Sleep(3000);
  34.             return DateTime.Now;
  35.         }
  36.     }
  37. }

The traditional way

Just in case you didn’t get it. This is what you should NOT DO:

// This was traditional way :
  1.         // – UI will freeze until during 3 second …
  2.         // – you users will complain
  3.         // avoid this !
  4.         private void button1_Click2(object sender, EventArgs e)
  5.         {
  6.             try
  7.             {
  8.                 textBox1.Text = GetComplexDate().ToShortDateString();
  9.             }
  10.             catch (Exception ex)
  11.             {
  12.                 textBox1.Text = ex.Message;
  13.             }
  14.         }

Happy coding !

Make InvokeRequired/Invoke easy

Posted on : 23-01-2009 | By : manitra | In : C#, Developpement, WinForm

5

The problem

If you’re working on WinForms, you must know that you cannot call controls methods within a thread that is not the one that created those controls. To solve this problem, Microsoft recommend us to use the following code :

  1. namespace TestWinForm
  2. {
  3.     public partial class MainForm : BaseForm
  4.     {
  5.         public MainForm()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.         // a delegate that has been created specially for this method
  10.         private delegate void DisplayDelegate(string text);
  11.  
  12.         // a method that may be called from a worker thread
  13.         public virtual void Display(string text)
  14.         {
  15.             if (InvokeRequired)
  16.             {
  17.                 Invoke(new DisplayDelegate(Display));
  18.             }
  19.             else
  20.             {
  21.                 //the actual job is here
  22.                 textBox1.AppendText(text);
  23.             }
  24.         }
  25.     }
  26. }

This code is ugly because :

  • you need to create a delegate for each single public method you can call from outside
  • you need to put an “if/else” block in each method wich increase the complexity of your code

The trick

Here is a trick that could significantly reduce the amount of code needed to do the same job within a large project.

Within you base class

You probably have a common base class for all your UI components. Add this method :

  1. namespace TestWinForm
  2. {
  3.     public class BaseForm : Form
  4.     {
  5.         // This allows a sub class to easily run a method within
  6.         // an UI thread without the need of creating multiple
  7.         // delegate signatures for each method signatures
  8.         protected virtual void ThreadSafe(MethodInvoker method)
  9.         {
  10.             if (InvokeRequired)
  11.                 Invoke(method);
  12.             else
  13.                 method();
  14.         }
  15.     }
  16. }

Within your UI classes

Now the only thing you need to do is to encapsulate the methode content with the ThreadSafe() method :

  1. namespace TestWinForm
  2. {
  3.     public partial class MainForm : BaseForm
  4.     {
  5.         public MainForm()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.         // public method that may be called from outside and within any
  11.         // worker thread …
  12.         public virtual void Display(string text)
  13.         {
  14.             ThreadSafe(delegate
  15.             {
  16.                 //do any UI related code here
  17.                 //note that because this is an anonym method,
  18.                 //you can use the local parameter
  19.                 textBox1.AppendText(text);
  20.             });
  21.         }

Happy coding !

A .NET fault tolerant web service framework implementation

Posted on : 29-07-2008 | By : manitra | In : C#, Developpement, Web service

1

Here is a .NET implementation of a fault tolerant web service framework.

The idea

One of the bad sides of web services is that they just regularly fail. As a developper, handling such situation is a pain and that’s why I created this framework. It has two main parts :

  • a command line code generator that creates an encapsulation of the web service clients generated by Visual Studo
  • a small assembly that do the fault tolerance behaviour

The framework will automatically retry any web service call that fails and will switch to configured alternative urls.

Features

  • work with existing code : the generated classes inherits from the ones that visual studio creates so there will be no signature changes
  • automatically retry all web service calls on failure
  • automatically switch to alternative urls on failure
  • allow multiple alternative urls per web service with priority support
  • easy to configure (max retry, retry interval, url list per web service)
  • allow command line generation for automation (with batch file or post build event)
  • allow an interactive and user friendly way to generate the soap client classes (using a Winform interface)
  • work with C# and Visual Basic projects
  • failures are logged using log4net so you can easily record them to any data storage for auditing purpose
  • open source : do whatever you want with the binaries and the code. Just share you enhancements

Files

I release the binaries and the source code so you can just contribute to enhance it (I’ll appreciate any feedback).

How to install and use it

Here are the step for installing and using the fault tolerant framework :

Basic steps

  • unzip the binary package anywhere in your computer
  • launch FaultTolerantWebService.Ui.exe
  • click the “Load” button and select the .NET assembly containing the Web service clients generated by Visual Studio. You can see the generated code in the main text box now.
  • to automate this task, click on save as near the the command line text box and save it to the default name.
  • add a reference to the FaultTolerantWebService.Common.dll in the project containing the Web refences.
  • add the generated file named FaultTolerantWebService.cs in your project (by default, it will located on the root of your project )

You’re done ! Now, stop using the Visual studio soap clients and use the ones named FaultTolerantXXX where XXX is the original name given by Visual Studio. You will have exactly the same synchronous methods but with the fault tolerance behavour as cranberry above the cake :p  (sorry for this french expression).

Additional steps

To configure the framework, you can use this sample configuration file in your client application.

The road map

The next steps will be :

  • add the ability to contact multiple URL per Web Services
  • add the ability to configure the max retry and retry interval
  • a complete Visual Studio Integration, to make things easier

So if you want to help, just download the code and send me patches !

A great, open source, Visual Studio 2005/2008 Addin

Posted on : 21-03-2008 | By : manitra | In : C#, Developpement, Visual Studio Addin

1

I’d like to give an introdution of a great and open source addin for Visual Studio 2005/2008. Its name is Koda and its main features are:

  • generate Constructos and properties from existing fields
  • fast type/file search
  • goto Test
  • Collapse all project
  • Close all documents

More features are comming soon !

If you don’t want to pay for addin like resharper this one is the best “free” alternative.

To make it short, here are the project info :

Name : Koda

Url : http://www.codeplex.com/koda

Cost : Free

Source : Open source, hosted by CodePlex

Language : C#