ktsu.ScopedAction
1.1.3-pre.17
Prefix Reserved
See the version list below for details.
dotnet add package ktsu.ScopedAction --version 1.1.3-pre.17
NuGet\Install-Package ktsu.ScopedAction -Version 1.1.3-pre.17
<PackageReference Include="ktsu.ScopedAction" Version="1.1.3-pre.17" />
<PackageVersion Include="ktsu.ScopedAction" Version="1.1.3-pre.17" />
<PackageReference Include="ktsu.ScopedAction" />
paket add ktsu.ScopedAction --version 1.1.3-pre.17
#r "nuget: ktsu.ScopedAction, 1.1.3-pre.17"
#:package ktsu.ScopedAction@1.1.3-pre.17
#addin nuget:?package=ktsu.ScopedAction&version=1.1.3-pre.17&prerelease
#tool nuget:?package=ktsu.ScopedAction&version=1.1.3-pre.17&prerelease
ktsu.ScopedAction
A lightweight utility for executing paired actions at the start and end of code blocks.
Introduction
ktsu.ScopedAction
is a .NET utility that provides a clean way to execute actions at the beginning and end of code blocks. It leverages C#'s using
statement and the IDisposable
pattern to ensure that paired operations (like resource acquisition/release, state changes, or logging) are properly executed, even in the presence of exceptions.
Features
- Paired Actions: Execute actions when entering and exiting a scope
- Exception Safety: Cleanup actions execute even if exceptions occur
- Lightweight: Simple API with minimal overhead
- Flexible: Works with any action delegates
- Extendable: Can be subclassed for specialized behaviors
- Resource Management: Follows .NET's standard disposal pattern
Installation
Package Manager Console
Install-Package ktsu.ScopedAction
.NET CLI
dotnet add package ktsu.ScopedAction
Package Reference
<PackageReference Include="ktsu.ScopedAction" Version="x.y.z" />
Usage Examples
Basic Example
using ktsu.ScopedAction;
// Execute actions at the beginning and end of a scope
using (new ScopedAction(
onOpen: () => Console.WriteLine("Entering the scope"),
onClose: () => Console.WriteLine("Exiting the scope")))
{
// Any code here...
Console.WriteLine("Inside the scope");
}
// Output:
// Entering the scope
// Inside the scope
// Exiting the scope
Resource Management
// Manage resources with paired acquisition and release
public void ProcessFile(string filePath)
{
using (new ScopedAction(
onOpen: () => Console.WriteLine($"Opening file: {filePath}"),
onClose: () => Console.WriteLine($"Closing file: {filePath}")))
{
// Process the file...
Console.WriteLine("Processing file contents");
}
}
Measuring Performance
// Measure and log execution time of operations
public void TimedOperation()
{
var stopwatch = new Stopwatch();
using (new ScopedAction(
onOpen: () => stopwatch.Start(),
onClose: () => {
stopwatch.Stop();
Console.WriteLine($"Operation completed in {stopwatch.ElapsedMilliseconds}ms");
}))
{
// Operation to be timed
PerformComplexCalculation();
}
}
Advanced Usage
Temporary State Changes
// Temporarily change application state
private bool _isProcessing;
public void ProcessWithState()
{
using (new ScopedAction(
onOpen: () => _isProcessing = true,
onClose: () => _isProcessing = false))
{
// Code that requires _isProcessing to be true
PerformProcessing();
}
// _isProcessing is automatically reset to false here
}
Creating Custom ScopedAction Classes
// Create specialized scoped actions by extending the base class
public class LoggingScope : ScopedAction
{
private readonly string _operationName;
private readonly ILogger _logger;
public LoggingScope(string operationName, ILogger logger)
{
_operationName = operationName;
_logger = logger;
_logger.LogInformation($"Starting operation: {_operationName}");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_logger.LogInformation($"Completed operation: {_operationName}");
}
base.Dispose(disposing);
}
}
// Usage
using (new LoggingScope("Data Import", logger))
{
// Import data...
}
API Reference
ScopedAction Class
The primary class for executing actions at scope boundaries.
Constructors
Constructor | Parameters | Description |
---|---|---|
ScopedAction(Action? onOpen, Action? onClose) |
onOpen : Action executed on construction<br>onClose : Action executed on disposal |
Creates a new ScopedAction that executes the specified actions |
ScopedAction() |
None | Protected constructor for derived classes |
Methods
Method | Return Type | Description |
---|---|---|
Dispose() |
void |
Executes the onClose action if not already disposed |
Dispose(bool disposing) |
void |
Protected virtual method for implementing the dispose pattern |
Contributing
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Please make sure to update tests as appropriate.
License
This project is licensed under the MIT License - see the LICENSE.md file for details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (9)
Showing the top 5 NuGet packages that depend on ktsu.ScopedAction:
Package | Downloads |
---|---|
ktsu.ImGuiStyler
A library for expressively styling ImGui.NET interfaces. |
|
ktsu.ImGuiApp
A comprehensive .NET library that provides complete application scaffolding for Dear ImGui applications, featuring window management, DPI-aware rendering, precision PID-controlled frame limiting with comprehensive auto-tuning, advanced font handling with Unicode/emoji support, texture management, and debug tooling. Built on Silk.NET for cross-platform OpenGL support and Hexa.NET.ImGui for modern Dear ImGui bindings. |
|
ktsu.ImGuiWidgets
A library of custom widgets using ImGui.NET and utilities to enhance ImGui-based applications. |
|
ktsu.ImGuiPopups
A library for custom popups using ImGui.NET. |
|
ktsu.CodeBlocker
A specialized utility built on top of IndentedTextWriter that simplifies the process of programmatically generating structured code. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.1.5 | 263 | 8/26/2025 |
1.1.4 | 467 | 8/7/2025 |
1.1.3 | 265 | 8/6/2025 |
1.1.3-pre.17 | 138 | 5/20/2025 |
1.1.3-pre.15 | 95 | 5/17/2025 |
1.1.3-pre.14 | 145 | 5/16/2025 |
1.1.3-pre.13 | 215 | 5/15/2025 |
1.1.3-pre.12 | 219 | 5/14/2025 |
1.1.3-pre.11 | 221 | 5/13/2025 |
1.1.3-pre.10 | 252 | 5/12/2025 |
1.1.3-pre.9 | 185 | 5/11/2025 |
1.1.3-pre.8 | 139 | 5/10/2025 |
1.1.3-pre.7 | 75 | 5/9/2025 |
1.1.3-pre.6 | 140 | 5/8/2025 |
1.1.3-pre.5 | 140 | 5/7/2025 |
1.1.3-pre.4 | 140 | 5/6/2025 |
1.1.3-pre.3 | 143 | 5/5/2025 |
1.1.3-pre.2 | 141 | 5/4/2025 |
1.1.3-pre.1 | 147 | 5/4/2025 |
1.1.2 | 1,013 | 5/4/2025 |
1.1.2-pre.1 | 75 | 4/26/2025 |
1.1.1 | 852 | 4/25/2025 |
1.1.1-pre.1 | 138 | 4/4/2025 |
1.1.0 | 1,503 | 3/30/2025 |
1.0.16-pre.2 | 97 | 3/29/2025 |
1.0.16-pre.1 | 483 | 3/25/2025 |
1.0.15 | 1,925 | 2/17/2025 |
1.0.15-pre.3 | 104 | 2/6/2025 |
1.0.15-pre.2 | 98 | 2/5/2025 |
1.0.15-pre.1 | 89 | 2/5/2025 |
1.0.14 | 2,463 | 12/27/2024 |
1.0.14-pre.28 | 92 | 2/3/2025 |
1.0.14-pre.27 | 86 | 2/3/2025 |
1.0.14-pre.26 | 85 | 2/1/2025 |
1.0.14-pre.25 | 97 | 1/30/2025 |
1.0.14-pre.24 | 77 | 1/28/2025 |
1.0.14-pre.23 | 89 | 1/26/2025 |
1.0.14-pre.22 | 76 | 1/24/2025 |
1.0.14-pre.21 | 84 | 1/22/2025 |
1.0.14-pre.20 | 85 | 1/20/2025 |
1.0.14-pre.19 | 81 | 1/18/2025 |
1.0.14-pre.18 | 75 | 1/16/2025 |
1.0.14-pre.17 | 64 | 1/14/2025 |
1.0.14-pre.16 | 82 | 1/13/2025 |
1.0.14-pre.15 | 83 | 1/11/2025 |
1.0.14-pre.14 | 82 | 1/10/2025 |
1.0.14-pre.13 | 82 | 1/10/2025 |
1.0.14-pre.12 | 75 | 1/8/2025 |
1.0.14-pre.11 | 88 | 1/7/2025 |
1.0.14-pre.10 | 89 | 1/6/2025 |
1.0.14-pre.9 | 112 | 1/4/2025 |
1.0.14-pre.8 | 88 | 1/3/2025 |
1.0.14-pre.7 | 93 | 1/3/2025 |
1.0.14-pre.6 | 84 | 1/3/2025 |
1.0.14-pre.5 | 97 | 1/2/2025 |
1.0.14-pre.4 | 105 | 12/31/2024 |
1.0.14-pre.3 | 78 | 12/29/2024 |
1.0.14-pre.2 | 83 | 12/28/2024 |
1.0.14-pre.1 | 86 | 12/27/2024 |
1.0.13-pre.1 | 77 | 12/27/2024 |
1.0.12 | 832 | 12/26/2024 |
1.0.11 | 131 | 12/26/2024 |
1.0.10 | 143 | 12/26/2024 |
1.0.10-pre.1 | 91 | 12/27/2024 |
1.0.9 | 125 | 12/26/2024 |
1.0.8 | 119 | 12/26/2024 |
1.0.7 | 121 | 12/26/2024 |
1.0.6 | 113 | 12/26/2024 |
1.0.5 | 982 | 12/23/2024 |
1.0.4 | 118 | 12/23/2024 |
1.0.3 | 175 | 12/22/2024 |
1.0.2 | 209 | 12/22/2024 |
1.0.1 | 706 | 12/12/2024 |
1.0.0 | 453 | 12/4/2024 |
1.0.0-alpha.22 | 295 | 12/2/2024 |
1.0.0-alpha.21 | 192 | 11/30/2024 |
1.0.0-alpha.20 | 252 | 11/26/2024 |
1.0.0-alpha.19 | 328 | 11/20/2024 |
1.0.0-alpha.18 | 368 | 11/13/2024 |
1.0.0-alpha.17 | 573 | 11/1/2024 |
1.0.0-alpha.16 | 1,019 | 10/4/2024 |
1.0.0-alpha.15 | 576 | 9/19/2024 |
1.0.0-alpha.14 | 182 | 9/19/2024 |
1.0.0-alpha.13 | 70 | 9/19/2024 |
1.0.0-alpha.12 | 96 | 9/19/2024 |
1.0.0-alpha.11 | 82 | 9/19/2024 |
1.0.0-alpha.10 | 132 | 9/19/2024 |
1.0.0-alpha.9 | 106 | 9/18/2024 |
1.0.0-alpha.8 | 95 | 9/18/2024 |
1.0.0-alpha.7 | 163 | 9/18/2024 |
1.0.0-alpha.6 | 261 | 9/18/2024 |
1.0.0-alpha.5 | 383 | 9/14/2024 |
1.0.0-alpha.4 | 99 | 9/14/2024 |
## v1.1.3-pre.17
Initial release or repository with no prior history.