Hi I wanna share some experience I got while working with WPF.
English is not my first language so I'm sry for mistakes. Ask if you don't understand parts of my little guide.
First create your WPF Gui. You should have a classname.
You have to run it in another Thread!
Now the code which should run in the new Thread
I hope this helps someone here. I had a lot of problems because if you force a shutdown with the plugin manager the thread will not be closed and you lock your dll. Which results into a crash of the updater.
English is not my first language so I'm sry for mistakes. Ask if you don't understand parts of my little guide.
First create your WPF Gui. You should have a classname.
You have to run it in another Thread!
Code:
namespace Example
{
private Thread UiThread;
public bool UiOpen = true;
public partial class RotationLogic : Core
{
public void PluginRun()
{
// Create a new Thread and execute RunUi in it.
UiThread = new Thread(RunUi);
// Thread have to be Single threaded!
UiThread.SetApartmentState(ApartmentState.STA);
UiThread.Start();
//Prevent the end of the plugin
while (UiOpen)
Thread.Sleep(100);
}
//.... next code box ...
}
Now the code which should run in the new Thread
Code:
private void RunUi()
{
// Create new interface object
Ui = new UiWindow(this, me);
// Show the window
Ui.Show();
// Sry I can't explain it but it's necessary
SynchronizationContext.SetSynchronizationContext(
new DispatcherSynchronizationContext(
Dispatcher.CurrentDispatcher));
// Event for when the window get closed
Ui.Closed += (s, e) =>
{
Dispatcher.CurrentDispatcher.ShutdownFinished += (ss, ee) =>
{
// Will be executed after the thread was close.
UiOpen = false;
};
// Tells the thread to close itself
Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background);
};
Dispatcher.Run();
}
I hope this helps someone here. I had a lot of problems because if you force a shutdown with the plugin manager the thread will not be closed and you lock your dll. Which results into a crash of the updater.