. NET Framework 2.0 advanced programming learning notes (3): Processes and threads

Source: Internet
Author: User

1. Introduction
Here is a metaphor:
If the course is compared to a process, every student is a thread. They share the classroom, that is, the thread shares the memory space of the process. At every moment, only one student can ask the teacher a question. After the teacher answers the question, it is the next time. That is, the thread occupies the cpu in a time slice.
This example is easy to understand ?! Next, let's take a look at some basic concepts. It is easy to understand.
2. Process
A process is the basic unit of resource allocation and the basic unit of scheduling. From the programming point of view, you can also regard the process as a memory area that contains some resources.
For example, when a user opens a txt document, the system creates a process and allocates resources to it. Sometimes the process is very slow, because there are too many processes running on the CPU at this time, the process needs to wait for scheduling to truly run. If you open another txt file, you can find two processes in the Resource Manager. The memory size is different because the file size is different.
So I understand that as long as the application is opened, a process will be created.



In the. NET Framework, there is a class Process in the System. Diagnostics namespace, which can be used to create a new Process. The following code is used to create a notebook of hello.txt. After running the code, start the task manager and you can find that a notebook process is created.
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Diagnostics;
Using System. Threading;

Namespace ProcessPgr
{
Class Program
{
Static void Main (string [] args)
{
Process process = Process. Start ("notepad.exe", "Hello.txt"); // create a notebook for Hello.txt
Thread. Sleep (1000 );
Console. ReadLine ();
Process. Kill ();
}
}
}
 

3. threads
A thread is the smallest unit for executing operations in a process and also the basic unit for executing Processor Scheduling. In fact, a thread is a lightweight process. So why thread?
(1) Easy to schedule.
(2) Improve concurrency. Concurrency can be easily and effectively realized through threads. A process can create multiple threads to execute different parts of the same program.
(3) low overhead. Creating a thread is faster than creating a process and requires little overhead ..
(4) facilitate the full use of the multi-processor function. By creating a multi-threaded process (that is, a process can have two or more threads), each thread runs on one processor, so as to realize the concurrency of the application, make each processor fully run.
Relationship between processes and threads: www.2cto.com
(1) A thread can belong to only one process, and a process can have multiple threads, but at least one thread.
(2) resources are allocated to the process. All threads of the same process share all the resources of the process.
(3) The processor is allocated to the thread, that is, the thread that actually runs on the processing machine.
(4) The thread must collaborate and synchronize during execution. Message Communication is required between threads of different processes for synchronization.
For example, when we create a winform, we create a thread, assign the content to the text box, and click get data. In this case, an exception will be reported if you directly operate in the delegate method. Because the created thread cannot access the UI control, we use Invoke or BeginInvoke to call the data asynchronously when obtaining the data.




 

Solution:
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using System. Threading;

Namespace WindowsFormsApplication1
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

Thread thread;
Public delegate void MyInvoke ();

Private void btnSend_Click_1 (object sender, EventArgs e)
{
LabMessage. Text = "reading data. Please wait ";
Thread = new Thread (Send );
Thread. IsBackground = true;
Thread. Start ();
}

Protected void Send ()
{
MyInvoke invoke = new MyInvoke (Get );
This. BeginInvoke (invoke );
}

Protected void Get ()
{
Thread. Sleep (3000 );
TxtName. Text = "TerryChan ";
TxtNum. Text = "07 ";
TxtGrade. Text = "Software Engineering ";
}
}
}
 

Effect:



 

Conclusion: The knowledge points of processes and threads can be clearly explained without a single sentence or two. You just have a rough understanding of them and there is a long way to learn.

From ForEvErNoMe

Related Article

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.