C # Forms WinForm processes, threads

Source: Internet
Author: User
Tags reflection

First, the process

A process is a run-time activity of a program with independent functionality about a data collection.

It can apply and own system resources, is a dynamic concept, is an active entity.

Process class , which is used to manipulate processes.

Namespaces:using System.Diagnostics;

Process.Start ("Calc"); Open Calculator
Process.Start ("MSPaint"); Open Paint
Process.Start ("IExplore", "http://www.baidu.com"); //Open browser and specify address

(i) through a process, open the specified file:

1. Create a Process object
Process P = new process ();

2. Create a StartInfo object, which is the path that specifies the drive letter .
ProcessStartInfo psi = new ProcessStartInfo (@ "C:\user\ ...);

3. Process designation and Commencement
P.startinfo = PSI; Specify the path
P.start (); Start process

(b) The File selection box allows the user to select the program they want to open and open it:

        private void Button1_Click (object sender, EventArgs e)        {            //This is the type of select File            openfiledialog1.filter = "Application |*.exe ";            Displays the dialog box and determines if the user has selected the file            if (openfiledialog1.showdialog () = = DialogResult.OK)            {                //Fetch file path,                string path = Openfiledialog1.filename;                Create a new process                , p = new process ();                Startup information for the manufacturing process                ProcessStartInfo psf = new ProcessStartInfo (path);                Sets the execution information for the process                p.startinfo = psf;                Start Process                p.start ();            }                 }

Example: Logout

This path is the absolute path to the EXE program you are calling.

string path = Application.startuppath;

Gets the file name path of its own EXE or DLL

string s = System.Reflection.Assembly.GetExecutingAssembly (). Manifestmodule.fullyqualifiedname;

        private void Button2_Click (object sender, EventArgs e)        {            //Open the program            //Take the path of the program file            //string Path = Application.startuppath;            String path = System.Reflection.Assembly.GetExecutingAssembly (). Manifestmodule.fullyqualifiedname;                        Create a process            , p = new process ();            Create a process start information            ProcessStartInfo PS = new ProcessStartInfo (path);            Set Process start information            p.startinfo = PS;            Start Process            p.start ();            Close Program this            . Close ();        }

Example: Window implementation mouse drag

        private int x;        private int y;        private void Picturebox1_mousedown (object sender, MouseEventArgs e)        {            //mouse click on xy coordinate            x = e.x;            y = e.y;        }        private void Picturebox1_mousemove (object sender, MouseEventArgs e)        {            //To determine if the mouse button is pressed            (E.button = = System.Windows.Forms.MouseButtons.Left)            {                //margin = current distance + moving distance this                . left = this. left+ (e.x-x);                This. Top = this. Top + (e.y-y);            }        }    


Second, the thread

A thread , sometimes called a lightweight process (lightweight PROCESS,LWP), is the smallest unit of program execution flow.

The difference: The process has its own independent resources, and threads share resources.

Thread class , namespace:using System.Threading;

The program defaults to only one main thread, and if you do complex processing, there will be suspended animation, because a thread can only do one thing at a time.

Multi-Threading role : At the same time to do more things, save time, the background to run programs, improve the efficiency of the program, will not make the main interface unresponsive situation.

To create a thread:

Open the new thread to execute which function
  

  Thread th = new Thread (the method in which the thread is to be manipulated, the method name does not contain parentheses);

Flag this thread is ready to execute at any time, CPU determines execution time
  

Th. Start ();

The threads are divided into two types:

foreground thread -only all foreground threads are closed to complete the program shutdown.

background thread-as long as all foreground threads are finished, the background thread ends automatically.

the difference between the two: the application must run out of all foreground threads before it can exit;

For a background thread, the application can exit without considering whether it has finished running, and all background threads will automatically end when the application exits.

Set th thread to be a background thread

  

  Th. IsBackground = true;

Control is created by the main thread and the new threads want to access the main thread resource, which is not allowed to be accessed by default across threads.

To cancel cross-thread access restrictions
In the interface Load event

  Control.checkforillegalcrossthreadcalls = false;

In the FormClosing event:

Determines whether the new thread is null and, if NULL, automatically shuts down
 if (th = null)
{
Ends the thread immediately, but can no longer be restarted after the end.
 Th. Abort ();
}

To execute a function with parameters:

If a thread executes a method that requires parameters , then this parameter must be of type Object!

Thread th = new Thread (Test); Parameter names do not need parentheses
Th. IsBackground = true;
Th.         Start ("123"); The parameters required to pass in the test function here

In this function, a strong turn is required:
  private void Test (object s)
  {
    string ss = (string) s;
    ......
  }

C # Forms WinForm processes, threads

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.