This is my code:
Action<int, ProgressBar, Label, Label, int, Button> downloadFileAsync = (i, pb, label2, label1, ServID, button1) =>
{
var bd = AppDomain.CurrentDomain.BaseDirectory;
var fn = bd + "/" + i + ".7z";
var down = new WebClient();
DownloadProgressChangedEventHandler dpc = (s, e) =>
{
label1.Text = "Download Update: " + i + " / " + ServID;
int rec =Convert.ToInt16(e.BytesReceived / 1024);
int total =Convert.ToInt16(e.TotalBytesToReceive / 1024) ;
label2.Text = "Downloaded: " + rec.ToString() + "KB / " + total.ToString() + " KB";
pb.Value = e.ProgressPercentage;
};
AsyncCompletedEventHandler dfc = null; dfc = (s, e) =>
{
label1.Text = "Extracting Files...";
Rar Ra = new Rar();
Ra.Open(i + ".7z");
Ra.Unrar(AppDomain.CurrentDomain.BaseDirectory);
File.Delete(fn);
down.DownloadProgressChanged -= dpc;
down.DownloadFileCompleted -= dfc;
down.Dispose();
if (ServID == i)
{
button1.Enabled = true;
label1.Text = "Have Fun In-Game...";
label2.Text = "Done...";
}
};
down.DownloadProgressChanged += dpc;
down.DownloadFileCompleted += dfc;
down.DownloadFileAsync(new Uri("http://unknowndekaron.net/updatee/" + i + "/" + i + ".7z"), fn);
};
And This is My Call:
while (i <= ServID)
{
downloadFileAsync(i, progressBar1, label2, label1, ServID, button1);
i++;
}
Decompress:
public void Decompress(int i)
{
Rar Ra = new Rar();
Ra.Open(i + ".7z");
Ra.Unrar(AppDomain.CurrentDomain.BaseDirectory);
}
In this code program downloads all the updates at once…
It starts to decomprees the first one which’s download is completed completed.
I need the app to download only one update at once and then decompreeses it.
You can use unelegant solution with using booleans:
You should move the top level lambda into new
asyncmethod. You can call it in while normaly. After that declare booleandownloadCompletedon the start of your top level lambda and set it to false. Then go toAsyncCompletedEventHandlerand at the end set the value ofdownloadCompletedto true – this will signalize the last while loop that we have finished. End of top level lamba should wait until fullfillment of downloading and compressing. This won’t change the state of the application to “not responding”.Example
Call:
New method:
Calling while loop can stay as it is.