Ensure UI thread safety for WPF and WinForms with SafeInvoker. Includes a Dispatcher-aware method attribute and extension methods to guarantee safe control access in .NET applications.
$ dotnet add package DCTekSolutions.SafeInvokerSafeInvoker helps ensure your WPF and WinForms code interacts with UI elements safely and correctly.
It provides a Fody-powered method attribute and a clean extension API for thread-safe UI invocation.
Free to use for personal and commercial projects. This package is closed source.
[WPF.SafeInvoke] attribute for automatic thread safety on WPF methods[Forms.SafeInvoke] attribute for WinForms control methodsDispatcherObject.SafeInvoke(...) (WPF)Control.SafeInvoke(...) (WinForms)InvalidOperationException from unsafe UI thread accesspublic class MyViewModel : DispatcherObject
{
[WPF.SafeInvoke]
public void UpdateUI()
{
Console.WriteLine($"Running on UI thread: {Dispatcher.CheckAccess()}");
}
}
myWindow.SafeInvoke(w =>
{
w.Title = "Updated safely from any thread!";
});
public class MyForm : Form
{
[Forms.SafeInvoke]
public void UpdateLabel()
{
myLabel.Text = "Thread-safe update";
}
}
myLabel.SafeInvoke(lbl =>
{
lbl.Text = "Safe call from any thread";
});
(() => myWindow.Close()).SafeInvokeOn(myWindow.Dispatcher); // WPF
(() => myForm.Close()).SafeInvokeOn(myForm); // WinForms
CycleKit – round-robin async queuesAutoYumYum – DI via attributesTransactionAction – chained atomic operations[assembly: Interceptor] to your AssemblyInfo.cs[WPF.SafeInvoke] or [Forms.SafeInvoke]~~~~ to your methodSafeInvoker is proprietary but free to use in personal and commercial projects.
See LICENSE.md for details.
If you find SafeInvoker helpful, please consider supporting us: