-4

I am working on windows forms. I have two applications, one is parent and another one is child. In my main application i have a grid with bunch of records. user needs to select one record and click on button, i need to send that selected record's data to child application as arguments like,

Process.Start("path to child exe","selected record's data");

and after that i will get that sent data to child application as,

Environment.GetEnvironmentVariables()[0];

now when the processing with sent data from parent has been completed, child application needs to close automatically. after that in parent application the selected row of gridview need to shift one row forward and send that selected row information to child application and the process needs to iterate until it traverse all records in my gridview in main application.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
Karthik Bammidi
  • 1,851
  • 9
  • 31
  • 65

3 Answers3

2

Well, first of all you'll notice that;

Process.Start()

Returns a reference to System.Diagnostics.Process object which has Exited event. Then you can hook a handler that will proceed to another row and re-start the process again:

var process = Process.Start("path to child exe","selected record's data");
process.Exited += (sender, args) => 
{
   // do your stuff
}

Then, I advise to check other options to do this:

  • Is this a .net managed application? Can you just reference the .exe assembly in your project and call the necessary method via provided interface?
  • If this is an unmanaged windows application you still can call its methods. Use DllImport for that:

    [DllImport(LibraryName, EntryPoint = FunctionName)]

    private static extern int Function(string param);

Then you will be able to call Function from your code. Google that for details.

Also, take a look at: How to call a Managed DLL File in C#?

Community
  • 1
  • 1
Artur Udod
  • 4,465
  • 1
  • 29
  • 58
0

Assuming you don't need to pass data back from the child application to master:

In your Master app create a recursive method to start Child app with parameters. In my example listBox1 contains a list of data items:

private void button1_Click(object sender, EventArgs e)
{
    listBox1.SelectedIndex = 0;
    startChild((string)listBox1.SelectedItem);
}

void startChild(string data)
{
    ProcessStartInfo psi = new ProcessStartInfo("child.exe", data);
    Process p = Process.Start(psi);
    p.WaitForExit();
    if (p.HasExited)
    {
        if ((listBox1.SelectedIndex + 1) < listBox1.Items.Count)
        {
            listBox1.SelectedIndex++;
            startChild((string)listBox1.SelectedItem);
        }
    }
}

Then in your Child app pick up the parameters and process them:

public Child()
{
    InitializeComponent();
    var data = Environment.GetCommandLineArgs()[1];
    ProcessDataAndExit(data);
}
StaWho
  • 2,488
  • 17
  • 24
-1

Passing data via command line arguments can get messy. It would be more robust to use sockets.

This might help: An Introduction to Socket Programming in .NET

Echilon
  • 10,064
  • 33
  • 131
  • 217