progress from Blocking method? or blocking main thread with async method??

  • Thread starter Thread starter giddy
  • Start date Start date
G

giddy

hi ,

I'm in a slight dilemma.

I have an updater program that checks for pending updates and
downloads them if the users chooses to.

Since i dont need a windows form to check if there is an update , I
dont use a form unless i have two. This , creates either one of two
problems. When i'm downloading asynchonously , the main thread exits!
Or if use a blocked download , how do i display progress!?

In the updater.DownloadUpdates() , i show a reusable ProgressForm ,
and update it in the downloadProgress changed event, but it just shows
and nothing happens and the App never closes!?

[STAThread]
static void Main(string[] args)
{
Updater updater = new Updater();
if (updater.PendingUpdate)
{
Application.EnableVisualStyles();
DialogResult result = MessageBox.Show("There is an
update availiable , would you want to download and install it?",
"Updater", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
updater.DownloadUpdates(); // since the download
is threaded this just returns!?
}
}
else if (args[0] == "/shownot")
{
MessageBox.Show("Hotel Management Studio is up to
date");
}
//do {} //this does'nt work!?
//while (updater.Completed);
}

Thanks so much

Gideon
 
[...]
Since i dont need a windows form to check if there is an update , I
dont use a form unless i have two. This , creates either one of two
problems. When i'm downloading asynchonously , the main thread exits!
Or if use a blocked download , how do i display progress!?

In the updater.DownloadUpdates() , i show a reusable ProgressForm ,
and update it in the downloadProgress changed event, but it just shows
and nothing happens and the App never closes!?

Somewhere in there, you need to wait until your progress status form
closes, and you need to close the progress status form when you're done,
as well as update it to show progress.

There are a variety of ways you could do this, but IMHO the most obvious
is to start with the basic template for a form-based application and
modify that. So, do the usual Application.Run() on the new form instance,
but in your case only if you need to do the update.

Once you have that in place, then there's question of how to interact with
your updater thread. You don't say how you've implemented it, but it
seems to me that the BackgroundWorker class would work fine in this
instance. It provides well-defined mechanisms for all of the behaviors
you're looking for. In particular, progress notifications as well as
completion notification. In the completion notification, you can use
Invoke() to call some code that closes your progress form, and the
application will exit at that point (well, it will return from
Application.Run(), and that should naturally fall out of the Main()
method, exiting the application).

Pete
 
HI,

Thanks so much, Application.Run()! thats all it needed!

Oh , i used the WebClient class inside the Updater class. It has a
DownfileAsync() and DownProgressChanged and DownloadCompleted events

Gideon
 

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

Back
Top