我是线程世界的新手,并试图让我的应用程序与线程一起工作。这是我得到的:
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();
}
}
System.Windows.Threading.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority, System.Delegate, object)
的错误。它不编译。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/