Hi,
I'm a bit new to file handling in c# especially when it comes to having a file be accessed by more than one process or application.
My problem is that I am trying to write a string "continue" into a file in one application, then the application starts another process...that reads that string with every iteration of it's loop.
When I want this process to stop I write "stop" into the file from the original application and the called application is supposed to pick up on this and terminate it's loop.
Basically I want am asp.net web app to control a console application's execution.
The problem is that the second application does not read the file and get the proper value.
the calling function in the web app is like this:
the looping code in the console application is like this:Code:protected void startButt_Click(object sender, EventArgs e) { using (TextWriter tsw = new StreamWriter(@scriptPath + "running.txt")) { tsw.WriteLine("cont"); tsw.Close(); } Process SECExaminerNewNamesParser = new Process(); SECExaminerNewNamesParser.StartInfo.FileName = @scriptPath +"secexaminernewnamesparser.exe"; SECExaminerNewNamesParser.Start(); scriptStatus.Text = "process running"; isRunning = true; startScriptButt.Enabled = false; stopScriptButt.Enabled = true; }
the loop does not even pick up the initial value "cont" set by the calling app. I think it has created another file in memory because it cannot access the one on disk? I don't know.Code:while (reader.Read()) { tr = new StreamReader(@"running.txt"); //Do Stuff if ((runres = tr.ReadLine()) == "stop") { tr.Close(); Console.WriteLine("I should stop"); break; } else { Console.WriteLine(runres); } tr.Close(); }
If anyone can help I will be grateful.
I don't think this is the way it should be done... My proposal is:
1. Two .NET applications can communicate with each other over internet using object serialization. You serialize an object with some data state, send it over internet and deserialize it on the other end.
2. You could have some kind of web service which would listen to incomming commands and then control your windows application that iterates through stuff (starting it or stopping it).
About option 2: How would the web application communicate with the console application? Are you talking about windows messages?
BTW... the web app and console app are probably going to be in the same computer, maybe even the same directory...the only reason I am using a web app is because it can be accessed remotely... but the web server and source docs of the web app are gonna be on the same machine.
about object serialization: In .net for communicating between apps... can you tell me a b it more about that? What namespaces and classes are involved?
does the recipient and sender application have to be a Windows application or can they can asp.net and console etc?
Sender and recipient can be any .NET application. Serialization is very easy to use, so I don't think you're gonna have much problem with that. Just try to google some good example. But I'm surprised that you never heard of it, while it's quite common way of sending objects over internet from one app to another.
You could also create same server side listener that would listen on some port and then send it a command to stop iterating through UDP protocol which is also quite simple to implement.
But doing this with text files is really a bad solution.
Serialization in C#
Ok... both apps are on the same machine... so I should send the command to the console application using UDP? Isn't that an internet protocol?
but since I have a local webserver... yeah sure it can be done... I just never used an internet protocol to send a message to a console or windows app. I was researching windows messaging, which seems to be a slight hassle... but I will read up on this. thanks.
Could you kindly tell me what namespaces/references I might need to recieve a UDP message in a console app? thanks.
So basically you are suggestiong a two stage thing:
1- serialize object
2- send as UDP packet
otherside:
1- recieve UDP packet
2- convert to string or whatever
is that correct?
BTW..I had used serialization before... but normally just for dumping objects into a file... and not in .net. Am sort of new at this..first job. heh. Sorry for being a noob.
I just discussed this with our expert and here's what the best solution might be:
Create a HttpListener on server machine.
In your loop you can place same boolean variable flat set on true. While this flag is true, the loop will continue looping.
From client machine send a packet to server listening, to set the boolean flag on false and this should result in terminating the loop.
bool doWork = true;
while (doWork)
{
if (!doWork)
{
return;
}
//do some stuff
}
This might be quite easy solution and you are able to test it directly from your web browser (not needing to write a client's appliction).
http listener would be better than UDP?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks