NPRG042 Programming in Parallel Environment

Labs 01 - C# Tasks and GUI

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

Alternative: One of the former students implemented a similar application using Avalonia UI, which is a cross-platform GUI framework. If you run Linux or Mac, you may try using cs-para-factor-avl.zip as the starter pack instead (no guarantees).

The objective is to make the prime-number computation parallel without breaking the visualization. The trouble is that GUI updates must be performed only from the main thread. So we need to run the computation itself on background threads (preferably using the integrated thread pool) and then synchronize with the main thread to visualize the results.

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

Do not forget to properly perform all related GUI updates (including changing the background color when the task starts and disabling the buttons while 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.