- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用异步套接字操作,当客户端收到一条消息时,它使用以下代码调用一个方法并执行 Methodinvoker:
private void AddText(string message)
{
MethodInvoker mi = new MethodInvoker(delegate
{
textBox1.Text += message;
});
this.Invoke(mi);
}
我不明白为什么要使用 methodinvoker。最好使用它而不是声明和调用不带参数的 void 方法?一定有充分的理由。
最佳答案
显然你的方法是从另一个线程调用的,然后是你的主线程。
Invoke :
Executes the specified delegate on the thread that owns the control's underlying window handle.
您应该始终在主线程上与您的用户控件进行交互。 Invoke
方法将委托(delegate)发送回应用程序的主线程。
关于c# - 为什么我应该使用 methodinvoker 而不是声明一个 void 方法并调用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15734950/
public void SomeMethod() { List someList = LoadList(); if(condition) { MethodInvoke
这有什么区别: richTextBox1.Invoke((MethodInvoker)(() => richTextBox1.Clear())); 还有这个: Invoke((MethodInvoke
MethodInvoker 的这两种用法到底有什么区别: 1: textBox1.Invoke(new MethodInvoker(b)); 2: textBox1.Invoke((MethodInv
我有一个后端类,它必须引发一个没有参数的事件。 MethodInvoker 具有所需的签名,但在后端类中使用它感觉有点奇怪,因为它是在 System.Windows.Forms 中定义的。在 .Net
我现在正在编写 GUI 应用程序有一段时间了,我经常使用的一件事是 MethodInvoker + lambda 函数来进行跨线程访问。 从我发现的例子中,我总是看到这样的东西: 版本 1 if (I
以下代码可以正常编译和运行。 void myInvokedMethod(string s) { Console.WriteLine(s); } void myInvoker() { I
我需要完成以下场景。 ThreadMethod() 是一个 Timer 方法,我在 Invoke 语句中更新了 UI。我需要等到 Invoke 完成它的工作,然后继续该方法。我该如何应对? publi
我有一个具有后台线程的 C++/CLI 应用程序。我经常希望它把结果发布到主 GUI 上。我读过elsewhere on SO MethodInvoker 可以用于此目的,但我正在努力将语法从 C#
我正在使用它来更改其他线程上的某些内容: MethodInvoker m = () => { login_submit.Text = "Login"; }; if (I
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: MethodInvoker vs Action for Control.BeginInvoke Th
我正在对计时器进行简单的 GUI 更新。如果我更新单个控件,哪种方法更好?像这样的 MethodInvoker: this.Invoke((MethodInvoker)delegate { sys
在查看其他一些开发人员的代码时,我已经看到了这两个版本的代码: 1. Me.BeginInvoke(New MethodInvoker(Sub() cbo.ShowPopup())) 2.
有人可以解释一下下面的代码吗: this.Invoke((MethodInvoker)delegate {
我需要从我的线程中访问一个共享的日志文件,所以我现在尝试使用 MethodInvoker 来读取文件并返回一个 bool 值,这取决于它是否找到了一个条目。但是得到这个错误,无法弄清楚如何让它返回一个
我正在使用异步套接字操作,当客户端收到一条消息时,它使用以下代码调用一个方法并执行 Methodinvoker: private void AddText(string message)
哪个更正确,为什么? Control.BeginInvoke(new Action(DoSomething), null); private void DoSomething() { Mess
两者有什么区别? Invoke((MethodInvoker) delegate { checkedListBox1.Items.RemoveAt(i); checke
#1 和#2 有什么区别: 代码 1(编译成功): byte[] GetSomeBytes() { return (byte[])this.Invoke((
我有一个 c# 但是代码,我需要它在 cli/c++ 中,但我总是得到这个错误 "Error 2 error C2955: 'BaseLibCS::MethodInvoker' : use of cl
使用 C# 2.0 和 MethodInvoker 委托(delegate),我有一个 GUI 应用程序从 GUI 线程或工作线程接收一些事件。 我使用以下模式处理表单中的事件: private vo
我是一名优秀的程序员,十分优秀!