如何将参数传递给后台工作者?

时间:2014-12-09 13:25:19

标签: vb.net parameters backgroundworker

我目前有一个用数据填充列表视图的函数。该函数传递参数。

我现在想要在与主ui线程不同的线程中填充listview。但是我有点不确定如何将参数传递给线程。

2 个答案:

答案 0 :(得分:3)

必须通过RunWorkAsync作为参数(对象)传递。请务必将其投射到您传入的任何内容中。

Private Sub frm_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    bgwThread.RunWorkerAsync('your parameters here')

    'e.g.:
    Dim sTemp As String = "Hello"
    bgwThread.RunWorkerAsync(sTemp) 
End Sub

Private Sub bgWorker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles bgwThread.DoWork
    Dim sThisIsYourParameter As String = CStr(e.Argument)

    '...
    DoStuff()
End Sub

虽然正如Hans上面所说的那样,你无法在另一个线程中填充ListView。这只是“如何将参数传递给后台工作者。”

答案 1 :(得分:0)

旧帖子,但这是对绊脚石的任何人的完整解决方案。 这允许将参数传递给Backgroundworker,支持取消操作,并将信息传递回主线程(通过ProgressChanged),在此可以更新ListView。我从头开始创建了一个解决方案。经过测试的工作。

C#/ .NET Framework 4.6.2 / VS2019内部版本16.4.1

BackgroundWorker1属性:

WorkerReportsProgress = True

WorkerSupportsCancellation = True

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace test2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(Object sender, EventArgs e)
        {
            listView1.View = View.Details;
            listView1.GridLines = true;

            listView1.Columns.Add("Store",100);
            listView1.Columns.Add("Product", 100);
            listView1.Columns.Add("Price", 60);
            listView1.Columns.Add("Qty", 60);
        }

        private void backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e)
        {
            if(!(e.Argument is String store)) return;

            List<String> items = new List<String>();

            if(backgroundWorker1.CancellationPending) {e.Cancel = true; return;}

            items.Add(store);
            items.Add("Bananas");
            items.Add("0.79");
            items.Add("50");

            backgroundWorker1.ReportProgress(0, items);
        }

        private void backgroundWorker1_ProgressChanged(Object sender, ProgressChangedEventArgs e)
        {
            if(!(e.UserState is List<String> resultitems)) return;

            ListViewItem itm = new ListViewItem(resultitems.ToArray());
            listView1.Items.Add(itm);
        }

        private void backgroundWorker1_RunWorkerCompleted(Object sender, RunWorkerCompletedEventArgs e)
        {
            //Do any final cleanup once backgroundworker is complete
        }

        private void button1_Click(Object sender, EventArgs e)
        {
            if(backgroundWorker1.IsBusy)
            {
                backgroundWorker1.CancelAsync();
                return;
            }

            backgroundWorker1.RunWorkerAsync("Glendale");
        }

        private void button2_Click(Object sender, EventArgs e)
        {
            if(backgroundWorker1.IsBusy)
            {
                backgroundWorker1.CancelAsync();
                return;
            }

            backgroundWorker1.RunWorkerAsync("Toledo");
        }
    }
}

Program Output