我是线程世界的新手,并尝试让我的应用程序与线程一起使用。这是我得到的:
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;
答案 0 :(得分:1)
您不应该创建一个新的Action
只是将您的代理转换为一个:
异步:
Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)delegate()
{
});
同步:
Dispatcher.Invoke(DispatcherPriority.Background, (Action)delegate()
{
});
或者如果你不在控制或窗口上:
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, (Action)delegate()
{
});