Sunday, June 9, 2013

Parallele und asynchrone Datenverarbeitung mit .Net: Ein Blick in die Toolbox

Das ist der Titel eines Vortrags, den auf dem .Net Day Franken halten werde. Darin geht es um verschiedene Frameworks für asynchrone und parallele Programmierung und welches man für welche Probleme am besten einsetzen kann.

Hier will ich schon mal das Ergebnis zusammenfassen und nützliche Links posten.

Welches Framework für welches Problem?

Parallel.For

  • Um unabhängige Aktionen auf mehreren Datensätzen parallel auszuführen

PLINQ

  • Für die Manipulation von Listen (filtern, gruppieren, aufsummieren)
  • Für Transformation von Objekten

Async Functions

  • Zur Kombination asynchroner Methoden

Reactive Extensions

  • Zum Verarbeiten von Daten- und Ereignis-Strömen
  • Zur Kombination von Ereignissen aus unterschiedlichen Quellen

TPL Dataflow

  • Für agentenbasierte Systeme
  • Zum Buffern und Verteilen von Daten zwischen verschiedenen Verarbeitungsschritten

 

Nützliche Links


Parallel.For und. PLINQ

Async Functions

  • Three Essential Tips For Async von Lucian Wischik
    Eine Video-Serie über die häufigsten Fehler mit Async Functions und wie man sie vermeidet. Enthält einige (3)nützliche Tipps.

Reactive Extensions

  • RX Design Guidelines
    Eine gute Übersicht über Patterns für RX. Enthält gleichzeitig einige gute Beispiele.
  • Introduction to Rx von Lee Campbell
    Ein komplettes Online Buch über Rx mit vielen einfachen Beispielen
  • ReactiveUI von Paul Betts
    Ein MVVM Framework auf Basis von RX

TPL DataFlow

 

Viel Spaß beim Lesen

Peter

Friday, November 23, 2012

Asynchrone Programmierung in Franken

Gestern habe ich meinen “Vortrag Asynchrone Programmierung in C#” bei der dodned User Group Franken in Nürnberg gehalten.

Die Beispiele und die Präsentation gibts hier.

Danke für die Einladung und herzlichen Dank an alle die da waren. Und natürlich auch vielen Dank an die infoteam Software AG für die belegten Semmeln.

Thursday, October 25, 2012

Continuous asynchronous Ping using TAP and IProgress in C#5

When I prepared the talk for the .Net User Group, I realized that the TAP infrastructure can be (mis-)used for some cool things, that would be not so easy without that features.

I finally found the time to write a short article about using the TAP to write an easy wrapper for the .Net Ping class. You can find the article at CodeProject:

Continuous asynchronous Ping using TAP and IProgress in C#5

If you find that useful or have any other ideas for cool features using TAP please let me know.

Saturday, September 29, 2012

.Net User Group Vortrag: Asynchrone Programmierung in C#

Diese Woche habe ich meinen zweiten Vortrag bei der .Net User Group Regensburg gehalten. Nach einer Einführung in F# im letzten Sommer, war dieses Jahr Async Functions in C# 5 an der Reihe.

Die Beispiele und die Präsentation gibts hier.

Ohne die Präsentation gehört zu haben, ist das Material vermutlich wenig hilfreich. Wer einen guten Einstieg in die Thematik sucht, sollte sich LINQPad herunterladen. Im Bereich “Samples” lassen sich mit “Download more samples …”, wie zu vermuten war, weitere Beispiele installieren. Das “Asynchrony in C#5 Interactive Tutorial” von Joseph Albahari bietet einen guten Einstieg in die Thematik.

The Task-based Asynchronous Pattern von Stephen Toub beschreibt neue das Pattern detailliert und kompakt.

Tuesday, April 12, 2011

FSharpPlot: Copying charts as EMF to the clipboard

F# is a very handy tool when it comes to playing with new algorithms or data analysis. However an analysis is useless if you can’t present your results. Some days ago Don Syme published with FSharpPlot a wrapper for .Net 4.0 charting controls. It is a handy tool when analyzing data with F# Interactive and you can learn some interesting F# techniques from the sources.

FSharpPlot comes with the possibility to save charts as png file or copy them (also as png) to the clipboard (just right click the chart). That is fine, if you want to look at the chart on a computer screen. However if you want to produce printable reports you might want to resort to a vector file format like emf.

