从另一个工作线程调用主线程 [英] Invoke Main thread from another working Thread

查看:104
本文介绍了从另一个工作线程调用主线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
所以我找不到解决我问题的方法!
我正在使用Windows窗体.
我有1个按钮和1个ProgressBar,2个类(1个主类,自定义类)
所以我将一些数据从主类传递到自定义类(例如10)
自定义类的计数从1到10(使用委托和beginInvoke方法的自定义类)

如何在WinForm中将值传递给progressBar? (例如,自定义类变量递增,而progressBar值并行递增).

感谢您的回答! :)

Hi guys!
So I can''t find solution for my issue!
I''m using Windows forms.
I have 1 button and 1 progressBar, 2 classes(1 main class, custom class)
So I pass some data from main class to custom class (for example number 10)
custom class count from 1 to 10(custom class using delegate, and beginInvoke Method)

how I can pass value to progressBar in winForm? (for example custom class variable increments, and progressBar value increments parallel).

Thanks for answers! :)

推荐答案

首先,您使用了错误的术语-您不是在调用"主线程,因为它已经在运行.

其次,您应该学习如何使用google:

[
First, you''re using the wrong terminology - you''re not "invoking" the main thread because it''s already running.

Second, you should learn how to use google:

Google results for "C# update ui from thread"[^]


您不能从非UI线程调用与UI相关的任何内容.相反,您需要使用System.Threading.DispatcherInvokeBeginInvoke方法(对于Forms或WPF)或System.Windows.Forms.Control(仅对于Forms).

在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


查看CP文章 C#事件实施基础知识,最佳做法和惯例 [ ^ ].一篇关于您的问题的好文章.
Check out CP article C# Event Implementation Fundamentals, Best Practices and Conventions[^]. A good article about your question.


这篇关于从另一个工作线程调用主线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