Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

c# make Out-of-Process COM exe with UI,How to let it auto show up before method be called?

I have success make a runable COM exe , and let it have an object that called by other process,successfully...ref to below client code ..

    private void button1_Click(object sender, EventArgs e)
    {

        WindowsFormsApplication1.Calculator CALC = new WindowsFormsApplication1.Calculator();
        int i= CALC.Add(12, 3);
        button1.Text = i.ToString(); 
    }

but my question is ,why the main form of exe server that was not be auto show up first before this calling ...??

below is my server code ::

static class Program
{
    /// <summary>
    /// 
    /// </summary>
    [STAThread]
    static void Main()
    {

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);


        var regServices = new RegistrationServices();

        int cookie = regServices.RegisterTypeForComClients(
            typeof(Calculator),
            RegistrationClassContext.LocalServer | RegistrationClassContext.RemoteServer,
            RegistrationConnectionType.SingleUse);

        Application.Run(new Form1());

        //  regServices.UnregisterTypeForComClients(cookie);

    }
}



        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                // Calculator.ptrF = this;
            }

            private void button1_Click(object sender, EventArgs e)
            {
                this.Show();
            }

            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                // Calculator.ptrF = null;

            }
        }

        [ComVisible(true)]
        public interface ICalculator
        {
            int Add(int x, int y);
        }

        [ComVisible(true)]
        [ClassInterface(ClassInterfaceType.None)]
        public class Calculator : ICalculator
        {
            // public static Form1 ptrF;

            public int Add(int x, int y) {

               // if (ptrF != null)
               // {
               //     ptrF.Show();
               //
               // }else {
               //     ptrF = new Form1();
               //     ptrF.Show();
               // }

                return x + y;
            }
        }

For solve no form showing during/before method be called ,I add a some code as upper be comment,It worked ... but I still think these are not a elgent method...can anybody help me to improve it ??

Comments