|
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 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 by AfxBeginThread() . You use the parameter to pass a pointer to an instance of your class. In my example, I passed the this 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 called Thread() , and since it is a member function, it can access other members of the Stuff class.
Software Zen: delete this;
|
|
|
|
|
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
|
|
|
|
|
How would one retreive data input from the keyboard into a dialog box, which the compiler would process at run-time? I thought that making an instance of the class object which contains the data would be enough; but this doesn't cut it. See the following code:
<br />
void unbound(double *unbd, int nSize)<br />
{<br />
enum{nSize = 100};<br />
double A[nSize],B[nSize],k0[nSize],k1[nSize],s[nSize],expi[nSize],a[nSize],b[nSize];<br />
<br />
void bessk0(double *x,double *k0,int nSize);<br />
void bessk1(double *x,double *k1, int nSize);<br />
void ei(double *x, double *expi, int nSize);<br />
<br />
double x=0;<br />
<br />
s[0] = 1;<br />
a[0] = x*sqrt(s[0]);<br />
b[0] = 2*sqrt(s[0]);<br />
<br />
bessk0(a,k0,nSize);<br />
bessk1(a,k1,nSize);<br />
ei(b,expi,nSize);<br />
<br />
A[0] = k0[0]/(x*sqrt(s[0])*k1[0]);<br />
B[0] = (1-exp(-2*sqrt(s[0])))/(2*sqrt(s[0]));<br />
<br />
unbd[0]=0.5*(A[0]-B[0]-expi[0]);<br />
<br />
for (int t=1;t<nSize;t++)<br />
{<br />
CKuchukDlg MyClass; <br />
x = 2;
<br />
s[t] = 2*t*PI;<br />
a[t] = x*sqrt(s[t]);<br />
b[t] = 2*sqrt(s[t]);<br />
<br />
bessk0(a,k0,nSize);<br />
bessk1(a,k1,nSize);<br />
ei(b,expi,nSize);<br />
<br />
A[t] = k0[t]/(x*sqrt(s[t])*k1[t]);<br />
B[t] = (1-exp(-2*sqrt(s[t])))/(2*sqrt(s[t]));<br />
<br />
unbd[t]=0.5*(A[t]-B[t]-expi[t]);<br />
}<br />
}
When line **** read x=MyClass.m_dWellRad; the compiler didn't process the variable m_dWellRad at run-time, but simply used x = 0(initialization). When I used x=2, it used the latter variable.
How then can I make the compiler use the value input from keyboard into my dialog box at run-time?
|
|
|
|
|
The statement
CKuchukDlg MyClass; isn't sufficient by itself to display the dialog. You need to do something like this:
CKuchukDlg MyClass;
MyClass.DoModal(); That will display the dialog.
Software Zen: delete this;
|
|
|
|
|
const int MIN_ELEMENTS = 5;<br />
typedef int ArrayType[MIN_ELEMENTS];<br />
typedef int ElementType;<br />
<br />
bool findSum( ArrayType myArray, int n )<br />
{<br />
ElementType firstFiveSum = 0;<br />
bool isSuccess = true;<br />
<br />
if( n < MIN_ELEMENTS )<br />
{<br />
isSuccess = false;<br />
}<br />
else<br />
{<br />
for( int index = 0; MIN_ELEMENTS > index; ++index )<br />
{<br />
if( !( myArray[index] < 0 ) )<br />
{<br />
firstFiveSum += myArray[index];<br />
}<br />
else<br />
{<br />
isSuccess = false;<br />
firstFiveSum = 0;<br />
break;<br />
}<br />
}<br />
}<br />
<br />
std::cout << firstFiveSum << "\n";<br />
<br />
return isSuccess;<br />
}<br />
<br />
int main()<br />
{<br />
const int FIRST_ARRAY_SIZE = 7;<br />
const int SECOND_ARRAY_SIZE = 3;<br />
<br />
ArrayType myFirstArray[FIRST_ARRAY_SIZE] = {1,1,1,1,1,1,1};<br />
ArrayType mySecondArray[SECOND_ARRAY_SIZE] = {1,1,1};<br />
<br />
findSum( myFirstArray, FIRST_ARRAY_SIZE );<br />
<br />
return 0;<br />
}
Visual Studio compiler gives an error like;
cannot convert parameter 1 from 'ArrayType [7]' to 'int []'
How can I solve this?
Thanks in advance.
-
When in doubt, push a pawn!
-
|
|
|
|
|
|
tis abt getting keyboard eventz in a dialog box having many windows in it!!!!!how do i get the keyboard events to come to my dialogbox even when i am in some other window of that dialogbox???any ideas??
|
|
|
|
|
You will need to look at application wide hooking. That is, every time someone hit a key it gets preprocessed by your hook. A good place to start looking for this information is articles on custom menues, which offten need to hook the application inorder to change the appearance of all menues in the application. There are also, more than one article in the MSDN library on hooking (most are for subclassing though).
Keywords: hook, hooking, subclass, subclassing, application, system.
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
Hi,
I have a stylus (pen) whose coordinates are different from mouse coordinates. I tried to differentiate
the ratios but the stylus coordinates differ by mouse in speed , position and time. I thought of mouse hooking.
Like OnmouseMove() i have stylus OnMove() (sdk) , i thought if i could send the points generated by stylus in
OnMove() to the mouse callback function it should work,and i tried it i get assertions. Is there any way i could
map this stylus to mouse.
Thanx in advance.
ragiz
|
|
|
|
|
What does speed have to do with it?
It all has to do with coordinates, which may have to be translated from one coordinate system to anouther. (Sounds like a math problem!)
What mouse callback function? (Not OnMouseMove())
Ok! In order for someone to answer your question.
1) What is the difference between the stylus and mouse coordinates?
2) What do you mean by "differ by mouse in speed, position and time"? (This is not physics!)
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
What does speed have to do with it?
The stylus which i have( uses laser )generates lots of coordinates when written slowly. if u keep the pen still it generates alot of points while mouse generates only one point.
What mouse callback function? (Not onmousemove())
is the mouse hooking ( WH_JOURNALPLAYBACK )which plays the mouse move which was recorded earlier.
Just tell me is it possible to map these two by any means.
Thanx for reply
rajiz
|
|
|
|
|
I've did a little reading, this time>
Both WH_JOURNALPLAYBACK and WH_JOURNALRECORD are global for record and play back, they are probably not of much use for you purposes.
The MouseProc is application-defined, but it is only called after a mouse message is generated, and again does not seem to be what you need.
You may be able to have the DLL send mouse messages directly to your application, based on the stylus position.
If the resolution of the stylus is higher than the mouse resolution (which it sounds like it is), then you will have to calculate the relitive screen position from the given stylus position. If the relitive screen position changes: 1) save the position as current position and 2) Post (recommended) a mouse message to the application. You should only need to post a new message if the relitive mounse position changes; that is if the new calculated pixel position does not equal current position.
There should be some articles on posting mouse messages, some where.
I do not know if any of that helps, but my times up.
Good Luck!
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
i want to test how to change icon of an exe file with an icon file by function Updateresource() - do you know where i can find sample of this kind?
thx
includeh10
|
|
|
|
|
This question is to general. It sounds like you are talking about changing an existing exe. It might be difficult to figure out how to change the icon resource of an existing file, but once known it should be easy provide the replacement and origanal are the same size.
Interesting: But, don't know how.
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
Try here[^].
Software Zen: delete this;
|
|
|
|
|
Hi,
I have done a project and it was working fine all these days. Finally I had to add one more function so I created a new toolbar and attached the function to it,but when I run, this new toolbar button is not showing up. I also added this item in the menu bar....after running it is not showing up....any idea why is it acting like this??? Please do help, i have a deadline. I am sure it is nothing to do with the project, so I was thinking is this something to do with the system files of vc++.
Thanks a lot
|
|
|
|
|
ashsri wrote:
I am sure it is nothing to do with the project, so I was thinking is this something to do with the system files of vc++.
Wrong!
I've been there too many times, always look at the project (source files) first.
(MFC 6.0 has a few minor (ligitimate) memory leaks).
-----------------------------------------------------------------------------
You can not attach a message function to a toolbar. A message function is
associated with a message, nothing more.
-----------------------------------------------------------------------------
What do you mean you created a new toolbar?
If that is actualy what you did, then you have to write code to switch to the new toolbar (or it will not show up), so you can use it.
Now I get the impression that you wanted to but did not add it to the existing toolbar.
Question: Did you create a new menu-bar the same way you created a new toolbar?
If you did, then you have the same problem in both places.
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
Hi,
I mean, I added few more items on the menu and the toolbar, to the one already existing. but it is not showing up when I run. I need not add any code, as I have already done it. So what I did was, just add or more items for the menubar and toolbar,and it is not showing up...thanks a lot for the help you are doing.
thanks
|
|
|
|
|
Hi all,
I'm using VS 6.0 to try and explicitly load a DLL. However no
matter what I've tried GetProcessAddress() always returns NULL.
LoadLibrary works fine (not NULL anyway). I've checked the exported
name from the DLL via 'depends.exe' and it's not mangled: it shows
the same name ("fnTestDLL").
I've googled for a couple of hours with no luck, so I'm
out of ideas. Maybe it's my project settings? I've tried
including the testdll.lib in "Link", the testdll.h in testp.app
and the DLL in "Debug" all with no luck.
I've dumbed-down my code to the basics and still can't get it
to work.
Any help would be greatly appreciated!!
------------------------------------------------------------
This is my App's "testapp.cpp" that (tries to) call my DLL:
------------------------------------------------------------
#include "stdafx.h"
#include "testapp.h"
#ifdef _DEBUG_
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CWinApp theApp;
using namespace std;
typedef int (CALLBACK* LPFNDLLFUNC1)();
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{ .............. }
LPFNDLLFUNC1 LibMainEntryPoint;
HINSTANCE LoadMe = LoadLibrary("c:\\code\\begin\\testdll\\debug\\testdll.dll");
if (LoadMe == NULL)
{ ............. }
LibMainEntryPoint = (LPFNDLLFUNC1) GetProcAddress(LoadMe, "fnTestDLL");
if (LibMainEntryPoint == NULL)
{ ....ALWAYS FAILS HERE!!!...........}
LibMainEntryPoint();
FreeLibrary(LoadMe);
return (0);
}
----------------------------------------
This is my DLL's "testdll.cpp":
----------------------------------------
#include "stdafx.h"
#include "testdll.h"
using namespace std;
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason, LPVOID lpreserved)
{
switch (ul_reason):
{
default: break;
}
return (TRUE);
}
TESTDLL_API int nTestDLL = 0;
TESTDLL_API int fnTestDLL(void)
{
return (42);
}
CTestDLL::CTestDLL()
{
cerr << "constructed";
}
CTestDLL::~CTestDLL()
{
cerr << "destructed";
}
-----------------------------------------------------
This is the DLL's "testdll.h":
-----------------------------------------------------
#ifdef TESTDLL_EXPORTS
#define TESTDLL_API __declspec(dllexport)
#else
#define TESTDLL_API __declspec(dllimport)
#endif
class TESTDLL_API CTestDLL
{
public: CTestDLL(void);
~CTestDLL(void);
};
extern TESTDLL_API int nTestDLL;
TESTDLL_API int fnTestDLL(void);
|
|
|
|
|
'Depends' has a 'C++' button in the toolbar. Make sure it's not pressed. A sure sign that the name is in fact mangled is if the full signature appears in the list, i.e. int fnTestDLL(void) .
Since you haven't wrapped the testdll.h contents with an extern "C" block, I suspect that the names are in fact mangled. You can perform an additional check using dumpbin /exports , which doesn't perform any translation.
You can fix this either by prefixing the 'flat' exports with extern "C" , or by using a module-definition file[^]. The latter is useful if you want to define a different external name from the name in the code, or if you want to specify the ordinal.
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
Thanks for the quick response. I don't see the C++ button you're talking about, but I'm using "Dependency Walker" version 1.0. It reports the name "?fnTestDLL@@YAHXZ" with entry point 0x00001082, which looks relatively correct to me, but I'm somewhat new to Windows DLLs, so maybe it IS mangled?
I'd prefer to stay away from EXP files if possible, simply because it should work without them.
|
|
|
|
|
The "?fnTestDLL@@YAHXZ" is the mangled name. The "@@YAHXZ" is the 'mangled' portion, and identifies the argument signature for the function.
Software Zen: delete this;
|
|
|
|
|