Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having two EXE's say EXE1 and EXE2..

I am writing a textfile using EXE1.
As soon as EXE1 writes a file it should send an acknowledgement to EXE2.
So that EXE2 can read that file.

How to do this using TCP or any other IPC.

The main Thing is that EXE2 should not use most of the CPU.
Because I tried the below in EXE2
C#
while(true)
{

    if(File.Exists(Path)
        {
        }
}



It almost use 99 Percentage of CPU Usage.
How to get rid of this.
Can anyone help me with code.
Posted
Updated 19-Aug-14 21:46pm
v3
Comments
gggustafson 20-Aug-14 1:25am    
The problem does not have anything to do with TCP. It has to do with sharing data.

In exe1, create a named AutoResetEvent setting the initial state to not signalled.
In exe2, create the same named AutoResetEvent setting the initial state to not signalled and WaitOne ( Timeout.Infinite ).
When exe1 completes writing the file, Set ( ) the event. That will allow exe2 to execute.
KUMAR619 20-Aug-14 1:30am    
Can you answer with a code Sir.

You should use WCF (Windows Communication Foundation) based on NetTcpBinding (net.tcp or other binding) and for doing this you should:

1.You EXE2 should be the WCF server (that expose a WCF service to be used by others applications);

2.You EXE! should be the WCF client.

3.Here are some articles that could be used by you as WCF starting points:
Windows Communication Foundation Basics[^]
WCF for Beginners[^]
Windows Communication Foundation QuickStart - Multiple Binding VS2010[^]
 
Share this answer
 
Comments
KUMAR619 20-Aug-14 1:32am    
I am using .Net Framework 2.0 So I cant use WCF.
Any alternate idea sir?
Raul Iloc 20-Aug-14 1:42am    
In this case you have the next options:
1)Use .NET Remoting:
http://www.codeproject.com/Articles/14791/NET-Remoting-with-an-easy-example
2)Use .NET Named pipes:
http://www.codeproject.com/Articles/7176/Inter-Process-Communication-in-NET-Using-Named-Pip
KUMAR619 20-Aug-14 1:43am    
Thanks sir
Raul Iloc 20-Aug-14 1:45am    
Welcome!
To reduce CPU usage you need to put your thread to sleep.
Or else it will block by constantly loop in your while loop.

like this.
C#
//Add "using System.Threading;"
while(true)
{
    if(File.Exists(Path))
        {
        }
Thread.Sleep(1);
}


For TCP communication you need a server and a client (or multiple clients).
Take a look atTcpListener[^] for server and TcpClient[^] for clients. Good luck.
 
Share this answer
 
Comments
KUMAR619 20-Aug-14 1:34am    
I don't even want TCP I want to read data only when file is existing.
When we do While(true)
it often loops so taking much process.
I make other EXE slow that's why I want the EXE2 to work only when file is existing that reduces the process
Per Söderlund 20-Aug-14 2:36am    
Splitting logic into 2 EXE will make your head spin.
It´s hard work (debugging) and useless if you do it to reduce CPU.
Make sure your thread is not blocking by using Thread.Sleep as i showed, that will be enough to decrease CPU usage.
Also take a look at multithreading if you need to do parallell processing.
Multiple threads are way better then multiple applications (up to a certain point).
KUMAR619 20-Aug-14 2:38am    
Thanks Sir
Per Söderlund 20-Aug-14 2:41am    
Your welcome.
By the way, you should not write a question how to use TCP perfectly, if you dont want TCP :).
KUMAR619 20-Aug-14 3:03am    
Can you suggest a question for this existing or can you Edit the question depending upon your Answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900