Pages

Saturday, November 24, 2012

WPF Start New Background Process using Task

In GUI programming there is exist some very common problem - if some operation takes too much time the GUI will become unresponsive. For example, if you want establish connection with a server, or fetch some data from database or a large file, this probably will take some time and if you perform this operation in the main thread your GUI will stuck. In order to keep your GUI responsive you need to run long-running operations on a separate thread. So far, in order to open a new thread we used Thread class or BackgroundWorkerStarting from .NET Framework 4.0 we have a Task class for this purpose. 

If you already switched to Visual Studio 2012 with Framework 4.5 then read my previous post - C# Async Programming Example in WPF (Async, Await).
If you still working with Visual Studio 2010 Framework 4.0 then this post is for you.
Today I'll show how to build WPF application with background thread using Task Framework 4.0.

WPF with new background process using Task Framework 4.0

Sunday, November 18, 2012

C# Async Await Example in WPF

C# 5.0 and .NET Framework 4.5 bring to developer a very nice feature called Asynchronous Programming. Asynchronous Programming is based on Task Parallel Library (TPL). As you probably know traditional techniques for writing asynchronous programs are very complicated in many aspects, so Async Programming comes to make our life easier when we write Multithreading applications. 

Today we'll see a very simple example of Async Programming using Async, Await and Task keywords in WPF application. Let's build a simple WPF application with a button and a textbox. By pressing the button we call for some slow method (I named it SlowDude :). I don't want GUI will be locked until the method will finish his job. In other words I want to call that method asynchronously.

C# 5.0 Asynchronous Programming with WPF GUI

Friday, November 16, 2012

C# Application Performance Wizard in Visual Studio

If you want to know how much resources your C# application consumes there is a very nice tool in Visual Studio for this purpose. It allows you to see how much CPU your program uses, amount of memory allocated for it, how much time your program running and many other useful things. Today I'll show how to build a performance test for your C# program in Visual Studio.

Ok, let's suppose we've created some C# application in Visual Studio. In order to run Performance Wizard go to main menu and click on Analyze and Launch Performance Wizard.

Launch Performance Wizard in Visual Studio 2010

Friday, November 9, 2012

7 Regular Expressions a C# Developer Must Know

Regular expressions, aka Regex, are very commonly used by any developer no matter what language he's writing his program. Today I'll show the most useful Regex that sooner or later you will need them.

1. URL Regex:

^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$

Match:
http://www.codearsenal.net/


Not Match:
http://www.codearsenal.net?/



2. IP Address Regex:

^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

Match:
73.180.213.135


Not Match:
255.255.255.256


Saturday, November 3, 2012

WPF ObjectDataProvider - Binding Enum to ComboBox

Let's say we created some Enum data type. Now we want to show our Enum values on WPF ComboBox or ListBox. How do we do that?
In order to achieve this, in WPF we have an ObjectDataProvider class which provides mechanism for creation a XAML object available as a binding source.
Here is our Enum:
public enum TimePeriods
{
    Year,
    Month,
    Day,
    Hour,
    Minute
}
Now let's see how we bind it to a ComboBox in XAML: