我希望 SetLogText 下面的方法更新主 GUI,但它不起作用。没有错误。如何更新 GUI?
C# 代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.ServiceModel;
using System.ServiceModel.PeerResolvers;
namespace Client
{
public partial class Form1 : Form
{
IChatChannel participant;
DuplexChannelFactory<IChatChannel> factory;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{}
// Chat service contract
// Applying [PeerBehavior] attribute on the service contract enables retrieval of PeerNode from IClientChannel.
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples", CallbackContract = typeof(IChat))]
public interface IChat
{
[OperationContract(IsOneWay = true)]
void Join(string member);
[OperationContract(IsOneWay = true)]
void Chat(string member, string msg);
[OperationContract(IsOneWay = true)]
void Leave(string member);
}
public interface IChatChannel : IChat, IClientChannel
{}
public class ChatApp : IChat
{
// member id for this instance
string member;
public ChatApp(string member)
{
this.member = member;
}
//IChat implementation
public void Join(string member)
{
Console.WriteLine("[{0} joined]", member);
}
public void Chat(string member, string msg)
{
try
{
Form1 myForm = new Form1();
myForm.SetLogText("eureka");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
public void Leave(string member)
{
Console.WriteLine("[{0} left]", member);
}
}
// PeerNode event handlers
public void OnOnline(object sender, EventArgs e)
{
radListViewChats.Items.Add("Another user went Online");
}
public void OnOffline(object sender, EventArgs e)
{
radListViewChats.Items.Add("Another user went Offline");
}
private void radButtonConnect_Click(object sender, EventArgs e)
{
try
{
// Construct InstanceContext to handle messages on callback interface.
// An instance of ChatApp is created and passed to the InstanceContext.
InstanceContext instanceContext = new InstanceContext(new ChatApp(radTextBoxusername.Text));
// Create the participant with the given endpoint configuration
// Each participant opens a duplex channel to the mesh
// participant is an instance of the chat application that has opened a channel to the mesh
factory = new DuplexChannelFactory<IChatChannel>(instanceContext, "ChatEndpoint");
participant = factory.CreateChannel();
// Retrieve the PeerNode associated with the participant and register for online/offline events
// PeerNode represents a node in the mesh. Mesh is the named collection of connected nodes.
IOnlineStatus ostat = participant.GetProperty<IOnlineStatus>();
ostat.Online += new EventHandler(OnOnline);
ostat.Offline += new EventHandler(OnOffline);
try
{
participant.Open();
}
catch (CommunicationException)
{
radListViewChats.Items.Add("Could not find resolver. If you are using a custom resolver, please ensure");
radListViewChats.Items.Add("that the service is running before executing this sample. Refer to the readme");
radListViewChats.Items.Add("for more details.");
return;
}
radListViewChats.Items.Add("You are connected: ", radTextBoxusername.Text);
// Announce self to other participants
participant.Join(radTextBoxusername.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void radButtonLeave_Click(object sender, EventArgs e)
{
try
{
participant.Leave(radTextBoxusername.Text);
participant.Close();
factory.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void radButtonChat_Click(object sender, EventArgs e)
{
participant.Chat(radTextBoxusername.Text, radTextBoxText2Send.Text);
}
public void SetLogText(String text)
{
if (this.InvokeRequired)
{
this.Invoke(new EventHandler(delegate
{
textBox1.Text = text;
}));
}
else
textBox1.Text = text;
}
}
}