0

I'm new to the threading world and trying to make my application works with threads. Here is what I got:

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();
    }
}

This thing does compile when I comment all the Dispatcher.Invoke(). If I don't, I get an error about System.Windows.Threading.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority, System.Delegate, object) and it does not compile.

Any ideas?

I'm using VS2010 on Windows 7 Pro x64. This is a C# WPF project which also make use of some C++ DLLs also compiled in the same project. Finally, here is the header of the file:

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;
abatishchev
  • 98,240
  • 88
  • 296
  • 433
dan
  • 3,439
  • 17
  • 54
  • 82
  • Are you using vanilla WPF, or Silverlight or XAML for Windows 8 Store Apps? If Silverlight/Windows 8 Store Apps, then I am fairly certain that the Invoke() method doesn't exist, and you need to call the async variant. Please tag your question with the appropriate technology. – Rob Dec 05 '12 at 05:14
  • I'm using VS2010, this is for Windows 7 actually. – dan Dec 05 '12 at 05:15
  • @abatishchev thanks for the edition, however it's still not working when trying to call Dispatcher.Invoke (object reference required). – dan Dec 05 '12 at 06:31

1 Answers1

1

You should not be creating a new Action just casting your delegate as one:

Async:

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

});

Sync:

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

});

Or if your not on a control or window:

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

});
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
  • Even those without any content gave me the same errors. Am I missing a library or something? I added to my original post what I'm using. – dan Dec 05 '12 at 05:17
  • this is in a my Window1.xaml.cs file, this is a WPF project. – dan Dec 05 '12 at 06:08
  • This is the error: Dispatcher.Invoke - Error: object reference required – dan Dec 05 '12 at 06:32
  • Seems to work with Application.Current.Dispatcher.Invoke(). I got another problem however and that's what might cause the Dispatcher.Invoke() to not work. My .cs file isn't able to match the controls into my .xaml.cs file, I will take a look at that. – dan Dec 05 '12 at 06:38