Archive

Posts Tagged ‘PLINQ’

.Net 4.0 first tasting – Task Parallel Library (TPL)

March 11th, 2011 1 comment

Finally our project was “upgraded” to VS2010 and .Net 4.0, we was waiting anxiously for that event.
In first opportunity I have tried to use new language/Framework features like Parallel.Foreach and PLINQ.

My task was to aggregate duration time of recorded stuff (it doesn’t really matter what data I needed to sum). In order to retrieve required data and calculate it – we have had to communicate with the server (TCP protocol). Such operations of course are time consuming so I decided to use Parallel.Foreach.

See following code example:

15 public TimeSpan GetRecordedDuration(int recordingId)

16 {

17       TimeSpan duration = TimeSpan.Zero;

18       Parallel.ForEach(GetGroups(recordingId), group =>

19       {

20

21         var bookmarks = m_clientRecordingSys.GetGroupBookmarks(StreamsNames.VideoSourceName, group);

22         if (bookmarks.Count <= 1)

23         {

24             continue;

25         }

26         else

27       {

28       var startTime = TimeSpan.FromTicks(bookmarks[0].CreationTime);

29 var endTime   = TimeSpan.FromTicks(bookmarks[bookmarks.Count – 1].CreationTime);

30 duration += endTime – startTime;

31    }

32  }

33    );

34   return duration;

35

36 }

37

38 }

Read more…

Categories: .Net Tags: ,