Skip to content

Posts tagged ‘Delegates’

21
Jun

Managing multiple threads in C# with a GUI

Objective: manage multiple threads from C# with a responsive GUI
Description:: In this example, I simulate encoding from a source that is splittable in segments. It could be the b-frames between I-frames in a video or even some sort of audio codec. To simulate the workload, I use Thread.Sleep(TimeInMs).

bggui

For this code, I have 4 threads working on a source to encode of 10 segments. Each threads will take a slice to encode, process it and reassemble the end result in an array, that can be utilized by another process to unify the encoded version, for example.

A word of caution though: although working with threads can be useful in some cases it also complicates the code quite a bit, especially at debugging. It’s much harder to keep track of what is going on.

Here is the code in the windows form (download the whole project in VS2008 at the end):

Read moreRead more