-1

I have created 2 applications 1 WinForms and 1 console application and I am trying to call a method in the WinForms application using the console application but the main form object is null for some reason.

Winforms App:

     namespace ABC
     {
        public class WinFormsApp
        {
            public static WinFormsApp instance = null;
            public WinFormsApp()
            {
                 instance = this;
                 InitializeComponent();
            }
        }
        public static class BridgeClass
        {
            public static void Demo()
            {
                string msg = "no";
                if (WinFormsApp.instance != null)
                     msg = "yes";
                System.Windows.Forms.MessageBox.Show(msg);
             }
        }
    }

Console App:

    public static void Main()
    {
        Assembly SampleAssembly = Assembly.LoadFrom("ABC.exe");
        MethodInfo Method = SampleAssembly.GetType("ABC.BridgeClass").GetMethod("Demo");
        if (Method != null)
        {
            Method.Invoke(null, null);  
        }
    }

The pop-up message displays no when the console application calls the Winforms method but I have checked using breakpoint that the instance variable is initialized properly.

Note: The Winforms application is supposed to be running using the main method when calling the method from the console app.

Can someone please point out what I am doing wrong?

Ramesh Verma
  • 111
  • 1
  • 9
  • You haven't actually created an instance of the main form class when running the code from your console application. I assume your mention of the debugger is when debugging the actual `abc.exe` project, and not when debugging your console application? – Lasse V. Karlsen Aug 31 '20 at 10:28
  • 1
    Loading the EXE as assenbly and calling a method in it bypasses the Main method of the EXE and will thus not initialize your winforms app. – Klaus Gütter Aug 31 '20 at 10:34
  • @LasseV.Karlsen can you provide an example code of what should I do? – Ramesh Verma Aug 31 '20 at 10:39
  • Somewhere in your code you will have to do the equivalent of `new WinFormsApp()`, you're not doing that. Most likely you want the code from your main method in the abc.exe project. Since you're loading the assembly directly, that method will not be executed and as such your `abc.exe` *program* will not actually be running. You've just loaded the assembly and called a single method, you will need to start the main program of that abc.exe program in this case. – Lasse V. Karlsen Aug 31 '20 at 11:12

1 Answers1

0

I cannot add a comment... So I answer here. You have not created an instance of ABC, so you cannot invoke the method. Take a look at the Activator, you can create instances with it. Here is an example.

  • I want the WinForms object to be created only through the main method in the WinForms app. Is there any way to achieve that? – Ramesh Verma Aug 31 '20 at 10:49
  • I'm not sure if I understand you correctly. But take a look at the Singleton Pattern. I think this is what you are looking for. [Here](https://stackoverflow.com/questions/2667024/singleton-pattern-for-c-sharp) is an example. – Daniel Bendel Aug 31 '20 at 11:01