0

我正在使用LocalMessageReceiverandLocalMessageSender类在两个 Silverlight 应用程序之间进行通信(一个在浏览器中,另一个在浏览器外)。我需要messageReceiver.MessageReceived在一个新线程中执行。这是我的基本代码:

public MainPage()
{
    LocalMessageReceiver messageReceiver = new LocalMessageReceiver("Msg", ReceiverNameScope.Global, LocalMessageReceiver.AnyDomain);
    messageReceiver.MessageReceived += MessageReceiver_MessageReceived; //I want to execute this in a new Thread.
    messageReceiver.DisableSenderTrustCheck = true;

    try
    {
        messageReceiver.Listen();
    }
    catch (ListenFailedException)
    {
        //to do
    }
}

private void MessageReceiver_MessageReceived(object sender, MessageReceivedEventArgs e)
{
    string command = e.Message;

    switch (command)
    {
        case "SCAN":
            e.Response = Scan(e.Message); //I need to send a response
            break;
        default:
            e.Response = "NULL";
            break;
    }
}
4

1 回答 1

0

这可能是你想要的...

messageReceiver.MessageReceived += (s, e) => 
    Task.Factory.StartNew(() => MessageReceiver_MessageReceived(s, e));

或使用线程...new Thread(() => MessageReceiver_MessageReceived(s, e)).Start();

于 2012-04-13T22:35:26.350 回答