Microsoft Chart Controls come with the possibility to produce three kinds of emf-files. Examples how to save a plot can be found in the Samples for Chart Controls. But as it turns out copying an emf as metafile to the clipboard is not straightforward from the .Net Framework as described in KB article 323530.

Following this Knowledge Base article, I introduced a new private module to FSharpChart to be able to access the functionality from the native Windows API.
module private MetafileToClipboard =
  // Following http://support.microsoft.com/kb/323530
  module private Intern = 
    open System
    open System.Runtime.InteropServices

    [<DllImport("user32.dll")>]
    extern bool OpenClipboard(IntPtr hWndNewOwner);
    [<DllImport("user32.dll")>]
    extern bool EmptyClipboard();
    [<DllImport("user32.dll")>]
    extern IntPtr SetClipboardData(uint32 uFormat, IntPtr hMem);
    [<DllImport("user32.dll")>]
    extern bool CloseClipboard();
    [<DllImport("gdi32.dll")>]
    extern IntPtr CopyEnhMetaFile(IntPtr hemfSrc, IntPtr hNULL);
    [<DllImport("gdi32.dll")>]
    extern bool DeleteEnhMetaFile(IntPtr hemf);

    open Intern
    open System

    let PutEnhMetafileOnClipboard(hWnd:IntPtr, mf:System.Drawing.Imaging.Metafile) =
      let mutable bResult = false
      let hEMF = mf.GetHenhmetafile()
  
      if not <| hEMF.Equals(new IntPtr(0)) then
        let hEMF2 = CopyEnhMetaFile(hEMF, new IntPtr(0))
        if not <| hEMF2.Equals(new IntPtr(0)) then
          if OpenClipboard(hWnd) then
            if EmptyClipboard() then
              let hRes = SetClipboardData(uint32 14 , hEMF2)
              let bResult = hRes.Equals(hEMF2)
              CloseClipboard() |> ignore
        DeleteEnhMetaFile(hEMF) |> ignore
      bResult

Using this module, copying a chart as emf from within the ChartControl class to the clipboard is as easy as
let copyEmfToClipboard (_) =
  use ms = new IO.MemoryStream()
  chart.SaveImage(ms, ChartImageFormat.EmfPlus)
  ms.Seek(0L, IO.SeekOrigin.Begin) |> ignore
  use mf = new Drawing.Imaging.Metafile(ms)
  MetafileToClipboard.PutEnhMetafileOnClipboard(self.Handle, mf) |> ignore

let miCopyEmf = new ToolStripMenuItem("Copy Emf to Clipboard"
ShortcutKeys = (Keys.Control ||| Keys.Shift ||| Keys.C))
miCopyEmf.Click.Add(copyEmfToClipboard)

I also extended the module ChartExtensions to be able to save the chart directly from a script without opening a ChartForm.
module ChartExtensions =
  type FSharpChart with
    static member SaveImage filename format (width, height) ch =
      let chart = new ChartControl(ch, Size = new Size(width, height))
      chart.SaveImage(filename, format)
      ch

Using this tools you can easily create charts and paste them into Word, Powerpoint or many other applications.

The complete extended script can be downloaded here. It is based on FSharpPlot 0.2 from Don Syme, Tomas Petricek and their team.

The zip file also contains a unified diff file created with TortoiseMerge. You can see all changes with respect to version 0.2 published by Don Syme.

In addition to the functionality for copying emf’s to the clipboard there are some other minor changes:

  • I found a subtle bug at line 3670 of the FSharpChart.fsx where it says
    let typesToClone = 
      [ typeof<LabelStyle>; typeof<Axis>; typeof<Grid>; typeof<TickMark>
        typeof<ElementPosition>; typeof<AxisScaleView>; typeof<AxisScrollBar>; ]

    instead of
    let typesToClone = 
      [ typeof<DataVisualization.Charting.LabelStyle>; typeof<Axis>;
        typeof<Grid>; typeof<TickMark>; 
        typeof<ElementPosition>; typeof<AxisScaleView>; typeof<AxisScrollBar>; ]

  • I changed ChartData.Internal.ChartData and ChartTypes.GenericChart.Create from internal to public since the compiler complained when I tried to build the script into a dll.
Links:
Original version: FSharpPlot
My extended version: FSharpScript.zip