Wednesday, February 20, 2013
Visual Tree and Logical Tree
Dispatcher & Thread Affinity
When WPF application starts, it actually creates two threads automatically. One is Rendering Thread, which is hidden from the programmer, so you cannot use the rendering thread directly from your program; while the other is Dispatcher Thread, which actually holds all the UI elements. So in other words, you might say Dispatcher is actually the UI thread which ties all the elements created within the WPF application. Conversely, WPF requires all the UI elements to be tied with Dispatcher thread, this is called Thread Affinity. Thus you cannot change any element created on Dispatcher
thread from any other threads, thus it follows the same Win32 based APIs. Thus it allows you to inter-operate any WPF component into HWND
based API. For more, read this. [^]
Dispatcher
is a class that handles thread affinity. It is actually a prioritized message loop through which all elements are channeled through. Every UIElement
is derived from DispatcherObject
which defines a property called Dispatcher
which points to the UI thread. Thus from any other thread, if you want to invoke or access UI component, you need to Invoke using Dispatcher
thread. DispatcherObject
actually has two chief duties, to check and verify if the thread has access to the object.
Tuesday, February 19, 2013
Routed Events in WPF / Silverlight
- Tunneling The event is raised on the root element and navigates down to the visual tree until it reaches the source element or until the tunneling is stopped by marking the event as handeld. By naming convention it is called Preview... and appears before corresponding bubbling event.
- Bubbling The event is raised on the source element and navigates up to the visual tree until it reaches the root element or until the bubbling is stopped by marking the event as handled. The bubbling event is raised after the tunneling event.
- Direct The event is raised on the source element and must be handled on the source element itself. This behavior is the same as normal .NET events.
Create a Custom Routed Event
// Register routed event
public static readonly RoutedEvent SelectedEvent =
EventManager.RegisterRoutedEvent( "Selected", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(MyCustomControl));
// .NET wrapper
public event RoutedEventHandler Selected
{
add { AddHandler(SelectedEvent, value); }
remove { RemoveHandler(SelectedEvent, value); }
}
// Raise the routed event "selected"
RaiseEvent(new RoutedEventArgs(MyCustomControl.SelectedEvent));
Thursday, February 14, 2013
AntiPatterns
Tuesday, February 12, 2013
LINQ GroupBy Count
class Program
{
static void Main(string[] args)
{
var employeeProducts = new List<EmployeeProduct>();
employeeProducts.Add(new EmployeeProduct(1, 2, "XYZ"));
employeeProducts.Add(new EmployeeProduct(1, 5, "ZXY"));
employeeProducts.Add(new EmployeeProduct(2, 2, "XYZ"));
var way1 = employeeProducts.Select(
ep => new ProductCount
{
ProductNumber = ep.ProductID,
EmployeeNumber = ep.EmployeeID,
CountProducts = employeeProducts.Count(epc => epc.ProductID == ep.ProductID)
});
var way2 = employeeProducts
.GroupBy(ep => ep.ProductID)
.SelectMany(epg => epg.Select(
ep => new ProductCount
{
ProductNumber = ep.ProductID,
EmployeeNumber = ep.EmployeeID,
CountProducts = epg.Count()
}));
}
public class EmployeeProduct
{
public EmployeeProduct(int employeeID, int productID, string name)
{
EmployeeID = employeeID;
ProductID = productID;
Name = name;
}
public int EmployeeID { get; set; }
public int ProductID { get; set; }
public string Name { get; set; }
}
public class ProductCount
{
public int ProductNumber { get; set; }
public int EmployeeNumber { get; set; }
public int CountProducts { get; set; }
}
}
Types of WCF concurrency
- Increase throughput: Many times you want to increase the amount of work your WCF service instance does at any moment of time. In other words, you would like to increase the throughput. Throughput means how much work a given thing can do. By default, a WCF service handles only one request at a given moment of time.
- Integration with a legacy system: Many times your WCF services interact with legacy systems like VB6, COM, etc. It’s very much possible that these systems are not multithreaded, in other words they handle only one request at any given time. So even though your WCF service has concurrent capabilities, you would still like to handle one request at a time. This is achieved by using throttling in combination with WCF concurrency capabilities.
WCF Service Throttling
Attribute
|
Description
|
maxConcurrentCalls
|
Limits the total number of calls that can currently be in progress across all service instances. The default is 16.
|
maxConcurrentInstances
|
The number of InstanceContext objects that execute at one time across a ServiceHost. The default is Int32.MaxValue.
|
maxConcurrentSessions
|
A positive integer that limits the number of sessions a ServiceHost object can accept. The default is 10.
|