标签: invoke

调用任何交叉线程代码的最佳方法?

我知道之前已经问过这个问题,但我正在寻找一种方法:

  1. 简化安全交叉线程代码的创建.
  2. 在任何情况下重用此代码(没有Windows窗体引用).

这是我到目前为止,但我想删除Windows窗体引用.有任何想法吗?

public delegate void SafeInvokeDelegate(System.Action action);
public class SafeInvoke
{
    private readonly System.Windows.Forms.Control _threadControl;

    public SafeInvoke()
    {
        _threadControl = new System.Windows.Forms.Control();
    }

    public void Invoke(System.Action action)
    {
        if (_threadControl.InvokeRequired)
            _threadControl.Invoke(new SafeInvokeDelegate(Invoke), new object[] {action});
        else if (action != null) action();
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的类可能会这样使用:

SafeInvoke _safeInvoker = new SafeInvoke();
void SafeClearItems()
{
    _safeInvoker.Invoke(delegate
        {
            listView1.Items.Clear();
        });
}
Run Code Online (Sandbox Code Playgroud)

我如何删除SafeInvoke类中的System.Windows.Forms.Control但保持相同的功能?

.net c# invoke

36
推荐指数
4
解决办法
4万
查看次数

是否适合扩展Control以提供始终如一的安全Invoke/BeginInvoke功能?

在我维护一个严重违反winforms中的跨线程更新规则的旧应用程序的过程中,我创建了以下扩展方法,以便在我发现它们时快速修复非法调用:

/// <summary>
/// Execute a method on the control's owning thread.
/// </summary>
/// <param name="uiElement">The control that is being updated.</param>
/// <param name="updater">The method that updates uiElement.</param>
/// <param name="forceSynchronous">True to force synchronous execution of 
/// updater.  False to allow asynchronous execution if the call is marshalled
/// from a non-GUI thread.  If the method is called on the GUI thread,
/// execution is always synchronous.</param>
public static void SafeInvoke(this Control uiElement, Action updater, bool forceSynchronous)
{
    if …
Run Code Online (Sandbox Code Playgroud)

c# extension-methods controls invoke winforms

33
推荐指数
2
解决办法
8592
查看次数

如何使用Dispatcher.Invoke返回值?

任何人都知道如何从中返回值Dispatcher.Invoke?我想返回ComboBox的选定索引.

谢谢!

wpf return invoke dispatcher

30
推荐指数
2
解决办法
2万
查看次数

带有C++布尔函数的C#DllImport无法正确返回

我在C++ DLL中有以下功能

extern "C" __declspec(dllexport) bool Exist(const char* name)
{
 //if (g_Queues.find(name) != g_Queues.end())
 // return true;
 //else
 // return false;
 return false;
}
Run Code Online (Sandbox Code Playgroud)

在我的C#类中,我有以下内容:

[DllImport("Whisper.dll", EntryPoint="Exist", CallingConvention=CallingConvention.Cdecl)]
        public static extern bool Exist(string name);
Run Code Online (Sandbox Code Playgroud)

然而,每当我调用我的函数时,它总是返回true,即使我注释掉了我的小函数并使其返回false.我觉得我的调用约定或P/Invoking我的DLL的任何其他问题都有问题,可能与字符串和const char*相对应,但是现在我完全无能为力.我究竟做错了什么?为什么它返回true而不是false?

编辑:我已经发现这与const char*或字符串无关,因为问题仍然存在于空函数中.我试过改变Cdecl和StdCall之间的调用约定,但都没有正常工作.我还设法调试我的DLL并且它被正确调用并且确实返回false,但是一旦回到C#,它就会变成现实.更改CharSet也没有任何效果.我已经确保每次都为我的C#程序提供最新版本的DLL,所以这不应该是一个问题.再一次,当我实际上返回false时,我完全不清楚为什么结果是真的.

EDIT2: SOReader为我提供了一个修复另一个重要问题的建议,请参阅我的评论.可悲的是,它并没有解决退货问题.

EDIT3:我得出结论,将Exist(bool)的返回类型更改为(int)突然使其返回正确的数字(true = 1,false = 0).这意味着C++的bool和C#的bool之间可能存在问题.我可以继续使用int作为bool,但这仍然无法解释原始问题.也许其他人可以在这个上启发我?也许这与我使用x64这一事实有关(虽然两个poject都编译为x86)

c# c++ import dll invoke

29
推荐指数
2
解决办法
2万
查看次数

如何从除创建它之外的线程读取组合框?

我试图从一个线程读取一个combobox.Text而不是它创建的线程,但我收到错误:

System.Windows.Forms.dll中发生了未处理的"System.InvalidOperationException"类型异常

附加信息:跨线程操作无效:控制'levelsComboBox'从其创建的线程以外的线程访问.

我之前使用过.Invoke但只是设置属性,我怎么用它来读取combobox.Text?因为.Invoke返回void,我需要一个字符串.或者没有Invoke有另一种方法吗?

c# multithreading invoke thread-safety

28
推荐指数
3
解决办法
3万
查看次数

Dispatcher Invoke(...)vs BeginInvoke(...)混淆

我很困惑为什么我不能让这个测试计数器应用程序使用Count()方法在我的Dispatcher上使用"BeginInvoke"的2个(或更多个)同时运行的反文本框.

您可以通过Invoke替换BeginInvoke来解决问题.但这并不能解决我的困惑.

这是我正在谈论的示例代码:

public class CounterTextBox : TextBox
{
    private int _number;

    public void Start()
    {
        (new Action(Count)).BeginInvoke(null, null);
    }

    private void Count()
    {
        while (true)
        {
            if (_number++ > 10000) _number = 0;
            this.Dispatcher.BeginInvoke(new Action(UpdateText), System.Windows.Threading.DispatcherPriority.Background, null);    
        }
    }

    private void UpdateText()
    {
        this.Text = "" + _number;
    }
}
Run Code Online (Sandbox Code Playgroud)

c# multithreading invoke dispatcher begininvoke

28
推荐指数
1
解决办法
4万
查看次数

在处理控件时避免调用Invoke

我的工作线程中有以下代码(ImageListView下面是派生自的Control):

if (mImageListView != null && 
    mImageListView.IsHandleCreated &&
    !mImageListView.IsDisposed)
{
    if (mImageListView.InvokeRequired)
        mImageListView.Invoke(
            new RefreshDelegateInternal(mImageListView.RefreshInternal));
    else
        mImageListView.RefreshInternal();
}
Run Code Online (Sandbox Code Playgroud)

但是,我ObjectDisposedException有时会得到Invoke上面的方法.似乎控制可以在我检查IsDisposed和呼叫之间处理Invoke.我怎么能避免这种情况?

c# controls dispose invoke

27
推荐指数
2
解决办法
2万
查看次数

Reflection MethodInfo.Invoke()从方法内部捕获异常

我有一个MethodInfo.Invoke()通过反射执行函数的调用.调用包含在一个try/catch块中,但它仍然不会捕获我正在调用的函数抛出的异常.

我收到以下消息:

用户未处理异常.


为什么要MethodInfo.Invoke()阻止异常被捕获Invoke()
我该如何绕过它?

c# reflection methods exception invoke

27
推荐指数
2
解决办法
3万
查看次数

使用VB.NET通过字符串名称动态调用属性

我目前正在开发一个项目,其中一部分代码如下所示:

Select Case oReader.Name
    Case "NameExample1"
        Me.Elements.NameExample1.Value = oReader.ReadString
    ....
    Case "NameExampleN"
        Me.Elements.NameExampleN.Value = oReader.ReadString
    ....
End Select
Run Code Online (Sandbox Code Playgroud)

它会持续一段时间.代码显然很冗长,感觉可以改进.有没有办法动态调用VB.NET中的属性,这样就可以完成这样的事情:

Dim sReadString As String = oReader.ReadString
Me.Elements.InvokeProperty(sReadString).Value = sReadString
Run Code Online (Sandbox Code Playgroud)

vb.net reflection invoke

26
推荐指数
3
解决办法
3万
查看次数

Fabric vs Invoke

我一直在使用面料,最近被告知有关调用,所以我看了一下它的文档:

与Ruby的Rake工具和Invoke自己的前任Fabric 1.x一样,它提供了一个干净的高级API,用于运行shell命令和从tasks.py文件定义/组织任务函数.

除非我在调用时遗漏了某些东西和noob,但我发现fabric比invoke更强大,这让我觉得invoke不是替换fabric.我有点困惑.

我做了一个谷歌搜索'面料vs调用',什么都没有,所以在这里问.

invoke fabric

26
推荐指数
1
解决办法
4864
查看次数