Coesy
Coesy

Reputation: 936

Running Something In a thread with feedback

I have written a function which takes a whole bunch of files, zips them up and then allows the user to download this zip all through an asp.net website.

However, these zip files are going to be pretty large (like 1GB large), so it won't happen immediately, what i'd like to do is to be able to have this running on a seperate thread (possible?) while the user continues to navigate around the website, and once the zip is created, have some way of notifying them so they can then download this zip.

I was hoping for some pointers on how best to do this in c# and also in terms of UI.

Thanks in advance.

Upvotes: 3

Views: 750

Answers (4)

TalentTuner
TalentTuner

Reputation: 17556

Use Asynchronous Pages in ASP.NET 2.0

you can't wait too long in asp.net to complete the task due to the fear of page recycling , session expires , it would be great that you zip these files using some other process and update a flag either in database or somewhere in the private network that your zip component is ready !! your asp.net application should read this flag and than let the user know that their zip is ready to be downloaded.

Upvotes: 2

TheVillageIdiot
TheVillageIdiot

Reputation: 40517

You can use BackgroundWorker component. It has ability to raise progress event which you can use to give feedback to the main thread (like UI).

public void static Main()
{
    var bw = new BackgroundWorkder();
    bw.DoWork += _worker_DoWork;
    bw.RunWorkerCompleted += _worker_RunWorkerCompleted;
    bw.ProgressChanged += _worker_ProgressChanged;
    bw.WorkerReportsProgress = true;
    bw.WorkerSupportsCancellation = false;

    //START PROCESSING
    bw.RunWorkerAsync(/*PASS FILE DATA*/);
}

private void _worker_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker bw = sender as BackgroundWorker;
    var data = e.Argument as /*DATA OBJECT YOU PASSED*/;

    //PSEUDO CODE
    foreach(var file in FILES)
    {
        zipFile;
        //HERE YOU CAN REPORT PROGRESS
        bw.ReportProgress(/*int percentProgress, object userState*/)
    }
}

private void _worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // Just update progress bar with % complete or something
}

private void _worker_RunWorkerCompleted(object sender, 
                               RunWorkerCompletedEventArgs e)
{
    if (e.Error != null)
    {
        //...
    }
    else
    {
        //......
    }
}

Upvotes: 1

Greg Sansom
Greg Sansom

Reputation: 20850

The BackgroundWorker object is probably what you are looking for. Here is a good tutorial: http://dotnetperls.com/backgroundworker

Upvotes: 0

SteveCav
SteveCav

Reputation: 6729

use the backgroundworker: http://msdn.microsoft.com/en-us/library/cc221403(VS.95).aspx

Upvotes: 0

Related Questions