Remember the process by which the main process in the WinForm program opens a subprocess and passes parameters (interprocess pass-through parameters)

Source: Internet
Author: User

Goal: To pass parameters between WinForm programs. So that the child process can make the appropriate processing.

A wrong way.

Main program of Parent process:

1             New ProcessStartInfo (); 2             " ProcessChild.exe " ; 3             Psi. Arguments = Txtargs.text; 4             Process.Start (PSI); // The main problem here

Main program of child process:

1 txtargs.text = process.getcurrentprocess (). startinfo.arguments;

Results

The reason for the error is that it is assumed that the ProcessStartInfo of the parent process's instance of this class arguments passed into the child process. In fact Process.Start () returns an object of process type, the data is stored in the returned object, and is not passed across the process.

Two of the right ways

The first type:

The incoming data is received from main (string []args). The main method to modify the subprocess here is as follows:

1         Static void Main (string  []args)2        {3             Application.enablevisualstyles (); 4             Application.setcompatibletextrenderingdefault (false); 5             Application.Run (new  frmchild ()); 6         }

Because the default is no parameter. To save the string in args, it is worth mentioning that args always has at least one element, and the second method is easy to see.

The second type: Use the Environment class method.

1             string [] args = Environment.getcommandlineargs (); 2            // Txtargs.text = process.getcurrentprocess (). startinfo.arguments; 3             Txtargs.text = args[0"\ r \ n";

When not started from the parent process, the result is as follows: the number of elements in args is 1.

When started from the parent process, the parameter passed by the parent becomes the second element of args: The code in the child process

1             string[] args =Environment.getcommandlineargs ();2            //Txtargs.text = process.getcurrentprocess (). startinfo.arguments;3Txtargs.text + = args[0] +"\ r \ n";4             if(args. Length >1)5             {6Txtargs.text + = args[1];7}

Find the cause of the address: http://stackoverflow.com/questions/10682212/how-to-pass-argument-to-a-process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.