0

I have the following class (exposed to COM) which I am using as a wrapper to show forms called from VB6.

public class NetForm : INetForm
{
    public NetForm(Assembly assembly, string name, object[] parameters)
    {
        Assembly = assembly;
        Name = name;
        CreateInstance(parameters);
    }

    private Assembly Assembly { get; set; }
    private string Name { get; set; }
    private Form Instance { get; set; }

    private void CreateInstance(object[] parameters)
    {
        Type type = Assembly.GetType(Name);
        Type[] types = new Type[parameters.Length];
        for (int idx = 0; idx < parameters.Length; idx++)
            types[idx] = parameters[idx].GetType();

        ConstructorInfo ci = type.GetConstructor(types);
        Instance = (Form)ci.Invoke(parameters);
    }
    public void Show()
    {
        Instance.Visible = true;
        Instance.Show();       // Crashing
    }
    public void ShowDialog()
    {
        Instance.ShowDialog();  // Works perfectly
    }
}

public class NetAssembly
{
    public NetAssembly(string fullname)
    {
        Init(fullname);
    }

    private Assembly Assembly { get; set; }

    private void Init(string name)
    {
        Assembly = Assembly.LoadFrom(name);            
    }

    public NetForm GetForm(string nameSpace, string name, object[] parameters)
    {
        name = string.Concat(nameSpace, ".", name);
        return new NetForm(Assembly, name,parameters);
    }
}
  1. ShowDialog() works perfectly.
  2. Show() is not being displayed if the Visible property is not set to True. But also when setting it to true, the form is partially being displayed and crash after few seconds.

I am using a UnitTestProject to check it:

[TestClass]
public class NetFormUnitTest
{
    [TestMethod]
    public void ShowTestMethod()
    {
        var assembly = new NetAssembly(assemblyFullName);
        var form = assembly.GetForm(nameSpace, formName, new object[] { "Item1" });
        form.Show();
    }
}

What is the correct way to call the Show() method?

ehh
  • 3,412
  • 7
  • 43
  • 91
  • 1
    I think, your form needs a message pump (like *Application.Run* creates). The reason for ShowDialog is that it creates its own message loop. See https://stackoverflow.com/questions/26741841/how-does-a-modal-dialogs-message-pump-interact-with-the-main-application-messag See also [this](https://stackoverflow.com/questions/44998082/c-sharp-create-form-on-separate-thread-from-formless-application) how to create a background thread showing a form (of course you have to replace the code related with creating the form instance) – L.B Sep 11 '17 at 08:15
  • @L.B, Application.Run(Instance); make it works. Please add it as answer – ehh Sep 11 '17 at 08:21
  • Possible duplicate of [How to use reflection to call method by name](https://stackoverflow.com/questions/3110280/how-to-use-reflection-to-call-method-by-name) – Feras Al Sous Sep 11 '17 at 08:22

1 Answers1

1

Your form needs a message pump (like Application.Run creates). The reason for ShowDialog is that it creates its own message loop.

So adding Application.Run(formInstance) should solve the problem.

L.B
  • 114,136
  • 19
  • 178
  • 224