Running something In a feedback thread

I wrote a function that takes a whole bunch of files, zips them up, and then allows the user to download this zip code through the asp.net website.

However, these zip files will be quite large (for example, 1 GB), so it won’t happen right away, what would I do to be able to work in a separate stream (maybe?), While the user continues to navigate the web to the site, and as soon as the zip is created, you can somehow notify them in order to then download this zip code.

I was hoping for some recommendations on how best to do this in C #, as well as in terms of user interface.

Thanks in advance.

+3
source share
4 answers

Using Asynchronous Pages in ASP.NET 2.0

you cannot wait too long in asp.net to complete the task due to fear of page re-installation, session expiration, it would be great if you archived these files using some other process and updated the flag either in the database or in where something on the private network that your mail component is ready! your asp.net application should read this flag and tell the user that their zip is ready to download.

+2
source

You can use the component BackgroundWorker. It has the ability to raise a progress event, which you can use to give feedback to the main thread (for example, the user interface).

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
    {
        //......
    }
}
+1

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

0
source

Source: https://habr.com/ru/post/1776385/


All Articles