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

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

如何在我的方法運(yùn)行的同時加載進(jìn)度條

如何在我的方法運(yùn)行的同時加載進(jìn)度條

C#
胡子哥哥 2023-08-13 14:09:34
我有一個長時間運(yùn)行的方法,所以我創(chuàng)建了一個進(jìn)度條來顯示我的方法完成的百分比,但我很難弄清楚如何做到這一點(diǎn),將我的進(jìn)度條與我的方法同步 => excelHelper.InsertNewRows(); 。public void ButtonSubmit_Click(object sender, EventArgs e){    if (isProcessRunning)    {        MessageBox.Show("A Process is aleady running");        return;    }    Thread backgroundThread = new Thread(        new ThreadStart(() =>        {            for (int n = 0; n < 100; n++)            {                isProcessRunning = true;                Thread.Sleep(50);                progressBar1.Invoke(                    new Action(() =>                    {                        progressBar1.Value = n;                        label3.Text = ("Progress: " + n + "%");                    }                 ));            }            MessageBox.Show("Thread completed!");            progressBar1.Invoke(                new Action(() =>                {                    progressBar1.Value = 0;                }                ));            isProcessRunning = false;        }        ));    backgroundThread.Start();    excelHelper.InsertNewRows();    var folder = TextOutputFile.Text + @"\" +    DateTime.Now.ToString("yyyy_MM_dd_") + "SA_Analysis_Report.xlsx";    excelHelper.Save(folder);    MessageBox.Show("File has been added to file");}
查看完整描述

3 回答

?
子衿沉夜

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

excelHelper.InsertNewRows();在我看來,你的問題是,工作線程和進(jìn)度條線程之間沒有任何通信。兩個線程都在運(yùn)行,沒有任何有關(guān)另一個線程及其進(jìn)度的信息。


您可以重寫后臺線程,因此它能夠?qū)俜直茸鳛閰?shù),該參數(shù)在您調(diào)用線程時顯示。


例如偽代碼:


public void progressbarThread(int percentage)?

{

? ? // Invoke the percentage to your progressbar, then terminate this thread

}


public void InsertNewRows()?

{

? ? // Do something...

? ? // 10%

? ? Thread backgroundThread = new Thread(new ThreadStart(() => progressBarThread(10)));

? ? backgroundThread.Start();

? ? // Do something...

? ? // 50%

? ? Thread backgroundThread = new Thread(new ThreadStart(() => progressBarThread(50)));

? ? backgroundThread.Start();

? ? // etc. etc.

}

查看完整回答
反對 回復(fù) 2023-08-13
?
UYOU

TA貢獻(xiàn)1878條經(jīng)驗 獲得超4個贊

肯定有資源可以幫助您解決這個問題,但是我花了大約 2 天的時間來解決這個問題,所以我將在這里幫助您并減輕您的頭痛。進(jìn)度條本身位于主 UI 線程下(就像表單中的任何對象一樣),需要由主線程處理。無論你想做什么,都可以通過這樣的線程來處理

    String a, b;
    a = txtUsername.Text;
    b = txtPassword.Password;
    Thread Login = new Thread(() => CompleteLogin(a, b));
    InversePbVisibility();
    Login.Start();

InversePbVisibility() 方法將被您正在執(zhí)行的使進(jìn)度條對用戶可見的任何操作所替換。附帶說明一下,在聲明的線程上運(yùn)行的任何方法都只能傳遞變量,而不能傳遞主線程控制下的任何內(nèi)容。



查看完整回答
反對 回復(fù) 2023-08-13
?
慕妹3146593

TA貢獻(xiàn)1820條經(jīng)驗 獲得超9個贊

I use this form class: -


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading;

using System.Threading.Tasks;

using System.Windows.Forms;


namespace Yournamespace

{

    public partial class frmProgressBar : Form

    {

        public Action Worker { get; set; }


        System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();


        public const long MINIMUM_DISPLAY_TIME = 300;    // Minimum time to display the progress bar is 300 milliseconds


        public frmProgressBar(Action worker)

        {

            InitializeComponent();


            if(worker == null)

            {

                throw new ArgumentNullException();

            }


            Worker = worker;

        }


        protected override void OnLoad(EventArgs e)

        {

            base.OnLoad(e);


            Task.Factory.StartNew(Worker).ContinueWith(t => { this.Close(); }, TaskScheduler.FromCurrentSynchronizationContext());

        }


        protected override void OnClosed(EventArgs e)

        {

            // the code that you want to measure comes here

            watch.Stop();

            var elapsedMs = watch.ElapsedMilliseconds;


            if (elapsedMs < MINIMUM_DISPLAY_TIME) //ensure progress bar is displayed for at least 100 milliseconds, otherwise it just looks like the parent main form glitches.

            {

                long lDelay = MINIMUM_DISPLAY_TIME - elapsedMs;


                Thread.Sleep((int)lDelay);

            }

        }

    }

}


The form design looks like this : -


[![enter image description here][1]][1]



  [1]: https://i.stack.imgur.com/n4XqU.jpg



And I call it like this: -


            using (Yournamespace.frmProgressBar frm = new Yournamespace.frmProgressBar(Yourprocess))

            {

                frm.ShowDialog(this);

            }



Yourprocess is the code that you want to execute that is causing the delay.


The main problem with this implementation is Yourprocess cannot return a value or take parameters. I am sure the code can be changed to accomodate this but I did not have time so I use globals to pass in data and to see results(shame on me).


This is not my code, although I have modified it, came from a you tube video - Wait Form Dialog.

編輯。我忘了說我將表單設(shè)置為 100% 不透明度,因此每當(dāng)我使用它時,進(jìn)度條似乎都會漂浮在我的 winform 上方。


查看完整回答
反對 回復(fù) 2023-08-13
  • 3 回答
  • 0 關(guān)注
  • 146 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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