MFC multi-Thread ticket ticketing system

Source: Internet
Author: User

MFC Thread-Ticketing system demo

In the University operating system course, processes and threads are a frequent topic, and ticketing systems are a classic example of threading, which involves creating threads, thread synchronization, and so on.

Needless to say, the following is the use of MFC to implement the ticketing system.

1. Create a new MFC Dialog application (project name is thread) and press add control.

2. Add the following code to the "thread Dlg.h" file

struct, passed to the thread parameter struct ctrls{cprogressctrl* progress; cstatic* text;};/ /Declare the function executed in the thread uint Buy (LPVOID lpparameter);

3. Add the following code to the "Thread Dlg.cpp" file

Critical_section CS;  Defines a key code snippet object int total; Total votes ctrls ctrl1, Ctrl2, Ctrl3, Ctrl4; UINT Buy (LPVoid lpparameter) {while (1) {entercriticalsection (&cs), if (Total > 0) {ctrls* c = (ctrls*) lpparameter; C->progress->stepit (); int num = C->progress->getpos (); CString S;s.format ("%d", num); C->text->setwindowtext (s); total--; Sleep (50); LeaveCriticalSection (&AMP;CS);} Elsebreak;} return 0;} button click event void Cmydlg::onbuy () {//Todo:add your control notification handler code herem_buy. EnableWindow (FALSE); CString str; GetDlgItem (idc_total)->getwindowtext (str), total = Atoi (str), if (total <= 0 | | Total > 30000) MessageBox ("Please Don't mess!" "); InitializeCriticalSection (&cs); M_pro1. SetRange32 (0, total); M_pro1. SetStep (1); ctrl1.progress = &m_pro1;ctrl1.text = &m_num1;m_pro2. SetRange32 (0, total); M_pro2. SetStep (1); ctrl2.progress = &m_pro2;ctrl2.text = &m_num2;m_pro3. SetRange32 (0, total); M_pro3. SetStep (1); ctrl3.progress = &m_pro3;ctrl3.text = &m_num3;m_pro4. SetRange32(0, total); M_pro4. SetStep (1); ctrl4.progress = &m_pro4;ctrl4.text = &m_num4; AfxBeginThread (Buy, &AMP;CTRL1); AfxBeginThread (Buy, &AMP;CTRL2); AfxBeginThread (Buy, &AMP;CTRL3); AfxBeginThread (Buy, &AMP;CTRL4);}

--**** the functions used to create worker threads are as follows:

cwinthread* AFXAPI AfxBeginThread (afx_threadproc  pfnthreadproc,lpvoid  pparam,int  npriority,uint  Nstacksize,dword  dwcreateflags,lpsecurity_attributes lpsecurityattrs);


The first parameter, Pfunthreadproc, represents the entry function address of the thread, and the function should be the same as: UINT
Mycontrollingfunction (LPVOID pparam);
The second parameter, Pparam, represents the arguments passed to the thread.
The third parameter, npriority, indicates the priority of the thread, the default priority level Thread_priority_normal,
If 0, the thread that created the thread is the same. This parameter has the following levels, which are sorted from highest to lowest:
Thread_priority_time_critical
Thread_priority_highest
Thread_priority_above_normal
Thread_priority_normal
Thread_priority_below_normal
Thread_priority_lowest
Hread_priority_idle
The fourth parameter, Nstacksize, represents the stack size of the thread, and if 0 indicates the system default value is used.
The fifth parameter, Dwcreateflags, indicates the token when the thread was created, if the parameter is create_suspended
Represents a suspended state after a thread is created, or 0, which indicates that the thread runs immediately as soon as it is established.
The sixth parameter, Lpsecurityattrs, represents a security attribute, which is generally NULL.
the successful return value of the function call is a pointer to the CWinThread class, which enables control of the thread. In
The thread will be terminated when the thread function returns and the void AfxEndThread (UINT nexitcode) can be used inside the threads;
Ends the thread, nexitcode the exit code.
Once the worker thread starts, it starts executing the control function, and the thread ends, and the control function ends. Line Program
The prototype of the system function is as follows:
UINT mycontrollingfunction (LPVOID pparam);

3. Compile--link--run. The results are as follows:

* * Full source download: http://download.csdn.net/detail/u010233287/8202207


MFC multi-Thread ticket ticketing system

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.