471,357 Members | 1,087 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,357 software developers and data experts.

Send message to .Net process

I am searching for a solution of sending messages from not .Net process into
a .Net process written in C#.
I have gone already thru sockets solution and have porblems with it. I am
not a C# coder dont kill :)
I am thinking about something like this:
A remote non .Net process has a window with a name known to .Net program.
Can .Net program somehow process that window using window's title and
extracting a text from it?
What other solutions might be?

Thanks
Aug 15 '08 #1
5 9485
You are looking for IPC:

http://msdn.microsoft.com/en-us/libr...74(VS.85).aspx

Most likely Named Pipes will do what you want.
Aug 15 '08 #2
On Fri, 15 Aug 2008 13:35:25 -0700, Markgoldin <ma*************@yahoo.com>
wrote:
I am searching for a solution of sending messages from not .Net process
into
a .Net process written in C#.
I have gone already thru sockets solution and have porblems with it. I am
not a C# coder dont kill :)
I am thinking about something like this:
A remote non .Net process has a window with a name known to .Net program.
Can .Net program somehow process that window using window's title and
extracting a text from it?
Using unmanaged code, there are ways to identify windows for other
processes and manipulate them. But that would involve information flowing
_from_ the .NET process to the non-.NET process, which is the opposite of
what you've asked.

Using sockets would in fact be a reasonable solution, as would named pipes
as James suggests (my understanding is that .NET 3.5 includes support for
named pipes now). But you're not specific about why you "have problems
with it". It's impossible to provide any advice along those lines without
details about what problems you specifically had.

Pete
Aug 15 '08 #3
On Aug 15, 11:35*pm, "Markgoldin" <markgoldin_2...@yahoo.comwrote:
I am searching for a solution of sending messages from not .Net process into
a .Net process written in C#.
I have gone already thru sockets solution and have porblems with it. I am
not a C# coder dont kill :)
I am thinking about something like this:
A remote non .Net process has a window with a name known to .Net program.
Can .Net program somehow process that window using window's title and
extracting a text from it?
What other solutions might be?

Thanks
If you want to understand the hole communication between process and
thread read the book of Jeffrey Richter
You have the import one of this functions depends on your need
PostMessage Or SendMessage

Bast Regards
Ohav Baumgarten
Aug 15 '08 #4
Here is my complete project:
main class:

