<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7606927205095410759</id><updated>2011-11-27T16:52:22.997-08:00</updated><category term='C++'/><category term='MFC'/><category term='VC++'/><category term='Templates'/><title type='text'>programminggallery.com</title><subtitle type='html'>Shairing my knowledge and my experiece in VC++ , MFC,Win32,com,DCOM,C#ASP.net and also all the thigs that i am related to.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://programminggallery.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7606927205095410759/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://programminggallery.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Shine Joseph</name><uri>http://www.blogger.com/profile/07634054201076817071</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>8</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7606927205095410759.post-7610240391884480074</id><published>2007-10-05T23:43:00.000-07:00</published><updated>2007-11-04T20:46:28.277-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Templates'/><category scheme='http://www.blogger.com/atom/ns#' term='MFC'/><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>Collection classes  in MFC</title><content type='html'>MFC provides two kinds of collection classes&lt;br /&gt;&lt;br /&gt;1. Template based collection classes&lt;br /&gt;2. Non template collection classes&lt;br /&gt;&lt;br /&gt;Collection classes are charectarised by the "shape" and type of it's elements. Shape means the way objects are organiced and stored in the collection. MFC provides three basic collection shapes. Lists , arrays and maps . You can select which one is using based on your particular requirement.&lt;br /&gt;&lt;br /&gt;The following are the Description of the above items&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;1. Lists&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;The list class provides an ordered, nonindexed list of elements, implemented as a doubly linked list. A list has a "head" and a "tail," and adding or removing elements from the head or tail, or inserting or deleting elements in the middle, is very fast.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;2. Arrays&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;The array class provides a dynamically sized, ordered, and integer-indexed array of objects.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;3. Maps&lt;/strong&gt;&lt;br /&gt;a map is also known as a dictionaryA map is a collection that associates a key object with a value object.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The Search In the Maps are Fast and they are indexed by the key. While the Search inside the&lt;br /&gt;Lists and arrays are slower .For inserting an element in list and maps it is fast and for array it is slower&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;List Classes and Opreation on The lists&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;CList is the main class that we can use to implement the list .&lt;br /&gt;&lt;br /&gt;See the following egsample for how to implement a list for Insertion search and retreaving a string data&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#3333ff;"&gt;void &lt;/span&gt;main()&lt;br /&gt;{&lt;br /&gt;CList &lt;cstring,cstring&amp;amp;&gt;strlist;&lt;br /&gt;// inserting the element to the list&lt;br /&gt;&lt;span style="color:#3333ff;"&gt;for&lt;/span&gt; (&lt;span style="color:#3333ff;"&gt;int&lt;/span&gt; i = 0; i&lt;=20; i++) { CString strelement; strelement.Format("Element at %d",i); strlist.AddTail(strelement); } // the elements in the list POSITION pos = strlist.GetHeadPosition(); &lt;span style="color:#3333ff;"&gt;while&lt;/span&gt;(pos != NULL)&lt;br /&gt;{&lt;br /&gt;cout strlist.GetAt(pos) endl;&lt;br /&gt;strlist.GetNext(pos);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// Search the elementcout&lt;&lt;"Search for a string element" endl endl;CString strtosearch = "Element at 5";&lt;br /&gt;pos = strlist.Find(strtosearch,NULL);&lt;br /&gt;cout strlist.GetAt(pos) endl;&lt;br /&gt;&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7606927205095410759-7610240391884480074?l=programminggallery.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programminggallery.blogspot.com/feeds/7610240391884480074/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7606927205095410759&amp;postID=7610240391884480074' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7606927205095410759/posts/default/7610240391884480074'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7606927205095410759/posts/default/7610240391884480074'/><link rel='alternate' type='text/html' href='http://programminggallery.blogspot.com/2007/10/collection-classes-available-in-c-using.html' title='Collection classes  in MFC'/><author><name>Shine Joseph</name><uri>http://www.blogger.com/profile/07634054201076817071</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7606927205095410759.post-7856872709316466488</id><published>2007-09-27T00:54:00.000-07:00</published><updated>2007-10-05T23:41:59.242-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='VC++'/><title type='text'>How to Create modal and Modeless Dialog using MFC and what is the difference</title><content type='html'>&lt;strong&gt;Difference between Modal and Modless Dialog &lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;In some discussion discussion boards i saw lot of questions regarding what is the difference between the modal dialog and modeless dialog boxes. as to my opinion modal dialog if once invoked it will not allow to access the parent window. but modeless dialog is will allow the user to access the parent window.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;How to create a modal dialog&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;A modal dialog can easily created using the following steps.&lt;br /&gt;&lt;br /&gt;Start your visual studio Editor 6&lt;br /&gt;&lt;br /&gt;step 1&lt;br /&gt;&lt;br /&gt;Select File-&gt;New&lt;br /&gt;in the project tab select MFC appWizard(exe)&lt;br /&gt;Give "DialogModalDemo" in the project name text box&lt;br /&gt;Give OK&lt;br /&gt;&lt;br /&gt;Step 2&lt;br /&gt;&lt;br /&gt;Select single document&lt;br /&gt;uncheck document/architecture support&lt;br /&gt;click next&lt;br /&gt;&lt;br /&gt;Step 3&lt;br /&gt;&lt;br /&gt;Click Finish&lt;br /&gt;&lt;br /&gt;Now you created the farameword for implementing or Modal and Modeless dialog&lt;br /&gt;&lt;br /&gt;step 4&lt;br /&gt;&lt;br /&gt;Now go to the Resource view and select the menu item on there.&lt;br /&gt;&lt;br /&gt;Add a New Menu item as Dialog Demo to the main menu and also add tw2o sub menu item as&lt;br /&gt;"Modal Dialog" and "Modeless Dialog"&lt;br /&gt;&lt;br /&gt;step 5&lt;br /&gt;&lt;br /&gt;Now press the Ctrl+R and add a new dialog Templete to the project&lt;br /&gt;&lt;br /&gt;Rename the template id from IDD_DIALOG1 to IDD_MODAL_DIALOG&lt;br /&gt;&lt;br /&gt;Now go to to the CMainFrame class and press Ctrl + W&lt;br /&gt;add a handler for the ID_DIALOGDEMO_MODALDIALOG item&lt;br /&gt;&lt;br /&gt;Now go to the CFrameWnd eader implementation file and add #include "ModalDialog.h" on there&lt;br /&gt;&lt;br /&gt;now go to the handler CMainFrame::OnDialogdemoModaldialog()  that we added and add the code on there&lt;br /&gt;&lt;br /&gt;as following looks like&lt;br /&gt;&lt;br /&gt; ModalDialog dlg;&lt;br /&gt; dlg.DoModal();&lt;br /&gt;&lt;br /&gt;Now run the application and Go to the Modal Dialog Menu and click it. Check weather it is working file. If not Please let me know.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;How to Create Modless Dialog &lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Now i will explain how you can create a modeless dialog.&lt;br /&gt;&lt;br /&gt;Go to the CmainFrame class and add a handler for the ID_DIALOGDEMO_MODELESSDIALOG menu item to the CMainFrame class. Next go to the&lt;br /&gt;CMainFrame window class definition header file and decalre a variable of ModalDialog in the CMainFrame class as like following&lt;br /&gt;&lt;br /&gt;ModalDialog modelessdialog;&lt;br /&gt;&lt;br /&gt;Now go to the handler void CMainFrame::OnDialogdemoModelessdialog()  that we have created befor and add the following code on there.&lt;br /&gt;&lt;br /&gt;modelessdialog.Create(IDD_MODAL_DIALOG,this);&lt;br /&gt; modelessdialog.ShowWindow(SW_SHOW);&lt;br /&gt;&lt;br /&gt;Now run the application and check weather it is working for you.&lt;br /&gt;&lt;br /&gt;Expecting your comments on thi article you can contact me at my mail id &lt;a href="mailto:shinejos@gmail.com"&gt;shinejos@gmail.com&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7606927205095410759-7856872709316466488?l=programminggallery.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programminggallery.blogspot.com/feeds/7856872709316466488/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7606927205095410759&amp;postID=7856872709316466488' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7606927205095410759/posts/default/7856872709316466488'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7606927205095410759/posts/default/7856872709316466488'/><link rel='alternate' type='text/html' href='http://programminggallery.blogspot.com/2007/09/how-creating-modeless-dialog-using-mfc.html' title='How to Create modal and Modeless Dialog using MFC and what is the difference'/><author><name>Shine Joseph</name><uri>http://www.blogger.com/profile/07634054201076817071</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7606927205095410759.post-5209696472420987859</id><published>2007-09-23T05:51:00.000-07:00</published><updated>2007-09-23T07:38:18.500-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='VC++'/><title type='text'>implementing your application as Single instance.</title><content type='html'>&lt;p&gt;&lt;strong&gt;&lt;span style="color:#000099;"&gt;How to implement your application as Single instance.&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Some times you need to create an application that your application is allowed to run only one instance in the entire operating system. There are a lot of ways that you can implement the single instance for your application. Here I am going to explain a methods that you can implement &lt;/p&gt;&lt;p&gt;Create a Mutes instance in the by a valid name in the InitInstance() function in the application class as like&lt;/p&gt;&lt;p&gt;            HANDLE m_mutex = CreateMutex(NULL,false,"GuiThreadDemoApp");&lt;br /&gt;&lt;/p&gt;&lt;p&gt;            if(m_mutex &amp;amp;&amp;amp; GetLastError() == ERROR_ALREADY_EXISTS)&lt;br /&gt;            {&lt;/p&gt;&lt;p&gt;                        ReleaseMutex(m_mutex);&lt;br /&gt;                        AfxMessageBox("Application already running",MB_ICONINFORMATION);&lt;br /&gt;                        return false;&lt;/p&gt;&lt;p&gt;            }&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Expecting your comments        &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7606927205095410759-5209696472420987859?l=programminggallery.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programminggallery.blogspot.com/feeds/5209696472420987859/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7606927205095410759&amp;postID=5209696472420987859' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7606927205095410759/posts/default/5209696472420987859'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7606927205095410759/posts/default/5209696472420987859'/><link rel='alternate' type='text/html' href='http://programminggallery.blogspot.com/2007/09/implementing-your-application-as-single.html' title='implementing your application as Single instance.'/><author><name>Shine Joseph</name><uri>http://www.blogger.com/profile/07634054201076817071</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7606927205095410759.post-3917156952008219874</id><published>2007-09-09T03:15:00.000-07:00</published><updated>2007-09-23T06:17:41.118-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='VC++'/><title type='text'>Multithreading in MFC</title><content type='html'>&lt;p&gt;&lt;span style="color:#000099;"&gt;&lt;strong&gt;Multithreading in MFC&lt;/strong&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Using MFC you can create two kinds of threads.&lt;br /&gt;&lt;br /&gt;1. Worker threads or Generic Threads&lt;br /&gt;2. User interface threads.&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#000099;"&gt;&lt;strong&gt;Worker threads&lt;/strong&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Worker threads or Generic Threads worker threads are using mainly for background tasks like long mathematical calculation or doing the time taking process that the user intervention is not needed MFC provides a Specific function to create the Worker threads using AfxBeginThread&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#000099;"&gt;&lt;strong&gt;Starting the thread&lt;/strong&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;An MFC thread is started with a call to the function AfxBeginThread(). Here is the prototype of the AfxBefinThread()&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;CWinThread* AfxBeginThread(&lt;br /&gt;&lt;br /&gt;AFX_THREADPROC &lt;a href="http://www.blogger.com/"&gt;pfnThreadProc&lt;/a&gt;,&lt;br /&gt;&lt;br /&gt;LPVOID &lt;a href="http://www.blogger.com/"&gt;pParam&lt;/a&gt;,&lt;br /&gt;&lt;br /&gt;int &lt;a href="http://www.blogger.com/"&gt;nPriority&lt;/a&gt; = THREAD_PRIORITY_NORMAL,&lt;br /&gt;&lt;br /&gt;UINT &lt;a href="http://www.blogger.com/"&gt;nStackSize&lt;/a&gt; = 0,&lt;br /&gt;&lt;br /&gt;DWORD &lt;a href="http://www.blogger.com/"&gt;dwCreateFlags&lt;/a&gt; = 0,&lt;br /&gt;&lt;br /&gt;LPSECURITY_ATTRIBUTES &lt;a href="http://www.blogger.com/"&gt;lpSecurityAttrs&lt;/a&gt; = NULL );&lt;br /&gt;&lt;br /&gt;CWinThread* AfxBeginThread(&lt;br /&gt;&lt;br /&gt;CRuntimeClass* &lt;a href="http://www.blogger.com/"&gt;pThreadClass&lt;/a&gt;,&lt;br /&gt;&lt;br /&gt;int &lt;a href="http://www.blogger.com/"&gt;nPriority&lt;/a&gt; = THREAD_PRIORITY_NORMAL,&lt;br /&gt;&lt;br /&gt;UINT &lt;a href="http://www.blogger.com/"&gt;nStackSize&lt;/a&gt; = 0,&lt;br /&gt;&lt;br /&gt;DWORD &lt;a href="http://www.blogger.com/"&gt;dwCreateFlags&lt;/a&gt; = 0,&lt;br /&gt;&lt;br /&gt;LPSECURITY_ATTRIBUTES &lt;a href="http://www.blogger.com/"&gt;lpSecurityAttrs&lt;/a&gt; = NULL );&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Parameters&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.blogger.com/"&gt;pfnThreadProc&lt;/a&gt; Points to the controlling function for the worker thread. Cannot be NULL. This function must be declared as follows: UINT MyControllingFunction( LPVOID pParam );&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.blogger.com/"&gt;pThreadClass&lt;/a&gt; The RUNTIME_CLASS of an object derived from CWinThread.&lt;br /&gt;pParam Parameter to be passed to the controlling function as shown in the parameter to the function declaration in pfnThreadProc.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.blogger.com/"&gt;nPriority&lt;/a&gt; The desired priority of the thread. If 0, the same priority as the creating thread will be used. For a full list and description of the available priorities, see SetThreadPriority.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.blogger.com/"&gt;nStackSize&lt;/a&gt; Specifies the size in bytes of the stack for the new thread. If 0, the stack size defaults to the same size stack as the creating thread.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.blogger.com/"&gt;dwCreateFlags&lt;/a&gt; Specifies an additional flag that controls the creation of the thread. This flag can contain one of two values:&lt;br /&gt;&lt;br /&gt;CREATE_SUSPENDED Start the thread with a suspend count of one. Use CREATE_SUSPENDED if you want to initialize any member data of the CWinThread object, such as m_bAutoDelete or any members of your derived class, before the thread starts running. Once your initialization is complete, use the CWinThread::ResumeThread to start the thread running. The thread will not execute until CWinThread::ResumeThread is called.&lt;br /&gt;providing zero will Start the thread immediately after creation.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.blogger.com/"&gt;lpSecurityAttrs&lt;/a&gt; Points to a SECURITY_ATTRIBUTES structure that specifies the security attributes for the thread. If NULL, the same security attributes as the creating thread will be used. For more information on this structure, see the Platform SDK.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Return Value&lt;/strong&gt;&lt;br /&gt;Pointer to the newly created thread object.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Now let us See how we can create a WorkerThread using AfxBeginThread. the Worker threads functionality is incorporated into a static function. The prototype looks like&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;UINT MyThreadProc(LPVOID pParam);&lt;br /&gt;&lt;br /&gt;following is a sample that using the worker thread using MFC&lt;br /&gt;&lt;br /&gt;Create a Simple Dialog Based application using Application Wizard. Put a Button in the dialog and add the following code to the Dialog Template Header file&lt;br /&gt;&lt;br /&gt;static UINT ThreadFun(LPVOID lparam);&lt;br /&gt;&lt;/p&gt;&lt;p&gt;add the corresponding implementation to the .Cpp file like&lt;br /&gt;&lt;br /&gt;UINT CWorkerThreadDlg::ThreadFun(LPVOID lparam)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;HWND m_hwnd = (HWND)lparam;&lt;br /&gt;for(int i = 0; i&amp;lt;=2000;i++)&lt;br /&gt;{&lt;br /&gt;CString str;&lt;br /&gt;str.Format("%d",i);&lt;br /&gt;::SetWindowText(m_hwnd, str);&lt;br /&gt;Sleep(1000);&lt;br /&gt;}&lt;br /&gt;return 1;&lt;br /&gt;} &lt;/p&gt;&lt;br /&gt;&lt;p&gt;Add the following code to the click event of the Button you added to the Dialog template looks like &lt;/p&gt;&lt;p&gt;void CWorkerThreadDlg::OnButton1()&lt;br /&gt;{&lt;br /&gt;HWND m_hwn = this-&amp;gt;m_hWnd;&lt;br /&gt;CWinThread *thread1 = NULL;&lt;br /&gt;thread1 = ::AfxBeginThread(ThreadFun,(LPVOID)m_hwn);&lt;br /&gt;if(thread1-&gt;;m_hThread == NULL)&lt;br /&gt;{&lt;br /&gt;MessageBox("AfxBeginThread Failed to Create");&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Now hit the F5 and find how it is working&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:#000099;"&gt;&lt;strong&gt;Creating GUI threads or user interface threads using MFC&lt;/strong&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;The secon kind of thread hat is provided by the MFC is User interface threads. The main advantage of the user interface threads over the worker threads is that it can imlement message maps and message handlers. If you want to create a user inerface thread you have to derive a class using CwinThread&lt;br /&gt;&lt;br /&gt;To Create a GUI thread follow the following Steps&lt;br /&gt;&lt;br /&gt;Create a Dialog Based application by Starting the Visual studio editor 6&lt;br /&gt;&lt;br /&gt;1. Select File-&amp;gt;Project Tab-&amp;gt;Select MFC Application Wizard Exe&lt;br /&gt;2. In the project name column Give "GuiThreadDemo" and click Next&lt;br /&gt;3. Select the 'Dialog Based' option from the 2nd Wizard and click next and Click Finish&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Now you have Created a Dialog Template for your Purpose. Next step is to create the GUI&lt;br /&gt;Thread class for your purpose&lt;br /&gt;4. Add a label to the dialog template and Rename its id as IDC_STATIC_CNTR &lt;/p&gt;&lt;p&gt;5. change the caption of the OK button to “Start Thread”&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;For Creating A GUI thread you need to create a Class that is derived from the CWinThread&lt;br /&gt;&lt;br /&gt;6. Go to Insert-&amp;gt;In the main Menu-&amp;gt; Insert-&amp;gt;New Class&lt;br /&gt;7. Insert a New class GuiThreadSample where choosing the class derived from CWinThread &lt;/p&gt;&lt;p&gt;8. Add the following members as public to the class GuiThreadSample&lt;/p&gt;&lt;p&gt;CDialog dlg;&lt;br /&gt;bool exitthread;&lt;/p&gt;&lt;p&gt;9. Add a member function to the GuiThreadSample as&lt;/p&gt;&lt;p&gt;void GuiThreadSample::StartCounter()&lt;br /&gt;{&lt;br /&gt;for(int i = 0;i&amp;lt;=50;i++)&lt;br /&gt;{&lt;br /&gt;Sleep(100);&lt;br /&gt;CString str;&lt;br /&gt;str.Format("%d",i);&lt;br /&gt;SetDlgItemText(dlg.m_hWnd,IDC_STATIC_CNTR,str);&lt;br /&gt;if(exitthread == true)&lt;br /&gt;break;&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;&lt;p&gt;10. Add following to the GuiThreadSample constructor&lt;/p&gt;&lt;p&gt;exitthread = false;&lt;/p&gt;&lt;p&gt;11. and add the following to the destructor member function of GuiThreadSample&lt;/p&gt;&lt;p&gt;AfxMessageBox("Closed The Thread");&lt;/p&gt;&lt;p&gt;12. Add the following to the Run() member function &lt;/p&gt;&lt;p&gt;StartCounter();&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;13. Now go to the dialog class add add the following code on the OnOk Member function by douleclicking on the Ok button&lt;/p&gt;&lt;p&gt;void CGuiThreadDemoDlg::OnOK()&lt;br /&gt;{&lt;br /&gt;// TODO: Add extra validation here&lt;br /&gt;&lt;br /&gt;CString strcaption;&lt;br /&gt;GetDlgItemText(IDOK,strcaption);&lt;br /&gt;if(strcaption == "Start Thread")&lt;br /&gt;{&lt;br /&gt;SetDlgItemText(IDOK,"Stop Thread");&lt;br /&gt;threadwork = (GuiThreadSample *)AfxBeginThread(RUNTIME_CLASS(GuiThreadSample),THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED,0);&lt;/p&gt;&lt;p&gt;threadwork-&amp;gt;dlg.m_hWnd = this-&amp;gt;m_hWnd;&lt;br /&gt;threadwork-&amp;gt;ResumeThread();&lt;/p&gt;&lt;p&gt;if(threadwork == NULL)&lt;br /&gt;{&lt;br /&gt;MessageBox("Failed to Create the Thread");&lt;br /&gt;}&lt;br /&gt;// Creating The GUI Thread&lt;br /&gt;}else&lt;br /&gt;{&lt;br /&gt;threadwork-&amp;gt;exitthread = true;&lt;br /&gt;if(!threadwork-&amp;gt;PostThreadMessage(WM_QUIT,0,0))&lt;br /&gt;{&lt;br /&gt;AfxMessageBox("Failed to Post Message");&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;// CDialog::OnOK();&lt;br /&gt;}&lt;/p&gt;&lt;p&gt;Now execute the application by hitting alt + F5 and see how the thread is working for you&lt;/p&gt;any comments mail to me at &lt;a href="mailto:shinejos@gmail.com"&gt;shinejos@gmail.com&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7606927205095410759-3917156952008219874?l=programminggallery.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programminggallery.blogspot.com/feeds/3917156952008219874/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7606927205095410759&amp;postID=3917156952008219874' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7606927205095410759/posts/default/3917156952008219874'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7606927205095410759/posts/default/3917156952008219874'/><link rel='alternate' type='text/html' href='http://programminggallery.blogspot.com/2007/09/multithreading-in-mfc.html' title='Multithreading in MFC'/><author><name>Shine Joseph</name><uri>http://www.blogger.com/profile/07634054201076817071</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7606927205095410759.post-1933492077443360474</id><published>2007-09-07T22:45:00.000-07:00</published><updated>2007-09-23T06:18:11.411-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>Multithreading in win32</title><content type='html'>&lt;span style="color:#333399;"&gt;&lt;strong&gt;what is a thread or &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_0"&gt;multithreading&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_1"&gt;multithreading&lt;/span&gt; is a specialized form of multitasking. Generally &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_2"&gt;there&lt;/span&gt; are two kinds of multitasking. process based and thread based. A process is an essence one that is an application or &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_3"&gt;program&lt;/span&gt; that is executed in an operating system. for &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_4"&gt;example&lt;/span&gt; a word process or a notepad on your desktop is a process.&lt;br /&gt;&lt;br /&gt;All process have &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_5"&gt;at least&lt;/span&gt; one thread. But they can have more than one.So a &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_6"&gt;multi thread&lt;/span&gt; application can have more than one path of executions &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_7"&gt;concurrently.&lt;/span&gt; The main thread starts first and the main thread is creating child threads. so a thread that is designed properly will handle different logical units of it's functionality.&lt;br /&gt;&lt;br /&gt;The main advantage of multithread is it helps you to write applications that will utilize the idle time is that is available in the application or it will increase the throughput of the application.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;strong&gt;&lt;span style="font-size:85%;color:#3333ff;"&gt;Creating and managing a thread&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="font-size:85%;color:#3333ff;"&gt;&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;span style="color:#3333ff;"&gt;&lt;span style="font-size:85%;"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;span style="color:#000000;"&gt;&lt;span style="font-size:100%;"&gt;to use windows multithreading you must use '&lt;'windows.h'&gt;'&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; .&lt;br /&gt;&lt;br /&gt;To create a thread windows api provides CreateThread API.&lt;br /&gt;&lt;br /&gt;HANDLE &lt;span style="color:#000099;"&gt;CreateThread&lt;/span&gt;(&lt;br /&gt;LPSECURITY_ATTRIBUTES &lt;a class="synParam" onclick="showTip(this)" href="http://www.blogger.com/"&gt;lpThreadAttributes&lt;/a&gt;,&lt;br /&gt;SIZE_T &lt;a class="synParam" onclick="showTip(this)" href="http://www.blogger.com/"&gt;dwStackSize&lt;/a&gt;,&lt;br /&gt;LPTHREAD_START_ROUTINE &lt;a class="synParam" onclick="showTip(this)" href="http://www.blogger.com/"&gt;lpStartAddress&lt;/a&gt;,&lt;br /&gt;LPVOID &lt;a class="synParam" onclick="showTip(this)" href="http://www.blogger.com/"&gt;lpParameter&lt;/a&gt;,&lt;br /&gt;DWORD &lt;a class="synParam" onclick="showTip(this)" href="http://www.blogger.com/"&gt;dwCreationFlags&lt;/a&gt;,&lt;br /&gt;LPDWORD &lt;a class="synParam" onclick="showTip(this)" href="http://www.blogger.com/"&gt;lpThreadId&lt;/a&gt;&lt;br /&gt;);&lt;br /&gt;&lt;br /&gt;where&lt;br /&gt;&lt;br /&gt;&lt;a class="synParam" onclick="showTip(this)" href="http://www.blogger.com/"&gt;lpThreadAttributes&lt;/a&gt; [in] Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpThreadAttributes is NULL, the handle cannot be inherited.&lt;br /&gt;&lt;br /&gt;The lpSecurityDescriptor member of the structure specifies a security descriptor for the new thread. If lpThreadAttributes is NULL, the thread gets a default security descriptor. The ACLs in the default security descriptor for a thread come from the primary or impersonation token of the creator.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="color:#3333ff;"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;a class="synParam" onclick="showTip(this)" href="http://www.blogger.com/"&gt;dwStackSize&lt;/a&gt; &lt;span style="color:#000000;"&gt;[in] Initial size of the stack, in bytes. The system rounds this value to the nearest page. If this parameter is zero, the new thread uses the default size for the executable. &lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:#3333ff;"&gt;&lt;a class="synParam" onclick="showTip(this)" href="http://www.blogger.com/"&gt;lpStartAddress&lt;/a&gt; &lt;span style="color:#000000;"&gt;[in] Pointer to the application-defined function to be executed by the thread and represents the starting address of the thread. For more information on the thread function, see &lt;/span&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms686736.aspx"&gt;&lt;span style="color:#000000;"&gt;ThreadProc&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;. &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;a class="synParam" onclick="showTip(this)" href="http://www.blogger.com/"&gt;lpParameter&lt;/a&gt;[in] Pointer to a variable to be passed to the thread. &lt;/p&gt;&lt;p&gt;&lt;a class="synParam" onclick="showTip(this)" href="http://www.blogger.com/"&gt;dwCreationFlags&lt;/a&gt; [in] Flags that control the creation of the thread. If the CREATE_SUSPENDED flag is specified, the thread is created in a suspended state, and will not run until the &lt;a href="http://msdn2.microsoft.com/en-us/library/ms685086.aspx"&gt;ResumeThread&lt;/a&gt; function is called. If this value is zero, the thread runs immediately after creation. &lt;/p&gt;&lt;p&gt;&lt;br /&gt;If the STACK_SIZE_PARAM_IS_A_RESERVATION flag is specified, the dwStackSize parameter specifies the initial reserve size of the stack. Otherwise, dwStackSize specifies the commit size.&lt;/p&gt;&lt;p&gt;&lt;a class="synParam" onclick="showTip(this)" href="http://www.blogger.com/"&gt;lpThreadId&lt;/a&gt; [out] Pointer to a variable that receives the thread identifier. If this parameter is NULL, the thread identifier is not returned.&lt;br /&gt;Windows Me/98/95: This parameter may not be NULL.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Return Values&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;If the function succeeds, the return value is a handle to the new thread.&lt;br /&gt;If the function fails, the return value is NULL. To get extended error information, call GetLastError.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Thread Function&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Each thread of execution should start with a ThreadFunction that is passed to the &lt;a class="synParam" onclick="showTip(this)" href="http://www.blogger.com/"&gt;lpStartAddress&lt;/a&gt; the prototype for the function is &lt;/p&gt;&lt;p&gt;&lt;span style="color:#3333ff;"&gt;DWORD WINAPI threadfunc(LPVOID param);&lt;/span&gt;&lt;/p&gt;&lt;p&gt;Arguements that you want to pass to the thread should pass to &lt;a class="synParam" onclick="showTip(this)" href="http://www.blogger.com/"&gt;lpParameter&lt;/a&gt; of CreateThread()&lt;/p&gt;&lt;p&gt;Here is a sample application that will state how to use the Createthread &lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;span xmlns="http://www.w3.org/TR/REC-html40" w="urn:schemas-microsoft-com:office:word"&gt;&lt;style&gt;&lt;br /&gt;&lt;!--  /* Style Definitions */  p.MsoNormal, li.MsoNormal, div.MsoNormal  {mso-style-parent:"";  margin:0in;  margin-bottom:.0001pt;  mso-pagination:widow-orphan;  font-size:12.0pt;  font-family:"Times New Roman";  mso-fareast-font-family:"Times New Roman";} span.SpellE  {mso-style-name:"";  mso-spl-e:yes;} span.GramE  {mso-style-name:"";  mso-gram-e:yes;} @page Section1  {size:8.5in 11.0in;  margin:1.0in 1.25in 1.0in 1.25in;  mso-header-margin:.5in;  mso-footer-margin:.5in;  mso-paper-source:0;} div.Section1  {page:Section1;} --&gt;&lt;br /&gt;&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="Section1"&gt;&lt;p class="MsoNormal"&gt;&lt;span style="color:blue;"&gt;#include&amp;lt;&lt;/span&gt;&lt;span class="SpellE"&gt;windows.h&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt;&amp;lt;&lt;span class="SpellE"&gt;iostream.h&lt;/span&gt;&amp;gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="color:blue;"&gt;#include&lt;/span&gt;&amp;lt;&lt;span class="SpellE"&gt;string.h&lt;/span&gt;&amp;gt;&lt;?xml:namespace prefix = o /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;DWORD WINAPI &lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;MyThread&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;LPVOID &lt;span class="SpellE"&gt;lparam&lt;/span&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 1"&gt;&lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;cout&lt;/span&gt;&lt;/span&gt;&amp;lt;&amp;lt;"Printing the values that passed";&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 1"&gt;&lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color:blue;"&gt;char&lt;/span&gt;&lt;/span&gt; &lt;span class="SpellE"&gt;lzparam&lt;/span&gt;[10];&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 1"&gt;&lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;strcpy&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;&lt;span class="SpellE"&gt;lzparam&lt;/span&gt;,(&lt;span style="color:blue;"&gt;char&lt;/span&gt;*)&lt;span class="SpellE"&gt;lparam&lt;/span&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 1"&gt;&lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;&lt;span style="color:blue;"&gt;int&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class="SpellE"&gt;len&lt;/span&gt; = &lt;span class="SpellE"&gt;strlen&lt;/span&gt;(&lt;span class="SpellE"&gt;lzparam&lt;/span&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 2"&gt;&lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color:blue;"&gt;for&lt;/span&gt;&lt;/span&gt; (&lt;span class="SpellE"&gt;&lt;span style="color:blue;"&gt;int&lt;/span&gt;&lt;/span&gt; &lt;span class="SpellE"&gt;i&lt;/span&gt; = 0;i&amp;lt;=&lt;span class="SpellE"&gt;len;i&lt;/span&gt;++)&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 2"&gt;&lt;/span&gt;{&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 3"&gt;&lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;cout&lt;/span&gt;&lt;/span&gt;&amp;lt;&amp;lt;&lt;span class="SpellE"&gt;lzparam&lt;/span&gt;[&lt;span class="SpellE"&gt;i&lt;/span&gt;];&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 2"&gt;&lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 1"&gt;&lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;cout&lt;/span&gt;&lt;/span&gt;&amp;lt;&amp;lt;"\n\r";&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span class="GramE"&gt;&lt;span style="color:blue;"&gt;return&lt;/span&gt;&lt;/span&gt; 1;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span class="GramE"&gt;&lt;span style="color:blue;"&gt;void&lt;/span&gt;&lt;/span&gt; main()&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 1"&gt;&lt;/span&gt;HANDLE handle1&lt;span class="GramE"&gt;,handle2&lt;/span&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 1"&gt;&lt;/span&gt;DWORD &lt;span class="SpellE"&gt;Generalthread&lt;/span&gt;;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 1"&gt;&lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color:blue;"&gt;char&lt;/span&gt;&lt;/span&gt; &lt;span class="SpellE"&gt;lparamtopass&lt;/span&gt;[10];&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 1"&gt;&lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;strcpy&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;&lt;span class="SpellE"&gt;lparamtopass,"fromfirst&lt;/span&gt; ");&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 1"&gt;&lt;/span&gt;handle1 = &lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;CreateThread&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;NULL,0,MyThread,&amp;amp;lparamtopass,0,&amp;amp;amp;amp;amp;amp;amp;Generalthread);&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 2"&gt;&lt;/span&gt;&lt;span class="GramE"&gt;if(&lt;/span&gt;handle1 == NULL)&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 2"&gt;&lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 3"&gt;&lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;cout&lt;/span&gt;&lt;/span&gt;&amp;lt;&amp;lt;"Error Creating the Thread 1";&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 2"&gt;&lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color:blue;"&gt;return&lt;/span&gt; ;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 2"&gt;&lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 1"&gt;&lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;WaitForSingleObject&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;handle1,INFINITE);&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 1"&gt;&lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;strcpy&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;&lt;span class="SpellE"&gt;lparamtopass,"fromsecond&lt;/span&gt;");&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 1"&gt;&lt;/span&gt;handle2 = &lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;CreateThread&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;NULL,0,MyThread,&amp;amp;lparamtopass,0,&amp;amp;amp;amp;amp;amp;amp;Generalthread);&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 2"&gt;&lt;/span&gt;&lt;span class="GramE"&gt;if(&lt;/span&gt;handle2 == NULL)&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 2"&gt;&lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 3"&gt;&lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;cout&lt;/span&gt;&lt;/span&gt;&amp;lt;&amp;lt;"Error Creating the Thread 2";&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 2"&gt;&lt;/span&gt;&lt;span class="GramE"&gt;&lt;span style="color:blue;"&gt;return&lt;/span&gt; ;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 2"&gt;&lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count: 1"&gt;&lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;WaitForSingleObject&lt;/span&gt;&lt;/span&gt;&lt;span class="GramE"&gt;(&lt;/span&gt;handle2,INFINITE);&lt;o:p&gt;&lt;/o:p&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;}&lt;/p&gt;&lt;/div&gt;Using a &lt;a href="http://msdn2.microsoft.com/en-us/library/ms687032.aspx"&gt;WaitForSingleObject&lt;/a&gt; API to wait for the first thread to be executed and then execute second thread. If the thread is failed to return the WaitForSingleObject will wait for a infinite time . If you want you can give the milliseconds that you want to wait like 1000 (1 second) .&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7606927205095410759-1933492077443360474?l=programminggallery.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programminggallery.blogspot.com/feeds/1933492077443360474/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7606927205095410759&amp;postID=1933492077443360474' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7606927205095410759/posts/default/1933492077443360474'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7606927205095410759/posts/default/1933492077443360474'/><link rel='alternate' type='text/html' href='http://programminggallery.blogspot.com/2007/09/using-multithreading-in-mfc-and-win32.html' title='Multithreading in win32'/><author><name>Shine Joseph</name><uri>http://www.blogger.com/profile/07634054201076817071</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7606927205095410759.post-7577889956662856618</id><published>2007-09-06T11:09:00.000-07:00</published><updated>2007-09-23T06:18:50.746-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='VC++'/><title type='text'>Sending Message to Threads</title><content type='html'>&lt;div align="left"&gt;When a user want to send a message to a window postMessage or SendMessage is used but if you want to send a message to a Thread use PostThreadMessage&lt;br /&gt;&lt;br /&gt;The PostThreadMessage API is posts a message to the message queue of a specified GUI thread.&lt;br /&gt;&lt;br /&gt;The following is the prototype for the PostThreadMessage&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#6666cc;"&gt;&lt;/span&gt;&lt;span style="color:#333399;"&gt;BOOL PostThreadMessage ( DWORD idThread, UINT Msg, WPARAM wParam, LPARAM lParam);&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;where the parameters&lt;br /&gt;&lt;br /&gt;idThread [in] identifier of the thread to be the message posted&lt;br /&gt;The function will fail if the thread is not a GUI Thread&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;/blockquote&gt;Msg [in] Secify the message to me posted&lt;br /&gt;&lt;br /&gt;wParam [in] Additional message specific information&lt;br /&gt;&lt;br /&gt;lParam [in] Additional message specific information&lt;br /&gt;&lt;br /&gt;return type&lt;br /&gt;&lt;br /&gt;if the function call is suceeded the return type is non zero. otherwise it will be zero. for getting the error information use the &lt;a href="http://msdn2.microsoft.com/en-us/library/ms679360.aspx"&gt;GetLastError&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7606927205095410759-7577889956662856618?l=programminggallery.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programminggallery.blogspot.com/feeds/7577889956662856618/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7606927205095410759&amp;postID=7577889956662856618' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7606927205095410759/posts/default/7577889956662856618'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7606927205095410759/posts/default/7577889956662856618'/><link rel='alternate' type='text/html' href='http://programminggallery.blogspot.com/2007/09/sending-message-to-threads.html' title='Sending Message to Threads'/><author><name>Shine Joseph</name><uri>http://www.blogger.com/profile/07634054201076817071</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7606927205095410759.post-5437764621776642478</id><published>2007-09-06T10:49:00.000-07:00</published><updated>2007-09-23T06:19:25.721-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='VC++'/><title type='text'>SendMessage Vs PostMessage</title><content type='html'>Usually in win32 for sending a message to a window is doing by using the messageing API's like SendMessage And PostMessage&lt;br /&gt;&lt;br /&gt;The Sendmessage and postmessage works similer with same parameters . But they have a slight difference.&lt;br /&gt;&lt;br /&gt;The prototype for the Sendmessage is&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#6633ff;"&gt;LRESULT SendMessage( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;and for PostMessage is&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#3333ff;"&gt;BOOL PostMessage( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The difference is that when you send a message by using SendMessage thewindow procedure is called and the calling procedure will wait for the message to be processed and replied back. and the calling program or procedure will wait for it's completion and then only it will resume while the PostMessage will notwait for the completion of the process and it will return to the calling program immediatly.&lt;br /&gt;&lt;br /&gt;So incase of the application is busy in processing a long procedure and hangs the application it is better to use PostMessage insted of the SendMessage. The usage of the SendMessage and PostMessage is depends upon your requirement&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7606927205095410759-5437764621776642478?l=programminggallery.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programminggallery.blogspot.com/feeds/5437764621776642478/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7606927205095410759&amp;postID=5437764621776642478' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7606927205095410759/posts/default/5437764621776642478'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7606927205095410759/posts/default/5437764621776642478'/><link rel='alternate' type='text/html' href='http://programminggallery.blogspot.com/2007/09/sendmessage-vs-postmessage-and.html' title='SendMessage Vs PostMessage'/><author><name>Shine Joseph</name><uri>http://www.blogger.com/profile/07634054201076817071</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7606927205095410759.post-3676826448925459353</id><published>2007-09-05T22:00:00.000-07:00</published><updated>2007-09-23T06:19:53.518-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><category scheme='http://www.blogger.com/atom/ns#' term='VC++'/><title type='text'>how to create a simple widow using MFC and Win32</title><content type='html'>&lt;strong&gt;A simple MFC window Application &lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;Introduction &lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Creating a Simple window or dialog application using VC++ 6.00 or 7.00 applications Wizard is quiet easy job. But if you Examine the code you find that a lot of Stuff inside that you not familiar and for a beginner he will not get any idea how the basic stuff will work.&lt;br /&gt;&lt;br /&gt;Here i am going to explain how you can create a simple window without the help of Wizard. For simplicity I am using VC++ 6.00 Editor . Your machine must have installed the VC++ 6.0.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Start The VC++ Editor From your Programs And&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The following window will pop up&lt;br /&gt;&lt;br /&gt;&lt;img id="BLOGGER_PHOTO_ID_5107142768389679394" style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://2.bp.blogspot.com/_h5vmCeBvtO8/RuA3Sf8SaSI/AAAAAAAAAAY/5tQGhdDLyeQ/s320/image001.jpg" border="0" /&gt;&lt;br /&gt;&lt;br /&gt;Give ‘Simple Window’ in the project name box. And click ‘win32 Application’ from the project List and click OK. You can see the next dialog as shown below&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;img id="BLOGGER_PHOTO_ID_5107143086217259314" style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://4.bp.blogspot.com/_h5vmCeBvtO8/RuA3k_8SaTI/AAAAAAAAAAg/ZNFjDdhKZ5s/s320/image002.jpg" border="0" /&gt;now select 'An empty project' and click finishNow you have created the workspace named 'simple window' sucessfully.Next you have to add the Cpp files to the workspace. For adding the files to the workspace goto File-&gt;New and select the 'files' tab from the pop up dialog. select 'C++ Source file from the tab' and give 'Windowsource' in the as shown in the foollowing figure and click OK button&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;img id="BLOGGER_PHOTO_ID_5107143404044839234" style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://2.bp.blogspot.com/_h5vmCeBvtO8/RuA33f8SaUI/AAAAAAAAAAo/N2vtWiXGcpE/s320/image003.jpg" border="0" /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Now you have created all the necessaries to create a simple window.&lt;br /&gt;Now we can move to the coding section open the Windowsource.cpp and decalre the following header&lt;/p&gt;&lt;p&gt;&lt;span style="color:#000099;"&gt;&lt;br /&gt;#include "afxwin.h&lt;afxwin.h&gt;"&lt;afxwin.h&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;The above decalration will afxwin.h contain all the decalarations and reference for accessing the MFC classes including the CFrameWnd and CWinApp&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Write down the following code to the Windowsource.cpp &lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;span style="color:#330099;"&gt;#include "afxwin.h&lt;afxwin.h&gt;"&lt;/span&gt;&lt;afxwin.h&gt;&lt;/p&gt;&lt;p&gt;class mywindow : public CFrameWnd&lt;/p&gt;&lt;p&gt;{&lt;/p&gt;&lt;p&gt;public :&lt;/p&gt;&lt;p&gt;mywindow() &lt;/p&gt;&lt;p&gt;{ &lt;/p&gt;&lt;p&gt;Create(0,"Simple Window"); }&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;}; &lt;/p&gt;&lt;p&gt;class myapplication : public CWinApp&lt;/p&gt;&lt;p&gt;{&lt;/p&gt;&lt;p&gt;public: int InitInstance()&lt;/p&gt;&lt;p&gt;{ &lt;/p&gt;&lt;p&gt;mywindow *p = new mywindow; &lt;/p&gt;&lt;p&gt;p-&gt;ShowWindow(1); &lt;/p&gt;&lt;p&gt;m_pMainWnd = p; &lt;/p&gt;&lt;p&gt;return 1; &lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;};myapplication a; &lt;/p&gt;&lt;p&gt;Before compiling you go to project-&gt;settings or click ALT + F7 .Now your code redy to compile.&lt;/p&gt;&lt;p&gt;&lt;img id="BLOGGER_PHOTO_ID_5107144756959537490" style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://1.bp.blogspot.com/_h5vmCeBvtO8/RuA5GP8SaVI/AAAAAAAAAAw/t3Yxrs0eggo/s320/image004.jpg" border="0" /&gt;Now Run the project by selecting Build-&gt;Execute Simple Window.exe or use Ctrl + F5&lt;br /&gt;ow you can see the window on your desktop.&lt;/p&gt;&lt;p&gt;Try Your self see it working. &lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7606927205095410759-3676826448925459353?l=programminggallery.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programminggallery.blogspot.com/feeds/3676826448925459353/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7606927205095410759&amp;postID=3676826448925459353' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7606927205095410759/posts/default/3676826448925459353'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7606927205095410759/posts/default/3676826448925459353'/><link rel='alternate' type='text/html' href='http://programminggallery.blogspot.com/2007/09/how-to-create-simple-widow-using-mfc.html' title='how to create a simple widow using MFC and Win32'/><author><name>Shine Joseph</name><uri>http://www.blogger.com/profile/07634054201076817071</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_h5vmCeBvtO8/RuA3Sf8SaSI/AAAAAAAAAAY/5tQGhdDLyeQ/s72-c/image001.jpg' height='72' width='72'/><thr:total>0</thr:total></entry></feed>
