tanjera / infirmary-integrated

Medical device simulator for training healthcare professionals.
http://www.infirmary-integrated.com/
Other
42 stars 5 forks source link

Race Condition (?) Breaks Initialization Process #169

Closed tanjera closed 2 years ago

tanjera commented 2 years ago

When loading II under moderate/heavy CPU load, program does not initialize properly- patient parameters do not load or propagate, devices do not initialize with patient events properly, etc. Highly suspect this is due to race conditions in async functions during initalization.

Proposed fix: replace all discards "_" from initialization functions with "await" for Windows, Devices, UserControls... if need to create a separate function (e.g. InitAsync) and call that from Init, that could still solve the issue.

tanjera commented 2 years ago

Methods for implementing fix for this issue:

Wrap any async Init() calls from synchronous methods in a Task.Run(async () => {}) to ensure the Task contained within is actually awaited (and completes before moving on with other AvaloniaUI tasks!) Like so:

// e.g. within Window constructor
Task.Run (async () => {
  await Init ();
});

Then ensure all initialization within async Init() that may touch the UI happens on the UI thread using the following Dispatcher wrap:

await Dispatcher.UIThread.InvokeAsync (async () => {
  // all UI initiation here!
});

Any areas this is specifically not applicable to, mark in /* Takes place on UI thread- does not need to be added to Dispatcher.UIThread */

tanjera commented 2 years ago

Tentatively fixed in 822c7f55f66ecb048c09edebec214397cde23ea5 ... significant and deliberate improvements made to handling of async functions and use of sync-to-async transitions during all initialization processes... brownfield de-implementation of async among all init-specific functions. Also improved stability with edits made for issue #168

Will close issue for now- will re-open if discovered as not fixed.