Q:

perform Action<> in main thread

I'm writing an app, that performs very long requests at background. After each request I need to send result to main form.
So, here is a code:
Form1.cs
private async void StartButton_Click(object sender, EventArgs e)
{
    await Logic.GenerateStackAsync(stackSettings, delegate(FullOrder transaction)
          {
              lastOrderId.Text = transaction.OrderId;
          }
    );

    MessageBox.Show("Completed!");
}

Logic.cs:
    public static bool GenerateStack(StackSettings stackSettings, Action<FullOrder> onOrderCreated = null)
    {
         for(var i = 0; i < 10; i++) 
         {
              // long, long request, replaced with:
              System.Threading.Thread.Sleep(10000);
              if (onOrderCreated != null)
              {
                  onOrderCreated.Invoke(order);
                  // tried to change it with onOrderCreated(order), no results.
              }
          }

          return true;
    }

    public static Task<bool> GenerateStackAsync(StackSettings stackSettings, Action<FullOrder> onOrderCreated)
    {
        return TaskEx.Run(() => GenerateStack(stackSettings, onOrderCreated));
    }

It throws an exception: "Control 'lastOrderId' accessed from a thread other than the thread it was created on.", which can be fixed by adding CheckForIllegalCrossThreadCalls = false;, but I think that this is a bad experience. How make it right? Thank you in advance.
P.S. Sorry for bad English.

A:

I would say that you need to use Control.Invoke to solve that problem:
See http://msdn.microsoft.com/library/system.windows.forms.control.invoke(v=vs.110).aspx

