none
launch multiple instances of same program

    Question

  • Hi,

    I want to pass e.g 3 filenames to my hello.exe  through cmd (hello.exe test.txt, test1.txt, test2.txt)and have it launch three PARALLEL times writing some text those filename locations.  OR pass one filename to it, it starts executing BUT returns control to the commandline so that I can pass another filename while the first instance is running in background? Do I have to use threads for this?

    How can I do this?

    Wednesday, August 24, 2011 9:28 AM

Answers

All replies

  • You can solve this in several ways, one is to use multithreading inside of your application. For instance you can look at the ThreadPool class and how to use it.

    http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx

    http://msdn.microsoft.com/en-us/library/3dasc8as(v=VS.100).aspx

     

    Sometimes you may want to really launch it as separate processes and could do something like this, in pseudocode

    If arguments > 1 then
      for each argument in arguments
        start new process with argument
      end
    else
      process argument
    end

    To start new process you use the Process class

    http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx

     

    This is a couple of examples and depending on what requirements you have there are more way to accomplish this.

    • Marked as answer by snif123 Wednesday, August 24, 2011 12:28 PM
    Wednesday, August 24, 2011 9:40 AM
  • Hi,

    yes, to do multiple things in parallel, you should use threads. So only one instance of your program is running when you start it with multiple arguments.

    The control is not given back to the commandline by default if you create a console application. Something like that can be possible. One quick solution that comes into my mind is to set the Output type to Windows Application. But be aware, that this will take all I/O channels away from you. You should no longer use the Console to write / read from it. And you have no way to handle the return code. So this behaviour is a bad thing in my eyes. And the Administrator can always start it without blocking his script if that is required. Start is the command that people would use inside a batch script or inside the cmd window.

    And the last thing: If another instance is started: It will run in parallel. So that should be ok. If you want that only one instance is running, then you need to implement some IPC (Inter Process Communication) to check for other running instances and to tell them what to do (e.g. opening a file).

    I hope this helped a little.

    With kind regards,

    Konrad

    Wednesday, August 24, 2011 9:40 AM
  • Thanks! I have chosen to go with threadpool. Does each thread in the pool have its own stack where it has its owm copies of variables in the method?  Can I call the same method for the threads in the pool without data corruption and contention or do I have to put in locks adn mutexes?
    Wednesday, August 24, 2011 12:31 PM
  • Thanks! I have chosen to go with threadpool. Does each thread in the pool have its own stack where it has its owm copies of variables in the method?  Can I call the same method for the threads in the pool without data corruption and contention or do I have to put in locks adn mutexes?

    Yes each thread has its own stack. All your stack-based variables will be threadsafe without any extra work on your behalf.

    If you pass separate data to your method for each thread, it will be fine.

    [EDIT] (As Louis points out, "separate data" means separate instances of class types; two references to the same instance don't count as separate data!)

    Wednesday, August 24, 2011 12:35 PM
  • Thanks! I have chosen to go with threadpool. Does each thread in the pool have its own stack where it has its owm copies of variables in the method?  Can I call the same method for the threads in the pool without data corruption and contention or do I have to put in locks adn mutexes?


    Each thread has its own stack. However, the thread-safety of a variable depends on its type.

    Reference-type variables don't hold the data, only a reference to the object in the heap. Copies of a reference-type variable will reference the same object, and therefore modifying the object in one thread can affect other threads.

    Reference-types are all types defined using the 'class' keyword.

    Value-type variables hold the data itself. Copies of a value-type variable are not linked to the original variable.

    Value-types are all types defined using the 'struct' or 'enum' keyword.

    Most simple types (int, double, ...) are value-types. The exception is string, which is a reference-type. But since string doesn't expose any method or property to modify it, it is thread-safe.

    Also, static variables are not stored in the stack. Unless declared as ThreadStatic, they are shared by all threads.

    Wednesday, August 24, 2011 12:42 PM
  • Hi, I using threadpools but I can't get any output to my textfiles. The textfiles are being created but nothing written to them. I can not see the correct number of threads equalling to the number of times I queue the userworkitems. I am trying to get each thread called to write to a different file so that each thread called has its own specific output. I have set debug points in the methods (in pseudocode below) e.g Do stuff like writing to a specified file (writer, myStream); but the program never reaches these points? I am thinking whether the threadpool is in contention causing it to terminate?

    class { 

    public string FileName { get; set ; }

    public string WriterName { get; set ; }

    public string FileStreamName { get; set ; }

    }

    

    class program

    { // all static class variables accessible globally here

     

    static void Main(string[] args)

    {

     

    }

     

     

     

    for (int i<5....){

     

    ThreadPool.QueueUserWorkItem(new WaitCallback (main_start), threadInfo); 

    }}

    static

    void main_start(object a)

    {

     ThreadInfo threadInfo = a as ThreadInfo;

    }

     

    FileStream myStream = new FileStream(filename, FileMode.Create, FileAccess .Write);

     StreamWriter writer = new StreamWriter(myStream, Encoding.Default);

    Do stuff like writing to a specified file (writer, myStream);

    ThreadInfo

    • Edited by snif123 Wednesday, August 24, 2011 2:09 PM re organise code
    Wednesday, August 24, 2011 1:43 PM
  • Are you are closing (or Dispose()ing) the streams?

    Run the program under the debugger to check if you are getting any exceptions in the threads.

    Wednesday, August 24, 2011 2:04 PM
  • HI, I used the program with one thread and it works perfect. Now I wanted to use threadpool to run multiple instances of the program. I have put together code that am running in VS, Problem: I expect it to produce 5 .txt files e.g message0.txt and write something to them. But it only creates 2 of them and doesnt even werite to it. Here it is.. does any one have an idea why it can't create the five txt files and write to them

    CODE[is complete and can be compiled]

     

    class

     

    {

     

    public string FileName { get; set

     

    class

     

    {

     

    static int

     

    static void Main(string

     

    string[] orders1 = { "dsds", "sdsd", "sdsd", "sds", "dsds"

     

    for (int

     

    string messages = "message" + i.ToString() + ".txt"

     

    ThreadInfo threadInfo = new ThreadInfo

     

    ThreadPool.QueueUserWorkItem(new WaitCallback

     

    static void main_start(object

     

    ThreadInfo threadInfo = a as ThreadInfo

     

    string

     

    FileStream myStream = new FileStream(filename, FileMode.Create, FileAccess

     

    StreamWriter writer = new StreamWriter(myStream, Encoding

     

    true

     

    public static void write_to_file(StreamWriter

     

    for (int

     

    i = 0; i < 5; i++)

     

    {

    writer1.Write(tester + i);

    }

    }

    }

     

    writer1)

     

    {

     

    ;

     

     

    }

     

    .Default);

     

    write_to_file(writer);

    writer.AutoFlush =

    .Write);

     

     

    filename = threadInfo.FileName;

     

     

    ;

     

     

    a)

     

    {

     

    (main_start), threadInfo);

     

    }

    }

     

    ();

     

    threadInfo.FileName = messages;

     

    ;

     

     

    i = 0; i < orders1.Length; i++)

     

    {

     

    };

     

     

    [] args)

     

    {

     

    tester = 5;

     

     

     

     

    Program

    ; }

     

     

    }

     

     

    ThreadInfo

    • Edited by snif123 Wednesday, August 24, 2011 2:30 PM edit
    Wednesday, August 24, 2011 2:28 PM
  • I have identified where the problem is coming from.. the main thread iterates through the main and terminates so fast before the threadpool threads have a chance to startoff..so am doing a readline() outside the main to halt it and force it to wait for the threadpool threads. This is inefficience, how can I make the threadpool threads to tell the main thread when they are done?
    Wednesday, August 24, 2011 3:40 PM
  • How can I make the threadpool threads to tell the main thread when they are done?

     

     

    Hi snif123,

     

    Welcome to MSDN Forum.

    Glad to hear that you made a big progress to investigate your question.

     

    There are 3 articles for you to check about some ideas, which talks about the issue you concern.


    Wait for threads in a ThreadPool object to complete

    http://www.codeproject.com/KB/threads/waitforthreads.aspx

     

    Wait for pooled threads to complete

    http://stackoverflow.com/questions/540078

     

    The Thread Pool and Asynchronous Methods

    http://www.yoda.arachsys.com/csharp/threads/threadpool.shtml

     

    Thanks all for your friendly support and help in MSDN Forum.

     


    Martin Xie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Friday, August 26, 2011 9:37 AM