Multithreading
-
Hi guys, i am new to multithreading in MFC. I have gone through many of the examples posted on CodeProject, but i am still not satisfied. What i want to know is that, suppose i have my window, and i want to create a thread which, say, takes care of all the keyboard inputs, another thread which takes care of all the mouse inputs. So far whatever examples and tutorials i have gone through first create a new window using a thread and then control all the operations. I want my parent window to have a thread to do all those stuff. Can anyone help me out. :)Thanx in advance!:) AslFunky
-
Hi guys, i am new to multithreading in MFC. I have gone through many of the examples posted on CodeProject, but i am still not satisfied. What i want to know is that, suppose i have my window, and i want to create a thread which, say, takes care of all the keyboard inputs, another thread which takes care of all the mouse inputs. So far whatever examples and tutorials i have gone through first create a new window using a thread and then control all the operations. I want my parent window to have a thread to do all those stuff. Can anyone help me out. :)Thanx in advance!:) AslFunky
With MFC, all GUI interactions must take place on the UI thread; this is inherent to the MFC architecture. You can, however, have separate worker threads that do the actual work associated with keyboard and mouse actions. The UI thread detects a keyboard and/or mouse action and starts a worker thread. The worker thread does its thing, and the posts messages back to the UI thread as necessary. AslFunky wrote: i want to create a thread which, say, takes care of all the keyboard inputs, another thread which takes care of all the mouse inputs This statement sounds like you want to make a general change in how Windows applications work. I think you would be better off learning how to use the Windows input model, rather than trying to subvert it.
Software Zen:
delete this;
-
Hi guys, i am new to multithreading in MFC. I have gone through many of the examples posted on CodeProject, but i am still not satisfied. What i want to know is that, suppose i have my window, and i want to create a thread which, say, takes care of all the keyboard inputs, another thread which takes care of all the mouse inputs. So far whatever examples and tutorials i have gone through first create a new window using a thread and then control all the operations. I want my parent window to have a thread to do all those stuff. Can anyone help me out. :)Thanx in advance!:) AslFunky
Hi. The easist way is use AfxBeginThread (threadproc,hwnd,threadpriority) threadproc : threadfunction with form UINT threadproc(void* para) { //here do any thing return true } hwnd:this is the para parameter on threadproc function threadpriority:may be 1-THREAD_PRIORITY_NORMAL 2-THREAD_PRIORITY_LOWEST . AfxBeginThread return CWinThread object this object can be use to suspend and kill the thread u can use many thread with the above story If this not help u contact me on faroq_tam2004@yahoo.com to get sample MFC program describe MFC MThreading bye. On Earth nothing impossible, while your mind running. Faroqtam
-
With MFC, all GUI interactions must take place on the UI thread; this is inherent to the MFC architecture. You can, however, have separate worker threads that do the actual work associated with keyboard and mouse actions. The UI thread detects a keyboard and/or mouse action and starts a worker thread. The worker thread does its thing, and the posts messages back to the UI thread as necessary. AslFunky wrote: i want to create a thread which, say, takes care of all the keyboard inputs, another thread which takes care of all the mouse inputs This statement sounds like you want to make a general change in how Windows applications work. I think you would be better off learning how to use the Windows input model, rather than trying to subvert it.
Software Zen:
delete this;
Hi thanx for replying,;) Is it necessary to do my stuff in a worker thread? Actually i am a little nervous in creating a worker thread. i always have one confusion, how will i assign any job to a function which is not a member of my class. I mean, how will this function UINT threadproc(void* para) be able to access the members of my class when it is not a member of my class. Can you please elaborate on this point. Thanx in advance. AslFunky
-
Hi thanx for replying,;) Is it necessary to do my stuff in a worker thread? Actually i am a little nervous in creating a worker thread. i always have one confusion, how will i assign any job to a function which is not a member of my class. I mean, how will this function UINT threadproc(void* para) be able to access the members of my class when it is not a member of my class. Can you please elaborate on this point. Thanx in advance. AslFunky
AslFunky wrote: Is it necessary to do my stuff in a worker thread? That depends on what you need to do. If the process takes a lot of time, you don't want to block the user interface while the process runs. That sort of thing is something you can put in a worker thread. AslFunky wrote: I mean, how will this function UINT threadproc(void* para) be able to access the members of my class when it is not a member of my class. I always use something like the following:
class Stuff {
//...
static UINT ThreadStart(LPVOID parameter);
UINT Thread();
};
UINT Stuff::ThreadStart(LPVOID parameter)
{
Stuff *_this = (Stuff *)parameter;
return _this->Thread();
}
UINT Stuff::Thread()
{
//...
}
//...
AfxBeginThread(ThreadStart,(LPVOID)this);The
static
member function matches the prototype required byAfxBeginThread()
. You use the parameter to pass a pointer to an instance of your class. In my example, I passed thethis
pointer, which means I'm starting the thread from another member function of the class. The function that does the actual work of the thread is calledThread()
, and since it is a member function, it can access other members of theStuff
class.
Software Zen:
delete this;