Communication between 2 separate Winforms in separate projects

Aug 19, 2010 at 1:29 PM
Edited Aug 19, 2010 at 1:30 PM

Just wondering if you have any suggestions how to accomplish communication between 2 separate win forms in 2 separate projects.

Here is my clientForm:

**********************************

public partial class Form1 : Form
    {
        XcoAppSpace space = new XcoAppSpace("tcp.port=0");
        Microsoft.Ccr.Core.Port<string> port = null;
        public Form1()
        {
            InitializeComponent();

                    port = space.Connect<string>("ais-dt42:9001");
       }
        void OnNotification(int i)
        {
            Console.WriteLine("received notification {0} on thread {1}", i, Thread.CurrentThread.GetHashCode());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            port.Post("Hello, World!");
        }
    }

 

**********************************

 

Here is my ServerForm in a separate windows project:

public partial class Form1 : Form
    {
        private readonly Port<Port<int>> notifier;
        private readonly Port<int> notificationPort;

        XcoAppSpace space = new XcoAppSpace("tcp.port=9001");

        public Form1(/*XcoAppSpace space, Port<Port<int>> notifier*/)
        {
            InitializeComponent();
        }


        void OnNotification(int i)
        {
            Console.WriteLine("received notification {0} on thread {1}", i, Thread.CurrentThread.GetHashCode());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            space.Run<string>(msg => Console.WriteLine("Message received: " + msg));
        }
    }

 

When the button on the client form is clicked i would like to trigger an event on the server form.

Here are the steps i have followed:

 

1. I have statred the client form without debug.

2. Started server form in debug mode and get this exception:

 

System.Net.Sockets.SocketException was unhandled
  Message="Only one usage of each socket address (protocol/network address/port) is normally permitted"
  Source="System"
  ErrorCode=10048
  NativeErrorCode=10048
  StackTrace:
       at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
       at System.Net.Sockets.Socket.Bind(EndPoint localEP)
       at System.Net.Sockets.TcpListener.Start(Int32 backlog)
       at System.Net.Sockets.TcpListener.Start()
       at XcoAppSpaces.Transport.Sockets.TCPServer..ctor(Int32 port, String ipAddressStr, IXcoMessageReceiverService messageReceived, IXcoTransportService commService, IXcoLogger log) in d:\Development\Uni\XcoAppSpaces\source.implementation\XcoAppSpaces.Transport.Sockets\XcoAppSpaces.Transport.Sockets\TCPServer.cs:line 83
       at XcoAppSpaces.Transport.Sockets.XcoTCPTransportService.Start() in d:\Development\Uni\XcoAppSpaces\source.implementation\XcoAppSpaces.Transport.Sockets\XcoAppSpaces.Transport.Sockets\XcoTCPTransportService.cs:line 151
       at XcoAppSpaces.Core.Configuration.ServiceRegistry.StartServices() in d:\Development\Uni\XcoAppSpaces\source.implementation\XcoAppSpaces.Core\XcoAppSpaces.Core\internal\ServiceRegistry.cs:line 116
       at XcoAppSpaces.Core.XcoAppSpace..ctor(IXcoConfiguration config) in d:\Development\Uni\XcoAppSpaces\source.implementation\XcoAppSpaces.Core\XcoAppSpaces.Core\public\XcoAppSpace.cs:line 98
       at XcoAppSpaces.Core.XcoAppSpace..ctor(String configString) in d:\Development\Uni\XcoAppSpaces\source.implementation\XcoAppSpaces.Core\XcoAppSpaces.Core\public\XcoAppSpace.cs:line 78
       at WindowsFormsApplication1.Form1..ctor() in C:\Documents and Settings\...My Documents\XcoAppSpace Usage Demos\02_Advanced\WindowsFormsApplication1\Form1.cs:line 21
       at WindowsFormsApplication1.Program.Main() in C:\Documents and Settings\...My Documents\XcoAppSpace Usage Demos\02_Advanced\WindowsFormsApplication1\Program.cs:line 22
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

 

Just wondering if you have any suggestions.

Thanks

 

Coordinator
Aug 19, 2010 at 6:42 PM

There are several issues:

1. Don´t start an AppSpace in the form. Do it in the program.cs file like this:

using(var space = new XcoAppSpace(...))
{
    Application.Run(new MyForm(space));

2. If you receive a message in a form it´s received not on the form´s thread. You cannot display it in the form just like that. You need to switch the sync context first. That´s a simple multi threading truth of WinForms.

3. "Running" a worker should not be done in a button click event handler. Do it in the form´s ctor or Load event handler.

4. I´d even pass in the remote port as a ctor parameter. Keep the form as free of AppSpace details as possible.

The error msg you get might result from the tcp.port=0 being incidentally assigned the value 9001. Starting the client again should then get rid of the error. If you want to be 100% sure not to get this error assign the client its own port, eg. tcp.port=9002.

Aug 20, 2010 at 10:27 AM

Hi Ralf

Just wondering if you could provide a sample of 2 separate winforms projects which would be in 2 separte processes and are able to communicate.

Sorry for taking your time.

Thanks

Coordinator
Aug 20, 2010 at 12:41 PM

Unfortunately I can´t attach any files to this reply. But if you send me your email addr I´d be happy to provide you with a sample.

You can contact me via my CodePlex profile page.