Monday, July 5, 2010

#003 Invoking from UI Thread

Sometimes you just have to invoke a single line of code in the UI thread. The (very) old school way to to this is the following:

public delegate void VoidDelegate();
protected void WriteToConsole()
{
   Console.WriteLine("Invoked from UI Dispatcher");
}
...
View.Dispatcher.Invoke(new VoidDelegate(WriteToConsole));

To save time (and money), you can do this by using the System.Action delegate, which is a simple void delegate. By using anonymous methods, you additionally reduce the coding overhead by eliminating the need to create a separate method.

View.Dispatcher.Invoke(new 
Action(delegate{ 
Console.WriteLine("Invoked from UI 
Dispatcher"); })); 

Alternatively you can use lamda expressions for the delegate, which also reduces the coding by some chars...

View.Dispatcher.Invoke(new Action(() => 
  Console.WriteLine("Invoked from UI Dispatcher")));

Tuesday, June 29, 2010

#002 Render ReportViewer as PDF

If you want to programatically save a Microsoft.Reporting.WinForms.ReportViewer as a PDF to the filesystem or a database, the Render(...) Method will help you out by returning a byte array of the rendered file content.

Here is the code snippet:


   1:  byte[] fileData = null;
   2:  Warning[] warnings;
   3:  string[] streamids;
   4:  string mimeType;
   5:  string encoding;
   6:  string extension;
   7:   
   8:  fileData = reportViewer.LocalReport.Render(
   9:   "PDF", null, out mimeType, out encoding,
  10:    out extension,
  11:   out streamids, out warnings);

Monday, May 24, 2010

#001 create your own blog

As a software developer I'm often forced to do some ingenious hacks to get things working. So this blog should be a nice little work of reference which helps me not to forget how I solved those problems. Maybe I will also post some trivial code snippets which helps simplifying a developers life. Providing these informations to other developers and support them would be a cool side effect...