Controlling multi-threading, opening, pausing, continuing, terminating (instance)

Source: Internet
Author: User
Tags thread stop
Controlling multi-threading, opening, pausing, continuing, terminating (instances) We were illiterate before we heard the multi-threaded word, thought very advanced, difficult to engage in. Yesterday looked through the MSDN, found that, in fact, is not so difficult, the key lies in a comprehensive understanding, perhaps the use of more APIs, and slowly will understand some ...

I summed up a few easy-to-understand and shared together.

Let's not talk about how to use the parameters in the threading process, let's start with a simple one, and we'll talk about how to use the parameters of the threading process to implement the interaction in the next article.

AfxBeginThread Creating Threads
 
Function Prototypes:

cwinthread* AfxBeginThread (Afx_threadproc Pfnthreadproc,
LPVOID Pparam,
int npriority = Thread_priority_normal,
UINT nstacksize = 0,
DWORD dwcreateflags = 0,
Lpsecurity_attributes lpsecurityattrs = NULL
);

 

The function is used to create the thread; The return value type is: cwinthread*, a thread object that points to the new thread
Parameters:

Pfnthreadproc
The entry function of the thread, the declaration must be as follows: UINT mythreadfunction (LPVOID pparam);
  
Pparam:
Pass in the arguments to the thread, note that it is of type: LPVOID, so we can pass a struct into the thread.
  
Npriority:
The priority of the thread, typically set to 0. Let it have a common priority with the primary thread.
  
Nstacksize:
Specifies the size of the stack for the newly created thread. If it is 0, the newly created thread has a stack of the same size as the main path
  
Dwcreateflags:
Specifies a flag for how the line threads after the thread is created. You can specify two values:
  
Create_suspended:
After the thread is created, it is suspended until it is called: ResumeThread
  
0:
Starts running when the thread is created.
  
Lpsecurityattrs:
Points to a security_attributes structure that is used to flag the security of the newly created thread. If NULL,
Then the newly created thread will have the same security as the main path.
You can call AfxEndThread within a thread if you want to end the threads within the inline path.
Two ways to end a thread
When you are using threads to print some graphics in the background. Sometimes after you print a part, you want to be able to stop, so how do you let the thread stop it.
The polygon will explain to you in detail two ways to end a thread
1: This is the simplest way, that is, to let the thread function perform, at which point the thread ends normally. It returns a value, generally 0 is the successful end,
Of course, you can define your own values that you think are appropriate to execute successfully on behalf of the thread. Calling AfxEndThread in the thread will end the threads directly, and this line
All resources of the process will be recycled.
2: If you want to end thread A with a thread B, then you need to pass the information in these two threads.
Whether it's a worker thread or an interface thread, if you want to get it to end after the thread ends, then you can call:
:: GetExitCodeThread function

 

----------------------------------------------------------------------------------------------------------- -------------

SuspendThread Suspending (pausing) a thread

function Prototypes:

DWORD SuspendThread (
HANDLE hthread//Handle of the specified thread
);

 

The return value of this function: DWORD type, if this function succeeds, the return value is the thread's front stop count, otherwise, it (represents)-1.

 

----------------------------------------------------------------------------------------------------------- --------------------

resumethread continue thread execution

function Prototypes:

DWORD ResumeThread (
HANDLE hthread//Handle of the specified thread
);

 

The return value of this function: DWORD type, if this function succeeds, the return value is the thread's front stop count, otherwise, it (represents)-1.

 

----------------------------------------------------------------------------------------------------------- -----------------------

WaitForSingleObject terminating a thread

DWORD WaitForSingleObject (
HANDLE Hhandle,
DWORD dwmilliseconds
);
  
  
Parameters
Hhandle
is a handle to an event
Dwmilliseconds
In the specified pause interval, in milliseconds. This function will return if the interval passes, even if the object's state is nonsignaled. If Dwmilliseconds is zero, the function tests the state of the object and returns. If Dwmilliseconds is infinite, the pause interval for this function never passes.

--------------------------------------------------------------------------------------------------------------- -------------------

Example

Create a dialog box project,

Create an edit box with the associated variable: M_xianshi; used to start counting, from 11 straight back + +

declare a global variable first:

static int III; Used to Count

 

declare an object of type cwinthread* in the Dlg class to control the entire thread:

cwinthread* H_kongzhi;

//Note that it can be seen that the return value of the thread function is the same type as the creation, so before using the API function, we must understand the return value type of the function

 

Start creating threads, how to create, we see the parameters of the AfxBeginThread function can be seen, the first parameter is the process function of a thread, the declaration must be as follows: UINT mythreadfunction (LPVOID pparam);

UINT ThreadProc (LPVOID pparam)
{
while (true)
{
Sleep (500); Sleeps 500 milliseconds for synchronizing with Wm_timer objects
iii++; Count variable self-increment
}

return 0;
}

 

in order to let us know multi-threading, we create a Wm_timer time object, which is used to cycle the display count to the edit box;

UpdateData (TRUE);
M_xianshi = III; Assign a count variable to the edit box
UpdateData (FALSE); Refresh the value of the interface

 

 

then add a button titled: Start and add the following code to the Start button:

 

SetTimer (0,500,null); Activates the Wm_timer object, running every 500 milliseconds
H_kongzhi = AfxBeginThread (ThreadProc, NULL);

//Create a thread and assign the return value to H_kongzhi, where we do not have to pass parameters, there are some features and so on, so can be ignored directly, set to null

  

 

 

Add a button that is titled; pause and add the following code to the button:

 

H_kongzhi->suspendthread ();

//Call the suspend thread function, because the control object is a pointer type, you only need to point to that member in the object.

 

 

add a button titled: Continue execution and add the following code to the button:

H_kongzhi->resumethread (); Call continue execution thread function

 

add a button titled: Terminate the thread and add the following code to the button:

Wait for thread to end
WaitForSingleObject (H_kongzhi->m_hthread, INFINITE);



go from: http://www.cnblogs.com/yushao/archive/2010/02/08/1666069.html

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.