Aug 21, 2019, 09:43 AM

News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!


A Sample Thread Application in MFC

Started by thiruvasagamani, Sep 01, 2008, 09:16 PM

previous topic - next topic
Go Down

thiruvasagamani

A Sample Thread Application

   Thread is the smallest executable unit of code within an application. It is the path of execution of a process. An executing Instance of an application is a process while any specific job involving an application is the task and a process can contain many threads.

   The Windows environment has the ability to simultaneously process multiple processes and threads to enable multitasking. An application can be either Single or Multithreaded. A multi-threaded application is executed by windows by allocating resources in a very organized way.

   A Single Threaded will usually have only one thread i.e. The application?s Main thread and a Multi Threaded will have one Main and several additional threads. E.g. Auto save , Background Printing. In WIN 32 environment, small chunks of CPU time are allocated for every competing thread. The small size of time slice is approx. 20 milliseconds which makes it appear that it is processed simultaneously.

  There are two kinds of threads which can be created in MFC namely Worker Threads & User Interface Threads.
Worker Threads:
  These are Useful chunks of program code for a specific purpose. They communicate with global variables and synchronization events.
User Interface Threads:

   The main thread of the application, typically for user interface. The CWinApp itself does the work of the main thread, but the application thread should be derived from the CWinThread class. They can have a message loop.

   Threads can be suspended, resumed, put to sleep or even terminated.
Thread termination:

   Worker thread execute "return" or calls AfxEndThread(). Another thread can call "::TerminateThread" but this may lead to resource allocation leaks Better to signal thread or set global variable that you want it to terminate itself. For User Interface threads, we can send a WM_QUIT message.
Thread Implementation:

   Threads can be created within an application through the AfxBeginThread().This function creates worker threads for many small chunks of code to be executed simultaneously.
Creating a Thread :

a) Associate a message handler for a menu , toolbar , or any control update message.

b) In the command handler use the AfxBeginThread()

c) Pass the address of the thread function i.e. The function where the unit of code to be executed is written.

d) Pass the HWND of the parent application window through the GetSafeHwnd().

e) Pass the priority of the thread as THREAD_PRIORITY_NORMAL etc.

f) Control shifts on meeting this statement to the portion of code within the function.
Example :

Code: [Select]
CWinthread *pthread1;
pthread1=AfxBeginThread(Thread1,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
UINT Thread1(LPVOID lp)
{
      Messagebox(?Thread In Execution?);
}

Thread Status Tracking:

   The status of a thread in execution can be tracked through the pointer which it returns to the CWinThread class.

Example :
CWinThread *thr;
DWORD status;
Thr=AfxBeginThread(Thread1,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
::GetExitCodeThread(thr->m_hThread,&status);
If ( status == STILL_ACTIVE )
{
    Messagebox ( ?Thread In Execution? );
}


Controlling Threads

The priority of a thread can be changed to boost or reduce performance to prefer other important job

Code: [Select]
through SetThreadPriority()
Example :
BOOL flag;
flag=thr-> SetThreadPriority (THREAD_PRIORITY_ABOVE_NORMAL);


Thiruvasakamani Karnan


nandagopal



This code works great

Thanks for the code !!!!!!




spurza

win32_PhysicalMedia , SerialNumber returns NULL varient

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:
Verification:
Please leave this box empty:

Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

shortcuts: alt+s submit/post or alt+p preview
IT Acumens Web Designing Chennai | GinGly :: SMS Backup | CineBuzz :: Latest Cinema News | My Kids Diary :: Kids magical moment
Copyright 2005 - 2019 :: IT Acumens :: All Rights Reserved.
ITAcumens Forum with 2 lakhs post running for 14 years - Powered by IT Acumens Dedicated Server