pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

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

問題描述

限時送ChatGPT賬號..

我不確定標(biāo)題是否很好地描述了這個問題.基本上我擁有的是一個 WinForm 應(yīng)用程序,它從文件夾中檢索文件列表到 ListView 中,然后單擊一個按鈕通過 FTP 將它們上傳到遠(yuǎn)程服務(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ù)期運(yùn)行:

Functionally speaking, the app works as expected:

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

我的問題是,單擊上傳按鈕后,用戶界面幾乎沒有響應(yīng),直到操作完成.ListView 會在每個文件上傳時按預(yù)期更新,甚至保持活動行處于焦點(diǎn).這是處理文件的 for 循環(huán).一點(diǎ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);
}

這是我需要實(shí)現(xiàn)某種異步功能的情況,還是有更好的方法來做到這一點(diǎn),以便 UI 在上傳過程中不會凍結(jié)?本質(zhì)上,我希望能夠在上傳運(yùn)行時與表單進(jìn)行交互,例如有一個取消按鈕來停止上傳.就目前而言,在作業(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 運(yùn)算符.

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

重要提示:避免在卸載方法中執(zhí)行任何與 UI 相關(guān)的操作.如果您想在操作過程中與 UI 進(jìn)行通信,例如對于 進(jìn)度報告,使用 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 進(jìn)行身份驗(yàn)證并跨請求保留自定義聲明)
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(如何獲取守護(hù)進(jìn)程或服務(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)用程序在通過管理門戶更新之前無法運(yùn)行)
主站蜘蛛池模板: 外观设计_设备外观设计_外观设计公司_产品外观设计_机械设备外观设计_东莞工业设计公司-意品深蓝 | 重庆监控_电子围栏设备安装公司_门禁停车场管理系统-劲浪科技公司 | 盘扣式脚手架-附着式升降脚手架-移动脚手架,专ye承包服务商 - 苏州安踏脚手架工程有限公司 | 恒压供水控制柜|无负压|一体化泵站控制柜|PLC远程调试|MCGS触摸屏|自动控制方案-联致自控设备 | 校服厂家,英伦校服定做工厂,园服生产定制厂商-东莞市艾咪天使校服 | 货车视频监控,油管家,货车油管家-淄博世纪锐行电子科技 | 胀套-锁紧盘-风电锁紧盘-蛇形联轴器「厂家」-瑞安市宝德隆机械配件有限公司 | 杭州ROHS检测仪-XRF测试仪价格-百科 | MES系统工业智能终端_生产管理看板/安灯/ESOP/静电监控_讯鹏科技 | Akribis直线电机_直线模组_力矩电机_直线电机平台|雅科贝思Akribis-杭州摩森机电科技有限公司 | 彩超机-黑白B超机-便携兽用B超机-多普勒彩超机价格「大为彩超」厂家 | 天津拓展_天津团建_天津趣味运动会_天津活动策划公司-天津华天拓展培训中心 | 海南在线 海南一家 | 贝朗斯动力商城(BRCPOWER.COM) - 买叉车蓄电池上贝朗斯商城,价格更超值,品质有保障! | 上海公众号开发-公众号代运营公司-做公众号的公司企业服务商-咏熠软件 | 定制异形重型钢格栅板/钢格板_定做踏步板/排水沟盖板_钢格栅板批发厂家-河北圣墨金属制品有限公司 | 新能源汽车教学设备厂家报价[汽车教学设备运营18年]-恒信教具 | atcc网站,sigma试剂价格,肿瘤细胞现货,人结肠癌细胞株购买-南京科佰生物 | 广州昊至泉水上乐园设备有限公司 | 无线遥控更衣吊篮_IC卡更衣吊篮_电动更衣吊篮配件_煤矿更衣吊篮-力得电子 | 河北凯普威医疗器材有限公司,高档轮椅系列,推车系列,座厕椅系列,协步椅系列,拐扙系列,卫浴系列 | 免费个人pos机申请办理-移动pos机刷卡-聚合收款码办理 | PTFE接头|聚四氟乙烯螺丝|阀门|薄膜|消解罐|聚四氟乙烯球-嘉兴市方圆氟塑制品有限公司 | 云杂志网-学术期刊-首页| 手术室净化厂家_成都实验室装修公司_无尘车间施工单位_洁净室工程建设团队-四川华锐16年行业经验 | 高效复合碳源-多核碳源生产厂家-污水处理反硝化菌种一长隆科技库巴鲁 | 冰晶石|碱性嫩黄闪蒸干燥机-有机垃圾烘干设备-草酸钙盘式干燥机-常州市宝康干燥 | 西安中国国际旅行社(西安国旅)| 成都茶楼装修公司 - 会所设计/KTV装修 - 成都朗煜装饰公司 | 泰来华顿液氮罐,美国MVE液氮罐,自增压液氮罐,定制液氮生物容器,进口杜瓦瓶-上海京灿精密机械有限公司 | 济南网站策划设计_自适应网站制作_H5企业网站搭建_济南外贸网站制作公司_锐尚 | 石油/泥浆/不锈钢防腐/砂泵/抽砂泵/砂砾泵/吸砂泵/压滤机泵 - 专业石油环保专用泵厂家 | 天助网 - 中小企业全网推广平台_生态整合营销知名服务商_天助网采购优选 | 北京网站建设-企业网站建设-建站公司-做网站-北京良言多米网络公司 | 纯水设备_苏州皙全超纯水设备水处理设备生产厂家 | 硫酸亚铁-聚合硫酸铁-除氟除磷剂-复合碳源-污水处理药剂厂家—长隆科技 | 仿古瓦,仿古金属瓦,铝瓦,铜瓦,铝合金瓦-西安东申景观艺术工程有限公司 | 切铝机-数控切割机-型材切割机-铝型材切割机-【昆山邓氏精密机械有限公司】 | 瓶盖扭矩测试仪-瓶盖扭力仪-全自动扭矩仪-济南三泉中石单品站 | 万烁建筑设计院-建筑设计公司加盟,设计院加盟分公司,市政设计加盟 | 杭州货架订做_组合货架公司_货位式货架_贯通式_重型仓储_工厂货架_货架销售厂家_杭州永诚货架有限公司 |