久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

UI 無響應(yīng),直到操作完成

UI unresponsive until action is complete(UI 無響應(yīng),直到操作完成)
本文介紹了UI 無響應(yīng),直到操作完成的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

我不確定標題是否很好地描述了這個問題.基本上我擁有的是一個 WinForm 應(yīng)用程序,它從文件夾中檢索文件列表到 ListView 中,然后單擊一個按鈕通過 FTP 將它們上傳到遠程服務(wù)器.

I'm not sure if the Title is a good description of this issue or not. Essentially what I have is a WinForm app that retrieves a list of files from a folder into a ListView, then a button is clicked to upload them via FTP to a remote server.

從功能上講,該應(yīng)用按預(yù)期運行:

Functionally speaking, the app works as expected:

  1. 打開應(yīng)用
  2. 查看 ListView 控件中的文件列表
  3. 點擊上傳按鈕
  4. ListView 中列出的文件被上傳;每次成功上傳后,ListView 都會更新以顯示'成功'
  5. 上傳所有文件后,操作停止.

我的問題是,單擊上傳按鈕后,用戶界面幾乎沒有響應(yīng),直到操作完成.ListView 會在每個文件上傳時按預(yù)期更新,甚至保持活動行處于焦點.這是處理文件的 for 循環(huán).一點背景 - 在下面的代碼摘錄中,每個 for...loop 處理 2 個文件 - 主文件是 ListView 中唯一顯示的文件.每個循環(huán)中的第二個文件是一個觸發(fā)器文件,在其主文件發(fā)送后發(fā)送,即:.primary、.trigger.這兩個文件都必須發(fā)送才能注冊成功.如果主文件沒有對應(yīng)的觸發(fā)文件,則無法在ListView中上傳.

My issue is, after clicking the upload button the UI is pretty much unresponsive until the operation finishes. The ListView updates as expected as each file is uploaded and even keeps the active row in focus. Here is the for loop that processes the files. A little background - in the code excerpt below, each for...loop processes 2 files - the primary file is the only one that shows in the ListView. The 2nd file in each loop is a trigger file that is sent after its primary is sent, ie: .primary, .trigger. Both files have to send in order to register a success. If a primary file does not have a corresponding trigger file, it won't be available in the ListView for upload.

foreach (ListViewItem item in lvSourceFiles.Items)
{
    int rowIndex = item.Index;
    string fileName = item.SubItems[2].Text;

    lvSourceFiles.EnsureVisible(rowIndex);

    transferStatus = "Failed"; // Set this as a default

    // Transfer the source file first
    transferResult = session.PutFiles(readyFile, destFile, false, transferOptions);

    // Throw on any error
    transferResult.Check();

    // If the source file transfer was successful, then transfer the trigger file
    if (transferResult.IsSuccess)
    {
        transferResult = session.PutFiles(triggerFile, destFile, false, transferOptions);
        transferResult.Check();

        if (transferResult.IsSuccess)
        {
            transferStatus = "Success";
        }
    }

    UpdateResultsToListView(lvSourceFiles, rowIndex, fileName, transferStatus);
}

這是我需要實現(xiàn)某種異步功能的情況,還是有更好的方法來做到這一點,以便 UI 在上傳過程中不會凍結(jié)?本質(zhì)上,我希望能夠在上傳運行時與表單進行交互,例如有一個取消按鈕來停止上傳.就目前而言,在作業(yè)完成或終止應(yīng)用程序之前,我無法對表單執(zhí)行任何操作.

Is this a situation where I need to implement some sort of asynchronous functionality, or is there a better way to do this so the UI doesn't freeze during the upload process? Essentially I want to be able to interact with the form while the upload is running, such as having a cancel button to stop the upload. As it stands, I can't do anything with the form until the job completes, or I terminate the app.

謝謝,詹姆斯

推薦答案

您可以通過使用 async/await 和方便的 ThreadPool 線程https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.run" rel="nofollow noreferrer">Task.Run 方法:

You could offload the long-running operation to a ThreadPool thread, by using async/await and the handy Task.Run method:

transferResult = await Task.Run(() => session.PutFiles(readyFile, destFile, false, transferOptions));

...和:

transferResult = await Task.Run(() => session.PutFiles(triggerFile, destFile, false, transferOptions));

您還應(yīng)該添加 async事件處理程序中的 修飾符,以啟用 await 運算符.

You should also add the async modifier in the event handler, in order to enable the await operator.

重要提示:避免在卸載方法中執(zhí)行任何與 UI 相關(guān)的操作.如果您想在操作過程中與 UI 進行通信,例如對于 進度報告,使用 Progress<T> 類.

Important: Avoid doing anything UI related in the offloaded method. If you want to communicate with the UI during the operation, for example for progress reporting, use the Progress<T> class.

這篇關(guān)于UI 無響應(yīng),直到操作完成的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進行身份驗證并跨請求保留自定義聲明)
ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權(quán)不起作用)
How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何獲取守護進程或服務(wù)器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發(fā)技
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調(diào)用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
Getting access token using email address and app password from oauth2/token(使用電子郵件地址和應(yīng)用程序密碼從 oauth2/token 獲取訪問令牌)
New Azure AD application doesn#39;t work until updated through management portal(新的 Azure AD 應(yīng)用程序在通過管理門戶更新之前無法運行)
主站蜘蛛池模板: 伊人网91 | 亚洲欧美在线观看 | 亚洲成人av在线播放 | 亚洲va欧美va天堂v国产综合 | 日韩成人精品视频 | 日韩五月天 | 天天插天天狠天天透 | 国产乱码一二三区精品 | 国产成人精品一区二区三区网站观看 | 午夜精品久久久久久久 | 9久久婷婷国产综合精品性色 | 成人三区四区 | a网站在线观看 | 亚洲伊人久久综合 | 天堂一区| 成人黄色三级毛片 | 成人黄色三级毛片 | 精品视频免费 | 久草综合在线 | 日韩一二区 | 久久人体| 欧美激情视频一区二区三区在线播放 | 国产精品一区二区视频 | 国产精品视频一区二区三 | 在线观看精品 | 天天干天天草 | 精品国产91乱码一区二区三区 | 精品不卡| 国产成人免费视频网站视频社区 | 在线看亚洲 | 欧美日韩中文国产一区发布 | 久久不卡| 精品国产乱码久久久久久影片 | 狠狠爱综合 | 国产精品久久 | 操到爽| 亚洲在线| 日韩美女一区二区三区在线观看 | 成年人在线播放 | 国产一区二区三区视频免费观看 | 婷婷福利视频导航 |