Is this a bad restart() idea?

D

DeMarcus

I'm working on the preferences right now, and when the user
have done some changes I need to do a restart of the software.
For instance I have a feature where you can choose the language
for the whole software including buttons and everything, and
changing that in runtime feels a little tricky. Therfore I have
created a method like this.

static void initRestarter( JFrame mainFrame )
{
_mainFrame = mainFrame;
}

static void restart()
{
_mainFrame.setVisual( false );
_mainFrame.dispose();
MySoftware.main( new String[0] );
}


Now I can can call restart() from anywhere in the software and
have a restart.

The question is; is this bad programming?

Thanks
Daniel
 
C

Chris Smith

DeMarcus said:
static void restart()
{
_mainFrame.setVisual( false );
_mainFrame.dispose();
MySoftware.main( new String[0] );
}


Now I can can call restart() from anywhere in the software and
have a restart.

The question is; is this bad programming?

Yes, it's a really bad idea. Just the problems that first come to mind
are:

1. This fails to terminate any previous threads, which means you have to
avoid multithreading like the plague (bad) or risk random modifications
of shared state that you don't want (very bad).

2. You've failed to reset any static variable to their initial values,
which could randomly break your code.

3. Your existing code may continue to run after the method returns. If
nothing else, you should at least find a way to safely terminate the
current thread at the end of that method.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
D

DeMarcus

Chris said:
DeMarcus said:
static void restart()
{
_mainFrame.setVisual( false );
_mainFrame.dispose();
MySoftware.main( new String[0] );
}


Now I can can call restart() from anywhere in the software and
have a restart.

The question is; is this bad programming?


Yes, it's a really bad idea. Just the problems that first come to mind
are:

1. This fails to terminate any previous threads, which means you have to
avoid multithreading like the plague (bad) or risk random modifications
of shared state that you don't want (very bad).

2. You've failed to reset any static variable to their initial values,
which could randomly break your code.

3. Your existing code may continue to run after the method returns. If
nothing else, you should at least find a way to safely terminate the
current thread at the end of that method.

But still. The user can always terminate the software by pressing the
close-window-button up in the right corner (Windows). How can I
simulate such event to my favor? And then after that do a restart of
the software. Shouldn't that be kind of safe?

/Daniel
 
C

Chris Smith

DeMarcus said:
But still. The user can always terminate the software by pressing the
close-window-button up in the right corner (Windows). How can I
simulate such event to my favor?

Whether the user can terminate the software that way depends on what you
do in response to the resulting event. You can terminate the current
process, though, with System.exit.
And then after that do a restart of
the software.

If you need to restart your software, do it in a different process using
Runtime.exec. Unfortunately, there isn't a portable way to accomplish
this.
Shouldn't that be kind of safe?

I don't understand what you mean. The code you posted is definitely NOT
safe.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top