Skip to content

heytherewill/RunOnMainThread.Fody

Repository files navigation

RunOnMainThread.Fody

Run annotated methods on the Main Thread. The Fody plugin you never realized you needed.

Installation

Install via NuGet using:

PM> Install-Package RunOnMainThread.Fody

Then add it to your FodyWeavers.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<Weavers>
  <RunOnMainThread />
</Weavers>

Usage

The package will add a static MainThreadDispatcher class with a void RunOnMainThread(Action action) method. You can use it directly to access the main thread from anywhere in your app:

private void ShowDialogUsingDispatcher()
{
    MainThreadDispatcher.RunOnMainThread(() =>
    {
        Console.WriteLine("Main thread!");
    });
}

The cool part about this library, though, is that you can instead annotate your method with the RunOnMainThreadAttribute and all that boilerplate will be added for you. The method below is the same as the one in the static class example:

[RunOnMainThread]
private void ShowDialogUsingWeaver()
{
    Console.WriteLine("Main thread!");
}

For more examples, check out the sample apps.

About

🔮 Run annotated methods on the Main Thread

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published