13

I've got a .Net windows form application where a lot of variables are initialized in the Main_Load event and I have a situation where I want my DB re-queried and all vars set to null and re-initialized (basically the form 100% reloaded from the start), but I wrote my Main-Load in such a way (my fault) that it's not that easy to do...

I know I could get exactly what I want by simply calling Application.Restart and it does the trick beautifully, but I'm not sure if this is good programming practice or considered really bad.

Are there any problems that I'm likely to run into by using Application.Restart in this context?

Servy
  • 202,030
  • 26
  • 332
  • 449
John Bustos
  • 19,036
  • 17
  • 89
  • 151
  • 5
    I think the biggest problem you might have is when your colleagues see your code and say WTF?! – Fernando Oct 19 '12 at 19:55
  • ... Too late, Fernando... They already say that all the time :p – John Bustos Oct 19 '12 at 20:22
  • 1
    Couldn't you just abstract the code in Main_Load to a seperate function and call that whenever you need it? Then you don't have to worry about any side effects that aren't part of your function. – hubson bropa Oct 19 '12 at 19:52

4 Answers4

7

Not friendly to debug, but there's nothing really wrong with it. It is the exact same as terminating the app and starting it again.

You can avoid it by simply creating a new instance of your main form and closing the old one. That however does require you to prevent the program from exiting. Code is here.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
2

It's not that the method doesn't work; rather, many times programmers forget that they've put something in their code that would stop the application from automatically shutting down, or starting up.

Please follow this Thread

You can also do the job with

 System.Diagnostics.Process.Start(Application.ExecutablePath);
 Application.Exit();
Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • ... The part that scares me with both methods is that I put a breakpoint in my Main.Load event and it doesn't fire the second time... I'm guessing this is an issue with the debugger in .Net, but it definitely makes me wary of this approach... Is it bad?? – John Bustos Oct 19 '12 at 19:55
  • It is not bad as it has been discussed in the Thread also. It is just that it is not reliable – Rahul Tripathi Oct 19 '12 at 19:57
0

To directly answer the question in the title, yes restarting an application to re-initialize variables is bad practice.

There are cases where restarting an application is usefull (in example self-update), but restarting to mimic a ReInitialize() method is bad in my opinion.

Jf Beaulac
  • 5,206
  • 1
  • 25
  • 46
  • ... I am basically self-updating if I'm understanding you correctly... My Challenge is that I have dozens of list objects I have in my main form where I dimmed them as Dim xyz as new list(of ABC) then, in my main_load event and throughout the program I'm adding to the lists with DB data and now I'm going to need a new query to populate them... I'm afraid I'll miss one if I try and empty them all... – John Bustos Oct 19 '12 at 21:02
0

I had problems with this.

I really needed to restart a large Winforms Application, when a user logs off, to ensure all cached (my) data are purged.

Solved my problem by adding the Application.restart() into the Application Shutdown Event.

This works inside the VS environment and when running the EXE

If you want to find that event select your main project properties and from the Application (Side Tab) select View Application Events at the bottom.

My guess is that this works because it is very late on in the closing process. Hope this helps someone and, more importantly, it continues to work.

Tim F.
  • 268
  • 2
  • 8