using System;
using System.Net.Sockets;
using Lightstreamer.DotNet.Server;
using System.Net;
using System.Threading;
public class DataAdapterLauncher
{
public static TcpListener tcplJM4;
public static TcpListener tcplJM5;
public static TcpListener tcplJM6;
public static TcpListener tcplMD1;
public static TcpListener tcplMD2;
public static TcpListener tcplJM1;
public static TcpListener tcplJM2;
public static TcpListener tcplG2;
public static TcpListener tcpl1;
public static void Main(string[] args)
{
string host = "localhost";
bool go = true;
int reqrepPort = 6661;
int notifPort = 6662;
try
{
DataProviderServer server = new DataProviderServer();
server.Adapter = new SocketToLightStreamer();

MetadataProviderServer serverMeta = new
MetadataProviderServer();
serverMeta.Adapter = new MessageAwareLiteralBasedProvider();

TcpClient reqrepSocket = new TcpClient(host, reqrepPort);
server.RequestStream = reqrepSocket.GetStream();
server.ReplyStream = reqrepSocket.GetStream();

TcpClient notifSocket = new TcpClient(host, notifPort);
server.NotifyStream = notifSocket.GetStream();

server.Start();
System.Console.WriteLine("Remote Adapter connected to
Lightstreamer Server.");
System.Console.WriteLine("Ready to publish data...");

//start listeners that send data to the front-end. One for each
scanning area
IPEndPoint ipeJM4 = new IPEndPoint(IPAddress.Parse("0.0.0.0"),
63335);//listen on all local addresses
tcplJM4 = new TcpListener(ipeJM4);
tcplJM4.Start(); // block application until data and connection
is requested
IPEndPoint ipeJM5 = new IPEndPoint(IPAddress.Parse("0.0.0.0"),
63336);//listen on all local addresses
tcplJM5 = new TcpListener(ipeJM5);
tcplJM5.Start(); // block application until data and connection
is requested
IPEndPoint ipeJM6 = new IPEndPoint(IPAddress.Parse("0.0.0.0"),
63337);//listen on all local addresses
tcplJM6 = new TcpListener(ipeJM6);
tcplJM6.Start(); // block application until data and connection
is requested
IPEndPoint ipeMD1 = new IPEndPoint(IPAddress.Parse("0.0.0.0"),
63339);//listen on all local addresses
tcplMD1 = new TcpListener(ipeMD1);
tcplMD1.Start(); // block application until data and connection
is requested
IPEndPoint ipeJM1 = new IPEndPoint(IPAddress.Parse("0.0.0.0"),
63338);//listen on all local addresses
tcplJM1 = new TcpListener(ipeJM1);
tcplJM1.Start(); // block application until data and connection
is requested
IPEndPoint ipeJM2 = new IPEndPoint(IPAddress.Parse("0.0.0.0"),
63340);//listen on all local addresses
tcplJM2 = new TcpListener(ipeJM2);
tcplJM2.Start(); // block application until data and connection
is requested
IPEndPoint ipeG2 = new IPEndPoint(IPAddress.Parse("0.0.0.0"),
63342);//listen on all local addresses
tcplG2 = new TcpListener(ipeG2);
tcplG2.Start(); // block application until data and connection
is requested
IPEndPoint ipeMD2 = new IPEndPoint(IPAddress.Parse("0.0.0.0"),
63341);//listen on all local addresses
tcplMD2 = new TcpListener(ipeMD2);
tcplMD2.Start(); // block application until data and connection
is requested
catch (Exception e)
{
System.Console.WriteLine("Could not connect to Lightstreamer
Server.");
System.Console.WriteLine("Make sure Lightstreamer Server is
started before this Adapter.");
System.Console.WriteLine(e);
}
}
}

and another class:

using System;
using System.Collections;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;

using Lightstreamer.Interfaces.Data;

public class SocketToLightStreamer : IDataProvider
{
private IItemEventListener _listener;
private volatile bool goJM4;
private volatile bool goJM5;
private volatile bool goJM6;
private volatile bool goMD1;
private volatile bool goJM1;
private volatile bool goJM2;
private volatile bool goMD2;
private volatile bool goG2;

public void Init(IDictionary parameters, string configFile)
{
}

public bool IsSnapshotAvailable(string itemName)
{
return false;
}

public void SetListener(IItemEventListener eventListener)
{
_listener = eventListener;
}

public void Subscribe(string itemName)
{
if (itemName.Equals("floorupdate"))
{
new Thread(delegate() { Run(goJM4,
DataAdapterLauncher.tcplJM4); }).Start();
new Thread(delegate() { Run(goJM5,
DataAdapterLauncher.tcplJM5); }).Start();
new Thread(delegate() { Run(goJM6,
DataAdapterLauncher.tcplJM6); }).Start();
new Thread(delegate() { Run(goJM1,
DataAdapterLauncher.tcplJM1); }).Start();
new Thread(delegate() { Run(goJM2,
DataAdapterLauncher.tcplJM2); }).Start();
new Thread(delegate() { Run(goMD1,
DataAdapterLauncher.tcplMD1); }).Start();
new Thread(delegate() { Run(goMD2,
DataAdapterLauncher.tcplMD2); }).Start();
new Thread(delegate() { Run(goG2,
DataAdapterLauncher.tcplG2); }).Start();
}
}

public void Unsubscribe(string itemName)
{
if (itemName.Equals("floorupdate"))
{
goJM4 = false;
goJM5 = false;
goJM6 = false;
goMD1 = false;
goMD2 = false;
goJM1 = false;
goJM2 = false;
goG2 = false;
}
}

public void Run(Boolean go, TcpListener tcpl)
{
go = true;
// Buffer for reading data
Byte[] bytes = new Byte[1024];
String data;
Encoding ascii = Encoding.ASCII;
TcpClient tcpc = null;
while (go)
{
if (tcpc == null)
tcpc = tcpl.AcceptTcpClient(); //accept connection
data = "";
NetworkStream stream = tcpc.GetStream();
// Loop to receive all the data sent by the client.
int i = 0;
i = stream.Read(bytes, 0, bytes.Length);
while (i 0 && !data.EndsWith("\r"))
{
// Translate data bytes to a ASCII string.
data += ascii.GetString(bytes, 0, i);
i = stream.Read(bytes, 0, bytes.Length);
}
// Process the data sent by the client.
if (data != "")
{
if (data.IndexOf("close") != -1)
{
System.Console.WriteLine("Closing connection ...");
go = false;
tcpl.Stop();
tcpl.Start();
new Thread(delegate() { Run(go, tcpl); }).Start();
return;
}
else
{
System.Console.WriteLine(data);
IDictionary eventData = new Hashtable();
eventData["scan"] = data;
_listener.Update("floorupdate", eventData, false);
}
}
}
}
}

Three problems with.Net side:
1. Sometimes it crashes
2. when a client is disconnected CPU on a box that run this console
application goes to 100%.
I am trying to handle that with this:
....
if (data.IndexOf("close") != -1)
{
System.Console.WriteLine("Closing connection ...");
go = false;
tcpl.Stop();
tcpl.Start();
new Thread(delegate() { Run(go, tcpl); }).Start();
return;
}

3. I am missing some readings from sockets. I think that is due to this:
.....
else
{
System.Console.WriteLine(data);
IDictionary eventData = new Hashtable();
eventData["scan"] = data;
_listener.Update("floorupdate", eventData, false);
}

I am thinking to move that into a separate thread. Do you think it will
help?

Just want to remind I am not a c# coder. So dont kill if you see something
funny :)
Aug 19 '08 #5
On Tue, 19 Aug 2008 10:46:04 -0700, Markgoldin <ma*************@yahoo.com>
wrote:
Here is my complete project:
Surely whatever problems exist in your code, they could be demonstrated
with just one TcpListener, rather than nine. Or, in other words, the code
you posted isn't a "concise-but-complete" code sample, which is the usual
standard for posting code when you want someone else to find problems in
it.

So, keeping in mind that I haven't put a lot of effort into examining each
and every line of code you posted...
[...]
Three problems with.Net side:
1. Sometimes it crashes
That's too vague a problem description for anyone to help. What does
"crash" mean? Does the OS actually terminate the process? Is there an
error or exception? If so, what is it?
2. when a client is disconnected CPU on a box that run this console
application goes to 100%.
Ahh...this seems familiar. I'm guessing this is the network code you had
questions about previously?

As far as the #2 question goes, you have some irregularities in your
SocketToLightStreamer.Run() method, but the one that is causing the CPU
consumption is that after the stream is closed (Read() returns 0), you
should set your TcpClient variable back to null.

It's not a great way to control flow within the loop, but that should at
least fix that issue, without having to resort to:
I am trying to handle that with this:
...
if (data.IndexOf("close") != -1)
{
System.Console.WriteLine("Closing connection ...");
go = false;
tcpl.Stop();
tcpl.Start();
new Thread(delegate() { Run(go, tcpl); }).Start();
return;
}
As before, I still fail to see the point in having a variable (named "go")
to control the loop. Especially as you set it to "true" as the first
thing in the Run() method and the only other place it ever gets set to
"false" is shortly before a "return" statement, what's the point?

Also, stopping and starting the TcpListener is pointless, and could in
fact cause issues because of the way that the OS deals with TCP listening
sockets (you may find that the port you're trying to use is unavailable
for a short time after having closed the listening socket, which is what
TcpListener does implicitly).

But the main question is: are you sure that a closed connection will have
written the text "close" to the stream before it is closed? Assuming that
it does, then I don't see why the above wouldn't work, even if it is a bit
clumsy. So it seems that the most likely explanation for your high CPU
utilization issue is that the stream doesn't have the text "close" in it
before it's actually closed.
3. I am missing some readings from sockets. I think that is due to this:
....
else
{
System.Console.WriteLine(data);
IDictionary eventData = new Hashtable();
eventData["scan"] = data;
_listener.Update("floorupdate", eventData, false);
}

I am thinking to move that into a separate thread. Do you think it will
help?
Honestly, the last thing I think that would help is _more_ threads. :)

But regardless, the code above doesn't appear to me to have anything to do
with the socket i/o at all. As near as I can tell, it's just how you're
passing the most-recently-received data to your IItemEventListener
implementation. So I don't think any modification of that section of
code, whether putting it in a new thread or something else, would help
whatever the "missing some readings from sockets" problem is.

All that said, there's really no real diagnostics that anyone can do with
respect to your code without a concise-but-complete code sample. For
network code, that means code for both ends of the TCP connection, along
with some predefined data demonstrating how the network connection is used.
Just want to remind I am not a c# coder. So dont kill if you see
something
funny :)
Just to be clear: we may harass, laugh at, or otherwise bother someone who
posts funny code (though normally we just try to help), but there's a
strict no-kill policy around here. As far as I know, no one has ever
actually lost their life because of something they posted here. :)

Pete
Aug 19 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Kurda Yon | last post: by
5 posts views Thread by Go Perl | last post: by
3 posts views Thread by SenthilVel | last post: by
3 posts views Thread by =?Utf-8?B?SHVnaA==?= | last post: by
6 posts views Thread by ShieldsJared | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.