- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我继承了从主线程调用 BeginInvoke 的代码(不是后台线程,通常是这种模式)。我试图了解它在这种情况下的实际作用。
在 BeginInvoke 中调用的方法是否符合下行到窗口的消息?文档说 asynchronously
,所以这是我的假设。
框架如何确定何时启动 BeginInvoke 调用的方法的优先级?
编辑:代码如下所示:
System.Action<bool> finalizeUI = delegate(bool open)
{
try
{
// do somewhat time consuming stuff
}
finally
{
Cursor.Current = Cursors.Default;
}
};
Cursor.Current = Cursors.WaitCursor;
BeginInvoke(finalizeUI, true);
这发生在 Form_Load 事件中。
最佳答案
现在我们看到了代码,很明显这只是将一些初始化从 Form_Load 中移出的一种方法,但它仍然在用户可以与表单交互之前发生。
对 BeginInvoke
的调用是在 Form_load 内部,而不是在另一个对象上调用,因此这是对 Form.BeginInvoke 的调用。所以发生的事情是这样的。
以下为原帖
I 取决于您对其调用 BeginInvoke 的对象。如果对象派生自 Control
,则 Control.BeginInvoke将在创建控件的线程上运行。请参阅 JaredPar 的回答。
但还有另一种使用 BeginInvoke 的模式。如果对象是委托(delegate),则 BeginInvoke 在单独的线程上运行回调,该线程可能专门为此目的创建。
public class Foo
{
...
public Object Bar(object arg)
{
// this function will run on a separate thread.
}
}
...
// this delegate is used to Invoke Bar on Foo in separate thread, this must
// take the same arguments and return the same value as the Bar method of Foo
public delegate object FooBarCaller (object arg);
...
// call this on the main thread to invoke Foo.Bar on a background thread
//
public IAsyncResult BeginFooBar(AsyncCallback callback, object arg)
{
Foo foo = new Foo();
FooBarCaller caller = new FooBarCaller (foo.Bar);
return caller.BeginInvoke (arg);
}
这种模式是从主线程而不是后台线程调用 BeginInvoke 的原因之一。
关于c# - BeginInvoke 的性能影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2473299/
我对 Dispatcher.CurrentDispatcher.BeginInvoke 和 BeginInvoke 之间的区别感到困惑 我有以下部分代码不起作用,UpdateCounts 方法中的代码
我最初尝试使用 Dispatcher 类 BeginInvoke 方法在我的 C# Windows 窗体应用程序的主 UI 线程上显示消息框。当我使用该方法时,消息框没有出现。我在传递给 BeginI
伙计们! 我的.net应用程序有问题。当我尝试在程序中打印时,它会崩溃崩溃 与任何错误消息。我从Windows中的事件查看器中获得的有关错误的信息。有人能帮我吗?但是在另一台计算机上它可以正常工作。我
我有简单的(我认为的)逻辑。 public static void NotifyAboutNewJob(int jobId, bool forceSending = false) {
我有一个 c# .NET winforms 应用程序进行此异步调用: simpleDelegate.BeginInvoke(null, null); 委托(delegate)正在调用我的函数,一切正常
1) 我听说当我们不调用 EndInvoke() 时可能会导致内存泄漏?你能演示一下这怎么会导致内存泄漏吗? 2) 当我想调用 EndInvoke() 时,我应该使用如下代码吗? namespace
我遇到了在多线程应用程序中使用 Dispatcher.BeginInvoke 导致的我无法理解的错误。 我的程序包含一个 List 对象,我使用多个线程循环遍历这些对象并执行一些计算。我已将我的代码结
我正在研究其他人的代码,但对多线程没有太多经验。我遇到了这行代码: BeginInvoke((MethodInvoker)delegate() { btnCalibrate.PerformClick(
我继承了从主线程调用 BeginInvoke 的代码(不是后台线程,通常是这种模式)。我试图了解它在这种情况下的实际作用。 在 BeginInvoke 中调用的方法是否符合下行到窗口的消息?文档说 a
为什么会出现此错误以及如何修复它。感谢帮助 错误 4 无法将 lambda 表达式转换为类型“System.Delegate”,因为它不是委托(delegate)类型 void provide
我在 WPF 应用程序中有一些代码如下所示: public class MyTextBox : System.Windows.Controls.TextBox, IDisposable { p
我有以下问题。 FindRoot 实际上在第三方 dll 中,我无法控制它。 它必须通过 Begin invoke 调用。有时,FindRoot 方法会抛出异常。这导致我的整个应用程序崩溃。现在,即使
这个问题在这里已经有了答案: Does Func.BeginInvoke use the ThreadPool? (2 个答案) 关闭 1 年前。 在我的 WPF 应用程序中,我想在非 UI 线程中
我是线程和异步编程的新手。我正在尝试学习这些概念,到目前为止,我已经理解了,但是还有一个问题要问。 可以说我想调用一个名为GetAllUsers()的方法,该方法需要很长时间才能执行,因此它不会阻塞资
我已经看到(并阅读)关于使用 Dispatcher.BeginInvoke 来确保 UI 更新发生在 UI 线程上。我明白这个道理。 但是我看到了在 View 代码隐藏中分配属性(例如 TextBlo
我想确定使用 BeginInvoke 时执行处理程序的线程。现在,每次我调用该方法时,处理程序都会由不同的线程执行。有没有办法确定线程? using System; using System.Thre
我有一个长时间运行的函数¹: public string FindPasswordFromHash(String hash) { ... } 它被称为: private void Button
我找不到这个简单问题的答案:BeginInvoke() 是否保证以原子方式执行被调用的委托(delegate)?例如,如果我有类似的东西 public ObservableCollection Ite
我收到一个 InvalidOperationException 消息 Cross-thread operation not valid.. _waitForm 是在主窗体的构造函数中创建的。屏幕截图中
我是编码新手,在我的第一个项目中,我必须从数显卡尺读取信息并将其显示在网络应用程序的文本框中。我从另一个项目中找到了代码,其他人在他们使用 Windows 窗体的地方创建了这些代码。我正在使用 vis
我是一名优秀的程序员,十分优秀!