Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / June 2006

Tip: Looking for answers? Try searching our database.

Application.Restart Tough One

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chuck P - 16 Jun 2006 20:42 GMT
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();
           }
       }
Linda Liu [MSFT] - 19 Jun 2006 09:33 GMT
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.
====================================================
Chuck P - 19 Jun 2006 14:34 GMT
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?

>Hi Chuck,
>
[quoted text clipped - 36 lines]
>from your issue.
>====================================================
Linda Liu [MSFT] - 22 Jun 2006 13:30 GMT
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.
====================================================
Chuck P - 22 Jun 2006 16:58 GMT
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!

>Hi Chuck,
>
[quoted text clipped - 21 lines]
>from your issue.
>====================================================
Linda Liu [MSFT] - 23 Jun 2006 07:14 GMT
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.
====================================================

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.