title: Is it safe to call StateHasChanged() from an arbitrary thread?
body: <p>Is it safe to call <code>StateHasChanged()</code> from an arbitrary thread?</p>
  
  <p>Let me give you some context. Imagine a Server-side Blazor/Razor Components application where you have:</p>
  
  <ul>
  <li>A singleton service <code>NewsProvider</code> that raises <code>BreakingNews</code> events from an arbitrary thread.</li>
  <li>A component <code>News.cshtml</code> that gets the service injected and subscribes to <code>BreakingNews</code> event. When the event is raised, the component updates the model and calls <code>StateHashChanged()</code></li>
  </ul>
  
  <h3>NewsProvider.cs</h3>
  
  <pre><code>using System;
  using System.Threading;
  
  namespace BlazorServer.App
  {
      public class BreakingNewsEventArgs: EventArgs
      {
          public readonly string News;
  
          public BreakingNewsEventArgs(string news)
          {
              this.News = news;
          }
      }
  
      public interface INewsProvider
      {
          event EventHandler&lt;BreakingNewsEventArgs&gt; BreakingNews;
      }
  
      public class NewsProvider : INewsProvider, IDisposable
      {
  
          private int n = 0;
  
          public event EventHandler&lt;BreakingNewsEventArgs&gt; BreakingNews;
          private Timer timer;
  
          public NewsProvider()
          {
              timer = new Timer(BroadCastBreakingNews, null, 10, 2000);
  
          }
  
          void BroadCastBreakingNews(object state)
          {
              BreakingNews?.Invoke(this, new BreakingNewsEventArgs("Noticia " + ++n));
          }
  
          public void Dispose()
          {
              timer.Dispose();
          }
      }
  }
  </code></pre>
  
  <h3>News.cshtml</h3>
  
  <pre><code>@page "/news"
  @inject INewsProvider NewsProvider
  @implements IDisposable
  
  &lt;h1&gt;News&lt;/h1&gt;
  
  @foreach (var n in this.news)
  {
      &lt;p&gt;@n&lt;/p&gt;
  }
  
  
  @functions {
      EventHandler&lt;BreakingNewsEventArgs&gt; breakingNewsEventHandler;
  
      List&lt;string&gt; news = new List&lt;string&gt;();
  
      protected override void OnInit()
      {
          base.OnInit();
          breakingNewsEventHandler = new EventHandler&lt;BreakingNewsEventArgs&gt;(OnBreakingNews);
          this.NewsProvider.BreakingNews += breakingNewsEventHandler;
      }
  
      void OnBreakingNews(object sender, BreakingNewsEventArgs e)
      {
          this.news.Add(e.News);
          StateHasChanged();
      }
  
      public void Dispose()
      {
          this.NewsProvider.BreakingNews -= breakingNewsEventHandler;
      }
  }
  </code></pre>
  
  <h3>Startup.cs</h3>
  
  <pre><code>using Microsoft.AspNetCore.Blazor.Builder;
  using Microsoft.Extensions.DependencyInjection;
  using BlazorServer.App.Services;
  
  namespace BlazorServer.App
  {
      public class Startup
      {
          public void ConfigureServices(IServiceCollection services)
          {
              // Since Blazor is running on the server, we can use an application service
              // to read the forecast data.
              services.AddSingleton&lt;WeatherForecastService&gt;();
              services.AddSingleton&lt;INewsProvider, NewsProvider&gt;();
          }
  
          public void Configure(IBlazorApplicationBuilder app)
          {
              app.AddComponent&lt;App&gt;("app");
          }
      }
  }
  </code></pre>
  
  <p>it apparently works, but I don't know if <code>StateHasChanged()</code> is thread safe. If it isn't, how can I call <code>StateHashChanged()</code> safely?. Is there something similar to <code>Control.BeginInvoke</code>? Should I use <code>SyncrhonizationContext.Post</code>?</p>