第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

多線程c#WPF

多線程c#WPF

C#
catspeake 2021-05-06 09:09:47
將值從C#代碼綁定到WPF UI時(shí)遇到問題。我已經(jīng)了解了線程的基礎(chǔ)知識(shí),并且知道我必須使用Dispatcher來綁定自定義后臺(tái)線程中的ui線程。我有一個(gè)類似的要求,我想通過每秒點(diǎn)擊nse-stockmarket api來不斷更新我的WPF UI,并相應(yīng)地執(zhí)行一些邏輯,以便我可以顯示天氣股價(jià)正在上升或下降。以下是我如何嘗試實(shí)現(xiàn)此目標(biāo)的代碼...注意:我什至沒有“ CROSS-Thread”的異常 //globally declared var stockName = ""; //wpf button click  private void Button_Click(object sender, RoutedEventArgs e)  {      stockName = "LUPIN";      new Thread(() =>          {            RunStockParallel(share.Key);            Action action = new Action(SetTextBoxValues);          }).Start();   }    public void RunStockParallel(string stockName){  var count = 0 ;           do            {                HttpWebRequest stocks = null;                try                {                    //your logic will be here..                 }                catch (Exception e)                {                    //throw e;                }      //It will call the delegate method so that UI can update.                 Action action = new Action(SetTextBoxValues);                stockName = count++;            } while (true);} private void SetTextBoxValues()        {            this.Dispatcher.Invoke(() =>            {                this.text1.Text = stockName;            });        }當(dāng)我使用do-while循環(huán)時(shí),它將一直循環(huán)直到我終止應(yīng)用程序。在此do-while循環(huán)中,我不斷嘗試通過使用此“ counter ++;”更新Text1文本框來更新WPF ui。但是它沒有按預(yù)期工作。需要建議或解決方案。:)
查看完整描述

3 回答

?
米脂

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個(gè)贊

我得到了一個(gè)答案。.我在try catch塊內(nèi)的RunStockParallel方法中的代碼下面添加了代碼。


HttpWebRequest stocks = null;

try

{

   //your logic will be here.. 


       Dispatcher.BeginInvoke(new Action(() =>

       {

           txtName.Text = stockName;



       }), DispatcherPriority.Background);


}

catch (Exception e)

{

   //throw e;

}


查看完整回答
反對(duì) 回復(fù) 2021-05-08
  • 3 回答
  • 0 關(guān)注
  • 208 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)