Question: I'm working on a plugin for Revit. Revit permits only dll type addins and I chose c# and wpf for gui. I couldn't initialize wpf natively so I needed to contain it in a system.windows.window as content. 
Now if I close that window with upper right X program continues (looping processing etc.) it doesn't get terminated. Window.close() again just closes the window not the application it self.
How should I exit my app and not keep it lurking in the shadows?

```
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class FDNMain : IExternalCommand
{
    public Result Execute(ExternalCommandData CommandData, ref string message, ElementSet elements)
    {
        FDNControl wpfControl = new FDNControl();
        return Result.Succeeded;
    }
}

```

Above is the Revit external command class which cannot initialize (show up) the wpfControl. So I need to wrap the wpfcontrol with a System.Windows.Window as below:

```
Window hostWindow = new Window();
hostWindow.Content = wpfControl;
hostWindow.Show();

```

Answer: 
Unfortunately I don't know how to do this without any existing form. But I'm sure you have some kind of main form you can access. Or you can obtain a `Form` using

```
var invokingForm = Application.OpenForms[0];

```

So you can change the code of your method like this:

```
static void OnNewMessage(object sender, S22.Xmpp.Im.MessageEventArgs e)
{
    var invokingForm = Application.OpenForms[0]; // or whatever Form you can access
    if (invokingForm.InvokeRequired)
    {
        invokingForm.BeginInvoke(new EventHandler<S22.Xmpp.Im.MessageEventArgs>(OnNewMessage), sender, e);
        return; // important!!!
    }

    // the rest of your code goes here, now running
    // on the same ui thread as invokingForm
    if(CheckIfFormIsOpen(e.Jid.ToString(), e.Message.ToString()) == true)
    {
    }
    else
    {
        newMessageFrm tempMsg = new newMessageFrm(e.Jid.ToString());
        tempMsg._msgText = e.Jid.ToString() + ": " + e.Message.ToString();
        tempMsg.frmId = e.Jid.ToString();
        tempMsg.Show();
    }
}

```

Note that I assumend that `S22.Xmpp.Im.MessageEventArgs` is inherited from `System.EventArgs`.
In WinForms there is still a requirement that the UI objects need to be on a thread with a message pump. This is usually the main application thread, also called the UI thread.

In Winforms checking to see if you are on the right thread to access a UI object is done with `Control.InvokeRequired`. If this returns true, it means you are not on the proper thread, and need to use an Invoke operation.

Since you know you are one a different thread, you don't need to check, you can just Invoke.

```
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ThreadPool.QueueUserWorkItem(BadBackgroundLaunch);
        ThreadPool.QueueUserWorkItem(GoodBackgroundLaunch);
    }

    private void GoodBackgroundLaunch(object state)
    {
        this.Invoke((Action) (() =>
        {
            var form2 = new SecondForm();
            form2.Text = "Good One";
            form2.Show();
        }));
    }

    private void BadBackgroundLaunch(object state)
    {
        var form2 = new SecondForm();
        form2.Text = "Bad one";
        form2.Show();
    }
}

```