views:

96

answers:

3

The code:

[1]

private delegate void ThreadStatusCallback(ReceiveMessageAction action, 
      Dictionary<int, List<string>> message);

[2]

Dictionary<int, List<string>> messagesForNotification = 
      new Dictionary<int, List<string>>();

[3]

Invoke(new ThreadStatusCallback(ReceivesMessagesStatus), 
     ReceiveMessageAction.Notification , messagesForNotification );

[4]

private void ReceivesMessagesStatus(ReceiveMessageAction action, object value)
{
    ...
}

How can I send the two variable of type ReceiveMessageAction respectively Dictionary<int, List<string>> to the ReceivesMessagesStatus method.

Thanks.


I have a function that is executed in a thread. Here I create an object of type Dictionary <int, List <string>> and using a delegation I whant to use this object in my form.

A part of the code is above: [1] Decraration of the delegated [2] The object [3] The Call Of the delegate [3] the function Where the object Need To Be send

+1  A: 

Have you tried casting value as a Dictionary? e.g.

private void ReceiveMessagesStatus(ReceieveMessageAction action, object value)
{
    var myDictionary = (Dictionary<int, List<string>>)value;
    ...
}
James
@Emanuel: Why not be? Maybe `value as (Dictionary<int, List<string>>)` ?
abatishchev
@Emanuel: Dictionary can be cast as an object...
James
+1  A: 

Solution:

[1]

Invoke(new ThreadStatusCallback(ReceivesMessagesStatus), new object[] { ReceiveMessageAction.Notification, messagesForNotification } );

[2]

private void ReceivesMessagesStatus(ReceiveMessageAction action, Dictionary<int, List<string>> value)
{
    ...
}
Emanuel
+2  A: 

You can do it type-safe without casting by using a lambda:

  Invoke(new MethodInvoker(
    () => ReceivesMessagesStatus(ReceiveMessageAction.Notification, messagesForNotification)
  ));
Hans Passant