Simple Way to Establish Interaction Between Two Windows Applications by a Channel of a Connection Database

Introduction

While playing with a Windows application I attempted to provide communication between two applications in a simple way. I also would like to have the communication of the two Microsoft Windows applications be very simple and I found the following simple logic to do theat.

Concept behind the approach

A Microsoft Windows application runs on separate host processes when it runs in an IDE and as an exe with an executable build. But interaction between the two separate processes was not possible. But by this approach we can provide interaction between two Windows application processes by a bridge of a temp storage.

Here we have two Windows applications named “Sender” and “Receiver”. We will provide for interaction between the “Sender” and “Receiver” in a very simple way using a text file .txt as a bridge.

The sender updates the text in that temp .txt file whenever we enter text. Similarly the receiver monitors the .txt text file for any updates.

C# Code snippet for sender

namespace sender

{

    public partial class Form1 : Form

    {

        string path = string.Empty;

        public Form1()

        {

            InitializeComponent();

 

           path= System.IO.Path.GetTempPath(); // Geting Temp Path

 

            if (!File.Exists(path + "ConnectionDataBase.txt")) // Condition searching for the patch availablity

            {

                File.Create(path + "ConnectionDataBase.txt"); // Creating the file if the file not found

            }

        }

 

        private void textBox1_TextChanged(object sender, EventArgs e)

        {

                using (System.IO.StreamWriter file = new System.IO.StreamWriter(path + "ConnectionDataBase.txt")) // Open a text file to update the content

                {

                    file.Write(this.textBox1.Text); // Write the content

                }

        }

    }

}

C# Code snippet for receiver

namespace receiver

{

    public partial class Form1 : Form

    {

        string path = string.Empty;

        public Form1()

        {

            InitializeComponent();

 

            t.Tick += t_Tick;

            t.Interval = 100;

            t.Start(); // Start timer for keep on getting update from connection database

 

            path = System.IO.Path.GetTempPath(); // Geting Temp Path

 

            if (!File.Exists(path + "ConnectionDataBase.txt")) // Condition searching for the patch availablity

            {

                File.Create(path + "ConnectionDataBase.txt"); // Creating the file if the file not found

            }

        }

 

        void t_Tick(object sender, EventArgs e)

        {

                this.textBox1.Text = string.Empty;

                using (StreamReader sr = File.OpenText(path + "ConnectionDataBase.txt")) // Open the file to get updates

                {

                    var line = "";

 

                    while ((line = sr.ReadLine()) != null)

                    {

                        this.textBox1.Text += line; // Update the text in textbox from connection database

                    }

                }

        }

 

        Timer t = new Timer();

 

    }

}

Interaction Between Two Visual Studio Application 

The preceding image shows the data flow between the “Sender” application and “Receiver” application. Here the connection database acts as a bridge and provides the interaction between the two applications.

Conclusion

Hence the preceding method is a simple way to establish an interaction between two Microsoft Windows applications using a file as the connection database that acts as a channel between the applications.