|
|
2 months ago | |
|---|---|---|
| .. | ||
| .config | 2 months ago | |
| .vscode | 3 months ago | |
| Core | 2 months ago | |
| Platforms | 2 months ago | |
| BackgroundThreadTester.sln | 3 months ago | |
| README.md | 3 months ago | |
This project demonstrates background thread management in MonoGame applications.
This project has been modernized to use SDK-style projects with MonoGame 3.8.4 NuGet packages:
# Windows version
dotnet build BackgroundThreadTester.Windows.csproj
# DesktopGL version
dotnet build BackgroundThreadTester.DesktopGL.csproj
# Android version (requires Android SDK)
dotnet build BackgroundThreadTester.Android.csproj
# Windows version
dotnet run --project BackgroundThreadTester.Windows.csproj
# DesktopGL version
dotnet run --project BackgroundThreadTester.DesktopGL.csproj
The project includes .vscode/tasks.json and .vscode/launch.json for building and debugging in VS Code.
Available VS Code configurations:
Quick Start:
F5 to start debugging📖 Troubleshooting: See VSCODE_LAUNCH_GUIDE.md for detailed setup and troubleshooting instructions.
Open BackgroundThreadTester.sln in Visual Studio 2022.
This project demonstrates modern .NET threading best practices:
// Modern approach using async/await
private async Task CreateBackgroundTaskAsync(CancellationToken cancellationToken)
{
await Task.Delay(2000, cancellationToken); // Non-blocking delay
}
// Cancellation token support
cancellationToken.ThrowIfCancellationRequested();
// Thread-safe component addition
lock (_componentLock)
{
Components.Add(testTexture);
}
try {
// Background work
} catch (OperationCanceledException) {
// Handle cancellation
} catch (Exception ex) {
// Handle other errors
}
Sample originally created by CircleOf14 and modified by Kenneth Pouncey to create new textures to be added to the game components dynamically in the background.
Of special interest look at the following methods:
CreateBackgroundThread() - Creates a modern async background task with cancellation supportCreateBackgroundTaskAsync() - NEW Modern async worker using Task-based patterns with proper error handlingBackgroundWorkerThread() - Legacy method (now redirects to modern implementation for compatibility)Task.Delay() instead of Thread.Sleep() for non-blocking operationsCancellationTokenMake sure to read the comments in these two methods.
This project uses MonoGame 3.8.* NuGet packages for better dependency management and cross-platform support.