后台工作者

发布于 2024-09-17 17:06:53 字数 59 浏览 2 评论 0原文

如何与后台线程一起维护前台线程。如果我尝试将项目添加到 do work 中的列表中,则会出现跨线程异常。

how to maintain a foreground thread along with background thread. if i try to add items to the list in do work, it's giving me a cross thread exception.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凤舞天涯 2024-09-24 17:06:53

一般来说,UI 更新只能从 UI 线程本身执行。

使用 BackgroundWorker 执行此操作的机制是调用工作线程的 ReportProgress 方法(确保WorkerReportsProgress = true)。此方法将引发 ProgressChanged 事件,由 UI 线程处理。

因此,如果您想将项目添加到 ListBox 控件,例如,基于 BackgroundWorker 正在执行的某些工作,请调用 ReportProgress 并传递任何内容您需要的数据作为参数。此数据将存储在 UserStateProgressChangedEventArgs属性code> 由事件提供。您的事件处理程序可以获取此数据并用它填充 ListBox

或者,您可以通过处理工作人员的 在末尾添加所有内容RunWorkerCompleted 事件。如果您的工作人员执行的工作不需要那么长时间,那么这通常是更可取的,因为它更简单,并且对 UI 的压力较小(需要执行的离散更新较少)。

In general, UI updates may only be performed from the UI thread itself.

The mechanism for doing this with a BackgroundWorker is to call the worker's ReportProgress method (make sure WorkerReportsProgress = true). This method will raise the ProgressChanged event, to be handled by the UI thread.

So if you want to add items to a ListBox control, for example, based on some work your BackgroundWorker is doing, call ReportProgress and pass whatever data you need as a parameter. This data will be stored in the UserState property of the ProgressChangedEventArgs supplied by the event. Your event handler can take this data and populate the ListBox with it.

Alternately, you can add everything at the end by handling the worker's RunWorkerCompleted event. If the work performed by your worker does not take all that long, this is often preferable as it's simpler and it stresses the UI less (with fewer discrete updates to perform).

懷念過去 2024-09-24 17:06:53

如果要将项目添加到 DoWork-Event 的列表中,则需要“调用”控件。

查看此处了解更多详细信息

If you want to add the item to the list in the DoWork-Event, you need to "invoke" the controls.

Look here for more details.

木有鱼丸 2024-09-24 17:06:53

您可以使用 ListView 触发事件来执行将项目添加到列表的工作,如下面的示例所示。

ListViewItem _listViewItem;

private void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
{
    //your loop to get list view item
    _listViewItem = new ListViewItem(mytext) {tag = mytagobject);
    _listViewItem.SubItems.Add(mysubitemtext);
    Invoke(new EventHandler(UpdateUiEvent), new[] { sender, e });
}

void UpdateUiEvent(object sender, EventArgs e)
{
    listView1.Items.Add(_listViewItem);
}

You can fire event that do the work of adding items to the list as shown in the below example using ListView.

ListViewItem _listViewItem;

private void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
{
    //your loop to get list view item
    _listViewItem = new ListViewItem(mytext) {tag = mytagobject);
    _listViewItem.SubItems.Add(mysubitemtext);
    Invoke(new EventHandler(UpdateUiEvent), new[] { sender, e });
}

void UpdateUiEvent(object sender, EventArgs e)
{
    listView1.Items.Add(_listViewItem);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文