Application.Restart Tough One

C

Chuck P

I am having trouble getting Application.Restart to work.
My somewhat lengthy startup code is shown below.
Interestingly enough it will work, if I display a message box that
displays Application.Application.Name.

Just displaying the messagebox or just assigning
Application.ApplicationName doesn't work.

Is there a prerequesite or failure mode when using
Application.Restart()?????? Maybe some kind of hWnd or threadID
issue?


[STAThread]
static void Main()
{


Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

try
{
if
(System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed
&& ClickOnceToolKit.UpdateAvailable())
{
ClickOnceToolKit.UpdateComplete += new
ClickOnceToolKit.UpdateCompleteEventHandler(ClickOnceToolKit_UpdateComplete);
ClickOnceToolKit.StatusUpdate += new
ClickOnceToolKit.UpdateStatusEventHandler(ClickOnceToolKit_StatusUpdate);

ClickOnceToolKit.UpdateApplication(ClickOnceToolKit.RestartMode.AutoIn3Seconds);
Application.Run();
}

else
{
Form1 f = new Form1();
Application.Run(f);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}

static void ClickOnceToolKit_StatusUpdate(string UpdateText)
{
//update splash screen
}

static void ClickOnceToolKit_UpdateComplete(bool success,
Exception ex)
{
if (success)
{
//kludge won't restart without using
application.productname in messagebox.
MessageBox.Show("program.ClickOnceToolKit_UpdateComplete: updated
app, doing restart now" + Application.ProductName);

Application.Restart();
Application.Exit();
}
else
{
MessageBox.Show("program.ClickOnceToolKit_UpdateComplete: error:" +
ex.Message);
Application.Exit();
}
}
 
L

Linda Liu [MSFT]

Hi Chuck,

Thank you for posting.

There's no prerequisite for the Applicaiton.Restart method. Applications
are restarted in the context in which they were initially run. If your
application was started using a URL pointing directly to the application's
main executable file, it will be restarted using the same URL. If your
application is a ClickOnce application, it will be restarted using
ClickOnce.

Is the ClickOnceToolKit used in your program a third-party tool?

You may have a try removing the code related to the ClickOnceToolKit in
your Main method. Then add a button in the main form and add the statement
"Application.Restart()" in the button's Click event handler. When the
program is running, chick this button and check whether the program could
restart. If the result is yes, the problem may be related to the use of
ClickOnceToolKit in your program.

I recommend you to use ApplicationDeployment class to update the current
deployment.
For more information on using ApplicaitonDeployment to update the current
deployment, you may refer to the following link:
http://msdn2.microsoft.com/en-us/library/system.deployment.application.appli
cationdeployment.updateasync(d=ide).aspx

Please try my suggestions and let me know the result.

Have a nice day!


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
C

Chuck P

I wrote the toolkit so we could have a repeatable way of updating
applications. I am using the ApplicationDeployment class; The
application.restart just doesn't work as described.

When does the information that Application.Restart use get
initialized?
 
L

Linda Liu [MSFT]

Hi Chuck,

Thank you for your update.

Could you please have a try moving the update check code after the
statement "Application.Run()"? This sounds like a race condition between
the async update and Run().

Note that you shouldn't call Application.Exit after Application.Restart
because Restart internally already calls Exit.

Please try my suggestions. If the problem still exists, you could set up a
sample project that can just reproduce the problem and send it to me. To
get my actual email address, remove the "online" from my displayed email
address.


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
C

Chuck P

Thanks Linda Liu,

It was a threading issue. I put in some debug stuff and saw that the
events were not running on the main thread. MSDN
ApplicationDeployment.UpdateProgressChanged Event says it runs on the
main thread. I changed my class to inherit from control (get all the
invoke stuff). Poof it all works now!
 
L

Linda Liu [MSFT]

Hi Chuck,

Thank you for your response and the feedback on how you were successful in
resolving this issue.

If you have any other questions, please don't hesitate to contact us.

Have a nice day!


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 

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

Top