What's new
  • Visit Rebornbuddy
  • Visit Panda Profiles
  • Visit LLamamMagic
  • Visit Resources
  • Visit Downloads
  • Visit Portal

[Tipp] Howto use WPF with Archbuddy

zdennis

New Member
Joined
Aug 2, 2012
Messages
28
Reaction score
0
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!

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.
 
Hi friend, your code helped a lot. I had some problems when closing the main thread. Even after the plugin was stopped something was left running and therefore it locked the DLL. Adding the IsBackground property to the thread solved the issue. I hope it helps somebody along the way :)

Code:
UiThread.IsBackground = true;

Cheers!
 
Back
Top