0

私はスレッドの世界に不慣れで、アプリケーションをスレッドで動作させようとしています。これが私が得たものです:

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++ DLL も使用する C# WPF プロジェクトです。最後に、ファイルのヘッダーは次のとおりです。

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;
4

1 に答える 1

1

Actionデリゲートを1つとしてキャストするだけで新しいものを作成するべきではありません。

非同期:

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

});

同期:

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

});

または、コントロールまたはウィンドウを使用していない場合:

Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, (Action)delegate()
{

});
于 2012-12-05T05:13:37.757 に答える