Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

C# remote load the plugin(dll with modeless form) by non-default AppDomain,the Modeless Form can not accept the 'Enter' and 'Esc' from keyboard.

$
0
0

I make a plugin dll, there is a form in this dll. then remote load this dll by non-default AppDomain. if I open this form with modeless, this form can't accept the key 'Enter' and 'Esc' from keyboard. if i open this form whit model, this form accept normal. if i open this form with default appdomain,all is normal!

I hope, In the plugin dll, open Form with modeless that can accept all key(inclue 'Enter and Esc') from keyboard.

//Code: Form1.cs(of main)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;

namespace NonDefaultAppDomainModelessFormTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            AppDomain.CurrentDomain.SetupInformation.ShadowCopyFiles = "true";
            AppDomainSetup setup = new AppDomainSetup();
            setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            setup.LoaderOptimization = LoaderOptimization.SingleDomain;
            setup.ApplicationName = "Test";
            setup.ShadowCopyDirectories = setup.ApplicationBase;
            setup.ShadowCopyFiles = "true";
            setup.CachePath = Path.Combine(setup.ApplicationBase, "Test");
            setup.PrivateBinPath = Path.Combine(setup.ApplicationBase, "Test");

            _pluginAppdomain = AppDomain.CreateDomain("Test", AppDomain.CurrentDomain.Evidence, setup);
            _pluginRemoteLoader = (RemoteLoader)_pluginAppdomain.CreateInstanceFromAndUnwrap(Path.GetFileName(Application.ExecutablePath), typeof(RemoteLoader).FullName);

            string pluginAssemblyFile = @"..\..\..\ClassLibrary1\bin\Debug\ClassLibrary1.dll";
            string pluginFullName = @"ClassLibrary1.Class1";
            _pluginRemoteLoader.LoadPluginAssemblyAndCreateInstance(pluginAssemblyFile, pluginFullName);
        }

        private AppDomain _pluginAppdomain;
        private RemoteLoader _pluginRemoteLoader;

        private void button1_Click(object sender, EventArgs e)
        {           
            _pluginRemoteLoader.InvokeMethod("ShowWinForm", null);
        }
    }

    class RemoteLoader : MarshalByRefObject
    {
        public override object InitializeLifetimeService()
        {
            return null;
        }       

        public bool LoadPluginAssemblyAndCreateInstance(string pluginAssemblyFile, string pluginFullName)
        {
            bool loadSuccess = false;
            try
            {
                _pluginAssembly = Assembly.LoadFrom(pluginAssemblyFile);
                Type[] pluginTypes = _pluginAssembly.GetTypes();
                foreach (Type pluginType in pluginTypes)
                {
                    if (pluginType.FullName == pluginFullName)
                    {
                        _pluginType = pluginType;
                        _pluginInstance = _pluginAssembly.CreateInstance(pluginType.FullName);
                        loadSuccess = true;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return loadSuccess;
        }

        public object InvokeMethod(string methodName, object[] args)
        {
            MethodInfo methodInfo = _pluginType.GetMethod(methodName);
            return methodInfo.Invoke(_pluginInstance, args);
        }

        private Assembly _pluginAssembly;

        private Type _pluginType;

        private object _pluginInstance;
    }
}
//Plugin Dll Class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClassLibrary1
{
    public class Class1
    {
        Form1 _frmTest = new Form1();

        public void ShowWinForm()
        {
            if (_frmTest.WindowState == FormWindowState.Minimized)
            {
                _frmTest.WindowState = FormWindowState.Normal;
                _frmTest.Show();//can't accept 'Enter' and 'Esc'
                //_frmTest.ShowDialog();//can accept 'Enter' and 'Esc';
                _frmTest.ShowInTaskbar = true;               
            }
            else
            {
                _frmTest.WindowState = FormWindowState.Minimized;
            }
        }
    }
}
//Plugin Dll Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
                this.ShowInTaskbar = false;
            }
        }
    }
}


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>