Search code examples
c#.netwinformsformclosing

C# closing another form problem, Close(); does not work


I have this code on form1

TimerMode f2 = new TimerMode();
f2.show();

now I'm trying to use this code in some point in time, but nothing happens? Cmd = Closing

public void DoActions(string Cmd)
{
  switch(Cmd){

  case"Open":
      TimerMode f2 = new TimerMode();
      f2.show()
      break;
  case"Closing":
       f2.Close();
       break;
}
}

do you have any idea why its not closing?.

what I really want it to close it.

in vb6 I use this

unload form2

Solution

  • Most probably a threading issue. Try this:

    f2.Invoke((MethodInvoker)(() => f2.Close()));
    

    If that doesn't work, use below modification:

    public TimerMode f2 = new TimerMode();
    public void DoActions(string Cmd)
    {
      switch(Cmd){    
      case"Open":          
          f2.show()
          break;
      case"Closing":
           f2.Close();
           break;
      }
    }