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?