2

I've a static class called Service which starts a new thread to keep listening message from other process. In this class I manage a list of delegates which needs to be invoked when a message is received. Some of the methods of these delegates need to run in the main thread.

If I would create the threat in some form, for example, I could just do

this.Invoke(@delegate, new object[] { messageReceived });

But I can't do that because I'm in a static class. So I tried doing like this:

@delegate.Invoke(messageReceived);

But it doesn't work because it doesn't change the subprocess where the method is executed (it is executed from my created threat, not from the main one).

How can I do?

Thanks!

1
  • BeginInvoke, not Invoke. Oct 21, 2011 at 20:50

4 Answers 4

2

Modify your static class to accept a parameter.

Pass your this variable as a parameter to your static class.

1

Why you don't use the System.Threading.SynchronizationContext class? If you are going to move in the future in WPF is good to learn this. Synchronization context is the standard portable method to deal with interthreading messages. There is a version for each system... from System.Web, windows forms, wpf and also custom synchronization context.

public static class MyClass
{
    public static void MyFunction(MyObject messageReceived )
    {
        // Call this stuff from non-gui thread.

        SynchronizationContext syncContext = SynchronizationContext.Current;
        syncContext.Send(MyMethod, param);

        // You can also use SyncContext.Post if you don't need to wait for completion.
        syncContext.Post(MyMethod, param);
    }

    private static void MyMethod(object state)
    {
        MyObject myObject = state as MyObject;
        ... do my stuff into the gui thread.
    }

    // Using anonymous delegates you can also have a return value using Send.
    public static int MyFunctionWithReturnValue(MyObject parameter)
    {
        int result = 0;
        SynchronizationContext.Current.Post(delegate (object p)
        {
            result = parameter.DoSomething()
        }, null);

        return result;
    }
}
1

Your issue can be addressed through SynchronizationContext. In nutshell, capture the SynchronizationContext of UI thread and use Post method to post item on the GUI message loop. Needless to write more since you can find an excellent article Here

1
  • Thanks for the link! really better than any "full" explenation [yes, I know this is 11 years old...]
    – ephraim
    Aug 14, 2022 at 9:17
-1

Sorry I forgot it in my question. I also wanted to avoid using System.Windows.Forms classes in my static class.

The easiest way I found is to call Invoke method from the delegate object (as I tried before and it didn't work). And in the method of the delegate do this:

if (this.InvokeRequired)
    this.Invoke(MethodWhichDoesRealStuff, new object[] { message });
else
    MethodWhichDoesRealStuff(message);

I've also noticed that I actually didn't have to manage the delegate list in my class. An event does exactly that for me.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.