Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Problem with multiple processes accessing a file

  1. #1
    ibad is offline Learning Programmer
    Join Date
    Aug 2009
    Posts
    39
    Rep Power
    0

    Unhappy Problem with multiple processes accessing a file

    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:

    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 looping code in the console application is like this:

    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();
                }
    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.

    If anyone can help I will be grateful.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    FlashM is offline Learning Programmer
    Join Date
    Nov 2009
    Location
    Slovenia, Ljubljana (European Union)
    Posts
    90
    Rep Power
    9

    Re: Problem with multiple processes accessing a file

    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).

  4. #3
    ibad is offline Learning Programmer
    Join Date
    Aug 2009
    Posts
    39
    Rep Power
    0

    Re: Problem with multiple processes accessing a file

    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?

  5. #4
    ibad is offline Learning Programmer
    Join Date
    Aug 2009
    Posts
    39
    Rep Power
    0

    Re: Problem with multiple processes accessing a file

    does the recipient and sender application have to be a Windows application or can they can asp.net and console etc?

  6. #5
    FlashM is offline Learning Programmer
    Join Date
    Nov 2009
    Location
    Slovenia, Ljubljana (European Union)
    Posts
    90
    Rep Power
    9

    Re: Problem with multiple processes accessing a file

    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#

  7. #6
    ibad is offline Learning Programmer
    Join Date
    Aug 2009
    Posts
    39
    Rep Power
    0

    Re: Problem with multiple processes accessing a file

    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.

  8. #7
    ibad is offline Learning Programmer
    Join Date
    Aug 2009
    Posts
    39
    Rep Power
    0

    Re: Problem with multiple processes accessing a file

    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?

  9. #8
    ibad is offline Learning Programmer
    Join Date
    Aug 2009
    Posts
    39
    Rep Power
    0

    Re: Problem with multiple processes accessing a file

    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.

  10. #9
    FlashM is offline Learning Programmer
    Join Date
    Nov 2009
    Location
    Slovenia, Ljubljana (European Union)
    Posts
    90
    Rep Power
    9

    Re: Problem with multiple processes accessing a file

    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).

  11. #10
    ibad is offline Learning Programmer
    Join Date
    Aug 2009
    Posts
    39
    Rep Power
    0

    Re: Problem with multiple processes accessing a file

    http listener would be better than UDP?

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Saving and Accessing an image file to a Resource file: How?
    By oscarocasla in forum C# Programming
    Replies: 4
    Last Post: 02-08-2011, 09:30 AM
  2. Replies: 6
    Last Post: 09-21-2010, 03:11 AM
  3. Multiple processes or single process?
    By sharat in forum Linux Programming and Scripting
    Replies: 1
    Last Post: 01-12-2010, 09:01 AM
  4. Accessing data in a file?
    By Psynic in forum C# Programming
    Replies: 7
    Last Post: 10-01-2009, 06:15 AM
  5. Problem accessing another server
    By Sinipull in forum Java Help
    Replies: 2
    Last Post: 09-07-2009, 07:34 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts