Skip to main content
Left closed in review as "Original close reason(s) were not resolved" by Mark Rotteveel, chrslg, CPlus
edited tags
Link
TUS
  • 33
  • 3
added 458 characters in body; edited title
Added to review
Source Link
TUS
  • 33
  • 3

How Need to make a process wait until its completed beforedownload a new requestfile to for independent requests

I am developing a process in MVC to take input from a database object and convert the data to an Excel file export. I am using NPOI tool to do this.

The current conversion process is made completely independent with no external dependencies Or Shared objects.

The excel generation works fine, whereas it is a bit lengthy process and generates the output as required.

The problem isarises when one Excel file is processed, if anotherthe user initiates a second request when the first one is initiatedon progress. The first

When there are more than one requests the process breaksgenerates the byte[] and returns to the process continuescontroller to send the file to client side. But on return the newfile does not download. It only downloads the file related to the last request.

I want to make sure the first process finishes, before the next one can start.For example:

  • when there are 3 requests initiated one after the other.
  • The processes is carried out for first
  • The processes is carried out for second request
  • The processes is carried out for Third request
  • All requests are carried out simultaneously
  • Finishes Request1, Returns to the controller with Result for Request1
  • Does not download the file for Request1
  • Finishes Request2, Returns to the controller with Result for Request2
  • Does not download the file for Request2
  • Finishes Request3, Returns to the controller with Result for Request3
  • DOWNLOAD's the file for Request3

Hence converted the code to below using async await. Please noteHow can I am a beginner inhandle this and not completelymakes sure if what I did assures an await for each process. Please correct me if I am wrongall generated files are downloaded as they are processed.

byte[] IExportTranslator.CreateExcelExportFile(CalSetupVM**Controller**
public calSetupVM,ActionResult ExportExcel(ExportCriteriaVM exportCriteriaVM, string name)
{
  .....

  return Task.Run(async ()var =>excelByteArray await= CreateAsynExportFile_translator.CreateExcelExportFile(calendarSetupVM..., ganttExportCriteriaVM..., filterName)).Result;  ..);
}  
    return File(excelByteArray, _documentManager.GetMimeType("xlsx"), xlsxName);
}
public async Task<byte[]>
**Translator**

byte[] CreateAsynExportFileIExportTranslator.CreateExcelExportFile(CalSetupVM calSetupVMParam1, ExportCriteriaVM exportCriteriaVMParam2, string nameParam3)
{
    return await Task.Run(() => doSomeWork(calSetupVMParam1, exportCriteriaVMParam2, name)Param3);            
}

private byte[] doSomeWork(CalSetupVMCalendarSetupVM calSetupVMcalendarSetupVM, ExportCriteriaVMGanttExportCriteriaVM exportCriteriaVMganttExportCriteriaVM, string namefilterName)
{
    // dousing something(var ms = new MemoryStream())
     {
     ........
    return byte[...];.....
     return ms.ToArray();
     }
}

I did the above code based on some suggestions in the forums. And whatHow can I have read around async/await.

The basis need onensure the process is to wait until doSomeWorkfile is completed and returned the data to be sent to UI and then start any other request which may be initiated during the first run. And so on to queue thedownloaded for each request and handle one after the other.

How to make a process wait until its completed before a new request

I am developing a process to take input from a database object and convert the data to an Excel file export. I am using NPOI tool to do this.

The conversion process works fine, whereas it is a bit lengthy process.

The problem is when one Excel file is processed, if another request is initiated. The first process breaks and the process continues on the new request.

I want to make sure the first process finishes, before the next one can start.

Hence converted the code to below using async await. Please note I am a beginner in this and not completely sure if what I did assures an await for each process. Please correct me if I am wrong.

byte[] IExportTranslator.CreateExcelExportFile(CalSetupVM calSetupVM, ExportCriteriaVM exportCriteriaVM, string name)
{
    return Task.Run(async () => await CreateAsynExportFile(calendarSetupVM, ganttExportCriteriaVM, filterName)).Result;  
}        

public async Task<byte[]> CreateAsynExportFile(CalSetupVM calSetupVM, ExportCriteriaVM exportCriteriaVM, string name)
{
    return await Task.Run(() => doSomeWork(calSetupVM, exportCriteriaVM, name));            
}

private byte[] doSomeWork(CalSetupVM calSetupVM, ExportCriteriaVM exportCriteriaVM, string name)
{
    // do something...
    return byte[...];
}

I did the above code based on some suggestions in the forums. And what I have read around async/await.

The basis need on the process is to wait until doSomeWork is completed and returned the data to be sent to UI and then start any other request which may be initiated during the first run. And so on to queue the request and handle one after the other.

Need to download a file to for independent requests

I am developing a process in MVC to take input from a database object and convert the data to an Excel file export. I am using NPOI tool to do this.

The current conversion process is made completely independent with no external dependencies Or Shared objects.

The excel generation works fine and generates the output as required.

The problem arises when the user initiates a second request when the first one is on progress.

When there are more than one requests the process generates the byte[] and returns to the controller to send the file to client side. But on return the file does not download. It only downloads the file related to the last request.

For example:

  • when there are 3 requests initiated one after the other.
  • The processes is carried out for first
  • The processes is carried out for second request
  • The processes is carried out for Third request
  • All requests are carried out simultaneously
  • Finishes Request1, Returns to the controller with Result for Request1
  • Does not download the file for Request1
  • Finishes Request2, Returns to the controller with Result for Request2
  • Does not download the file for Request2
  • Finishes Request3, Returns to the controller with Result for Request3
  • DOWNLOAD's the file for Request3

How can I handle this and makes sure all generated files are downloaded as they are processed.

**Controller**
public ActionResult ExportExcel(ExportCriteriaVM exportCriteriaVM)
{
.....

    var excelByteArray = _translator.CreateExcelExportFile(..., ..., ...);
 
    return File(excelByteArray, _documentManager.GetMimeType("xlsx"), xlsxName);
}

**Translator**

byte[] IExportTranslator.CreateExcelExportFile(Param1, Param2, Param3)
{
    return doSomeWork(Param1, Param2, Param3);           
}

private byte[] doSomeWork(CalendarSetupVM calendarSetupVM, GanttExportCriteriaVM ganttExportCriteriaVM, string filterName)
{
     using (var ms = new MemoryStream())
     {
     ........
     ........
     return ms.ToArray();
     }
}

How can I ensure the file is downloaded for each request.

edited tags
Link
Lex Li
  • 64k
  • 12
  • 126
  • 163
Post Closed as "Needs details or clarity" by user207421, Servy, gnat
added 7 characters in body
Source Link
marc_s
  • 759.8k
  • 186
  • 1.4k
  • 1.5k
Loading
Source Link
TUS
  • 33
  • 3
Loading