亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從回調方法將進度傳回 UI

如何從回調方法將進度傳回 UI

PHP
慕的地8271018 2024-01-20 21:29:30
我正在開發一個應用程序,該應用程序使用 Jeff Kluge 的 WimgApi 包將 Windows 映像應用到磁盤。我在獲取示例回調方法來更新 UI 組件時遇到問題,特別是表單上的標簽(最好是進度條)。我嘗試使用委托來設置值,但這似乎不起作用,因為我無法弄清楚如何將委托傳遞給回調方法。如果我將回調方法設置為非靜態,我可以訪問表單屬性,但隨后我會遇到死鎖,即使我禁用死鎖打破,它也會鎖定。我被告知要考慮使用 IProgress 和 async,但是雖然我可以更改代碼以異步運行該方法(這有效并且 UI 不會鎖定),但我仍然無法弄清楚如何讓 MyCallbackMethod 將信息發送回用戶界面。//Apply Image Methodpublic void ApplyImage()        {            using (WimHandle wimHandle = WimgApi.CreateFile(@"C:\osimages\test.wim",                WimFileAccess.Read,                WimCreationDisposition.OpenExisting,                WimCreateFileOptions.None,                WimCompressionType.None))            {                // Always set a temporary path                WimgApi.SetTemporaryPath(wimHandle, Environment.GetEnvironmentVariable("TEMP"));                // Register a method to be called while actions are performed by WIMGAPi for this .wim file                WimgApi.RegisterMessageCallback(wimHandle, MyCallbackMethod);                try                {                    // Get a handle to the first image in the .wim file                    using (WimHandle imageHandle = WimgApi.LoadImage(wimHandle, 1))                    {                        // Apply the image contents to C:\Apply                        // This call is blocking but WIMGAPI will be calling MyCallbackMethod() during the process                        WimgApi.ApplyImage(imageHandle, @"X:\", WimApplyImageOptions.None);                    }                }                finally                {                    // Be sure to unregister the callback method                    //                    WimgApi.UnregisterMessageCallback(wimHandle, MyCallbackMethod);                }            }如果我嘗試在回調方法中訪問標簽屬性,我會收到“非靜態字段、方法或屬性 form1.progressLabel.text 需要對象引用”。我嘗試創建委托,但似乎在訪問回調中的方法時遇到問題。我觀看了幾個視頻并嘗試了解有關委托、回調和 async/backgroundworker 等內容的 msdn 文檔,但我似乎更加困惑。非常感謝我應該關注的任何指示/事情。
查看完整描述

2 回答

?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

免責聲明:我對 WimgApi 軟件包沒有任何經驗。但是該方法存在重載,WimgApi.RegisterMessageCallback該方法采用將傳遞給回調的任意對象。所以請嘗試這個:


WimgApi.RegisterMessageCallback(wimHandle, MyCallbackMethod, this);

并在回調中:


var form = (MyForm)userData;

if (form.InvokeRequired)

{

    form.Invoke((MethodInvoker)(() => UpdateProgressUI(...)));

}

else

{

    form.UpdateProgressUI(...);

}


查看完整回答
反對 回復 2024-01-20
?
森欄

TA貢獻1810條經驗 獲得超5個贊

在這里做出一些假設,但如果您一次只顯示一個進度表單,那么您應該能夠避免存儲對其的靜態引用。IE:


class ProgressForm

{

    private static ProgressForm staticRef;


    private void Form_Loaded(object sender, EventArgs e)

    {

        staticRef = this;

    }


    private void InternalCallback(uint m, IntPtr w, IntPtr l, IntPtr u)

    {

        // Ensure we're touching UI on the right thread

        if (Dispatcher.InvokeRequired)

        {

            Dispatcher.Invoke(() => InternalCallback(m, w, l, u));

            return;

        }


        // Update UI components

        // ....

    }


    private static uint StaticCallback(uint m, IntPtr w, IntPtr l, IntPtr u)

    {

        staticRef?.InternalCallback(m, w, l, u);


        return 0;

    }

}


查看完整回答
反對 回復 2024-01-20
  • 2 回答
  • 0 關注
  • 165 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號