WINFORM launcher (parallel EXE) design scheme

Source: Internet
Author: User

First, the story line

When there is a complete project scenario, and suddenly the need to increase the functionality to support parallel requirements, usually consider whether the modules in the project support parallelism, such as the use of Singleton mode, but also the module division, the single module will need to modify and so on.

At this point, the initiator can be implemented, the advantage is that the completion of the project only need to increase the relevant functions of the boot identity, other functions can be used directly, greatly reducing the development cycle of functional parallelism.

Second, the starter

The launcher that I need in my project is also a WinForm program that contains multiple buttons corresponding to parallel multiple parallel programs, and a copy of the program is opened after the button is clicked.

Questions to consider:

1 each copy is allowed to execute only once, such as support four copies, then there can be only one copy (of course, this is my requirement, if you do not consider the limit), you need to do a copy check of the scheme.

2 Item static variables within a single replica are shared issues. If shared, this scheme will expire. Verified to be independent.

3 How to start a new EXE program.

Third, the initiator implementation

1 The program can be more open, but the initiator must only run one, at this time using a mutex implementation:

var mutex = new Mutex (true, key, out result);

-Key is a unique identifier

-result, if False, indicates that your program has started if only your program has set the key.

2 launcher, open the copy program, using the following code:

Process myprocess = new process (); ProcessStartInfo startinfo = new Processstart (path, args); myprocess. StartInfo = startinfo;myprocess. Startinfo.useshellexecute = false;myprocess. Start ();

Args is a variable of type string, and if you are familiar with the following code, the first element of args should be the current args variable.

Path is the open program path, note that it must be an absolute path.

<summary>///The main entry point of the application. </summary>[stathread]static void Main (string[] args) {     //...}

3 The replica check scheme in the initiator, because a single copy can only be opened once, the initiator needs to continuously follow up whether the replica has been terminated, and allows the user to restart the open. The scenario I'm using is implemented through the INI configuration file:

Filetooler.iniwritevalue ("STARTUP", $ "chn{index}", "1", config_paht);

Filetooler.iniwritevalue is the INI method I encapsulated, the specific INI operation refers to the following:

[DllImport ("kernel32")]private static extern long WritePrivateProfileString (string section    , string key    , String Val    , string filePath); [DllImport ("kernel32")]private static extern int getprivateprofilestring (string section    , String key    , string def    , StringBuilder retVal    , int size    , string filePath);

The key here is the second argument $ "Chn{index}", the index corresponds to the copy number, and "1" means open.

Similarly, when the corresponding copy program is closed, the following code should be called to update the INI file.

Filetooler.iniwritevalue ("STARTUPA20", $ "chn{index}", "0", config_paht);

In addition to setting the INI file when starting a copy and ending the copy, you need to add a timer for continuous checking:

private void Timer1_Tick (object sender, EventArgs e) {for    (int i = 0; i < 4; i++)    {        var chk = syscan.file.f Iletooler.inireadvalue ("STARTUPA20", $ "chn{index}", config_paht) = = "1";        Btns[i]. Text = $ "Channel {index}" + (chk? "Started": "");        Btns[i]. Enabled =!chk;    }}

Here you can see that the corresponding channel is set by the button control, and the Enable and text descriptions remind the user whether it can be reopened.

The use of the timer is no longer here to repeat, do not understand, please visit:

Https://msdn.microsoft.com/zh-cn/library/system.windows.forms.timer.aspx

Iv. Other

In this framework, if you want to support partial data sharing, the simplest solution is to put the shared part in a database and not share it separately.

V. Summary

The main technical points involved in the programme include:

1 single process check based on mutex

2 INI file read/write

3 process mode to start a new program

4 Use of the timer

This starter program determines that you can leave a message if you have any questions.

WINFORM launcher (parallel EXE) design scheme

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.