NPRG042 Programming in Parallel Environment

Labs 01 - C# Tasks and GUI

Take a code startup pack cs-para-factor.zip, unzip it and open it in Visual Studio (or your favorite IDE). Please note, that this assignment requires GUI, so it currently works only on Windows.

The objective is to make the computation of the prime numbers parallel whilst not breaking the visualization. The trouble is the GUI updates must be performed from the main thread only. So we need to perform the computation itself in background threads (preferably using the integrated thread pool) and then synchronize with the main thread to visualize the results.

Hint: Probably the easiest way how to implement this is using tasks, which are scheduled in the thread pool. Use the ContinueWith() method to ensure chaining and remember that you can give it an additional parameter that restricts the synchronization context of particular tasks (i.e., so that the follow-up visualization tasks are executed by the main thread).

Do not forget to properly perform all related GUI updates (changing the background color also when the task is started and disabling the buttons whilst the computation is in progress).

Stretch goal: Try using async/await for this assignment and compare the benefits and the drawbacks with the solution based on tasks.


After implementing the solution on your own, you may consult the result notes.