wcf P2P更新主桂

时间:2013-03-07 17:57:16

标签: c# wcf

我希望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;

        }

    }
}

1 个答案:

答案 0 :(得分:2)

您正在Form1的新实例上调用SetLogText。您需要在现有实例上调用它才能对该实例产生任何影响。如果您将代码分成多个文件来构建项目,这可能会有所帮助。可能很明显,Chat对Form1一无所知,需要在程序中的某个时间点给出一个实例。

示例:

public class ChatApp : IChat
{
   string member;
   Form1 form;

   public ChatApp(string member, Form1 form)
   {
        this.member = member;
        this.form = form;
   }

   public void Join(string member)
   {
       Console.WriteLine("[{0} joined]", member);
   }


   public void Chat(string member, string msg)
   {
       try
       {                      
            form.SetLogText("eureka");
       }
       catch (Exception ex)
       {    
           MessageBox.Show(ex.Message.ToString());    
       }
   }

   public void Leave(string member)
   {
       Console.WriteLine("[{0} left]", member);
   }
}