c# - 编译委托(delegate)和调度程序时出错

我是线程世界的新手,并试图让我的应用程序与线程一起工作。这是我得到的:

public static void ThreadProc()
{
    Thread.Sleep(500);
    MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("SuperMMFofDoom", MemoryMappedFileRights.ReadWrite);
    MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor(0, sizeof(double)*3 + sizeof(int) *2);
    Image imgS = new Image();
    ImageTrigger myMessage;
    Mutex imgMutex = new Mutex(false, "imgMutex");

    while (threadRunning)
    {
        imgMutex.WaitOne();

        accessor.Read(0, out myMessage);

        // [...]

        Dispatcher.Invoke(DispatcherPriority.Normal,
            new Action(delegate()
                {
                    // [...]
                }),
            new object[] { imgS, myMessage.performance }
           );

        imgMutex.ReleaseMutex();
    }
}

当我评论所有 Dispatcher.Invoke() 时,这个东西会编译。如果我不这样做,我会收到关于 System.Windows.Threading.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority, System.Delegate, object) 的错误。它不编译。

有任何想法吗?

我在 Windows 7 Pro x64 上使用 VS2010。这是一个 C# WPF 项目,它还利用了一些在同一项目中编译的 C++ DLL。最后,这是文件的标题:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Threading;
using System.IO.MemoryMappedFiles;
using Common;

最佳答案

你不应该创建一个新的 Action只是将您的委托(delegate)作为一个:

异步:

Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)delegate()
{

});

同步:
Dispatcher.Invoke(DispatcherPriority.Background, (Action)delegate()
{

});

或者,如果您不在控件或窗口上:
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, (Action)delegate()
{

});

https://stackoverflow.com/questions/13716708/

相关文章:

python - scipy给出了 undefined symbol : clapack_sgesv

gcc - 如何在 MinGW 中将错误消息控制设置为详细?

Objective-C - 避免编译错误?

opengl - 使用UBO编译片段着色器时发生OpenGLSL错误

xcode - Xcode无法识别C++语法

wpf - WPF,将XAML文件移动到另一个项目

compiler-errors - 带有C++/CLI的VS2012中的错误C2039错误

git - 移动git项目导致许多Blob和树错误

compiler-errors - 免费的基本编译器错误23 : File not found, l

android - BadTokenException无法添加窗口(ProgressDialog)