簡體   English   中英

Winforms中的后台工作者未更新文本框

[英]Background Worker In Winforms Not Updating TextBox

我試圖使用后台工作程序,以便將取消按鈕添加到我的winform中。

我添加了以下語法,但是文本框從未像嘗試實現后台工作程序之前那樣以任何進度進行更新。

public Form1()
{
    AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
    {
        string resourceName = new AssemblyName(args.Name).Name + ".dll";
        string resource = Array.Find(this.GetType().Assembly.GetManifestResourceNames(), element => element.EndsWith(resourceName));

        using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource))
        {
            Byte[] assemblyData = new Byte[stream.Length];
            stream.Read(assemblyData, 0, assemblyData.Length);
            return Assembly.Load(assemblyData);
        }
    };
    InitializeComponent(); 
    backgroundWorker1.WorkerReportsProgress = true;  
    backgroundWorker1.WorkerSupportsCancellation = true;
}
private void btnQuery_Click(object sender, EventArgs e)
{
    backgroundWorker1.RunWorkerAsync();
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{

        //Iterating the array
        foreach (string s in sxc)
        {
            txt_ProgressDetails.Visible = true;
            AppendTextBoxLine("Getting Data For " + s);

            //Put data into DataTable
            PopulateDT(s);
        }

        //If the cancel button was pressed
        if (backgroundWorker1.CancellationPending)
        {
            e.Cancel = true;
            return;
        }
}

    private void AppendTextBoxLine(string statusMessage)
    {
        if (txt_ProgressDetails.Text.Length > 0)
            txt_ProgressDetails.AppendText(Environment.NewLine);
        txt_ProgressDetails.AppendText(statusMessage);
    }

BackgroundWorker的Do_Work事件在非STA線程中運行,您無法從那里更新UI。 您可以重寫代碼,以使用InvokeRequredInvoke方法更新創建UI元素的線程。

將此添加到您的表單:

delegate void UpdateUICallback(string statusMessage);

並將您的AppendTextBoxLine方法更改為:

if (InvokeRequired)
{
    UpdateUICallback d = new UpdateUICallback(AppendTextBoxLine);
    this.Invoke(d, new object[] {statusMessage});
}
else
{
    if (txt_ProgressDetails.Text.Length > 0)
            txt_ProgressDetails.AppendText(Environment.NewLine);
    txt_ProgressDetails.AppendText(statusMessage);
}

因此您的代碼將如下所示:

public Form1()
{
    AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
    {
        string resourceName = new AssemblyName(args.Name).Name + ".dll";
        string resource = Array.Find(this.GetType().Assembly.GetManifestResourceNames(), element => element.EndsWith(resourceName));

        using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource))
        {
            Byte[] assemblyData = new Byte[stream.Length];
            stream.Read(assemblyData, 0, assemblyData.Length);
            return Assembly.Load(assemblyData);
        }
    };
    InitializeComponent(); 
    backgroundWorker1.WorkerReportsProgress = true;  
    backgroundWorker1.WorkerSupportsCancellation = true;
}
private void btnQuery_Click(object sender, EventArgs e)
{
    backgroundWorker1.RunWorkerAsync();
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{

        //Iterating the array
        foreach (string s in sxc)
        {
            txt_ProgressDetails.Visible = true;
            AppendTextBoxLine("Getting Data For " + s);

            //Put data into DataTable
            PopulateDT(s);
        }

        //If the cancel button was pressed
        if (backgroundWorker1.CancellationPending)
        {
            e.Cancel = true;
            return;
        }
}

delegate void UpdateUICallback(string statusMessage);
private void AppendTextBoxLine(string statusMessage)
{
    if (InvokeRequired)
    {
        UpdateUICallback d = new UpdateUICallback(AppendTextBoxLine);
        this.Invoke(d, new object[] {statusMessage});
    }
    else
    {
        if (txt_ProgressDetails.Text.Length > 0)
                txt_ProgressDetails.AppendText(Environment.NewLine);
        txt_ProgressDetails.AppendText(statusMessage);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM