簡體   English   中英

ASP.NET處理在后台

[英]ASP.NET Processing in the background

有沒有辦法在后台將文件從URL下載到服務器? 提交表單時,文件上傳在后台自動處理,因此用戶無需等待文件上傳。

在Windows應用程序中,有BackgroundWorker類。 但是ASP.NET怎么樣?

這不是您想要在Response線程上運行的那種東西,因為它會延遲對用戶的響應,直到可以下載和處理該文件。 它更適合作為計算機上的服務運行的不同線程(或Azure工作者角色),它將為您執行這些項目。

但是,如果您確實希望(或必須)通過IIS / ASP.net運行它,並且如果您不關心向用戶返回任何內容,則可以在不同的線程中或通過調用的委托運行所有邏輯異步。 有關異步運行委托的示例,請參閱此答案 (從那里引用)。

private delegate void DoStuff(); //delegate for the action

protected void Button1_Click(object sender, EventArgs e)
{
    //create the delegate
    DoStuff myAction = new DoStuff(DownloadStuff); 

    //invoke it asynchronously, control passes to next statement
    myAction.BeginInvoke(null, null);
}

private void DownloadStuff() { 
  // your functionality here - this can also go in a code library
}

有關如何使用委托的更多信息,請參閱MSDN (包括使用參數傳遞信息)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM