从线程运行对话框类函数 [英] Run dialog class function from a thread

查看:77
本文介绍了从线程运行对话框类函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从线程执行对话类函数?

例如

How to execute a dialogue class function from a thread?

For example

		static DWORD WINAPI setBitThread(LPVOID lpParam) 
{	
	CUMKurunegalaDlg::OnCbnEditchangePtype(); /*This line give me an error that tell "Non-static member reference must be relative to a specific object */
    return 0; 
} 

推荐答案

我不久前在留言板上回答了这样的问题.有人想从线程弹出一个消息框.这是我的回答:
http://www.codeproject.com/Messages/4333885/Re-Messagebox.aspx [ ^ ]
在您的情况下,您应该在窗口的WM_USER + XXX消息处理程序中执行对话框类函数(通常是gui操作代码),其他人将在其中显示消息框.

这是对其他内容的另一个简短答案,如果您使用的是MFC,则非常重要:
http://www.codeproject.com/Messages/4334909/Re-Messagebox.aspx [ ^ ]

实际上,您有两个问题:您不知道如何在对话框中触发该动作,另一个是您正在尝试从非gui线程执行此操作. nv3在另一个解决方案中已经描述了第一个问题.
I answered a question like this not too long ago on a message board. Someone else wanted to pop up a messagebox from a thread. Here is my answer to that:
http://www.codeproject.com/Messages/4333885/Re-Messagebox.aspx[^]
In your case you should execute your dialog class function (generally your gui manipulator code) in the WM_USER+XXX message handler of your window, where the other guy show the messagebox.

Here is another short answer to something else, very important if you are using MFC:
http://www.codeproject.com/Messages/4334909/Re-Messagebox.aspx[^]

Actually you have two problems: you don''t know how to trigger that action in your dialog, another is that you are trying to do that from a non-gui thread. The first problem is already described by nv3 in another solution.


您的问题对我来说还不清楚.但是似乎您正在尝试从静态成员函数中调用非静态(即正常)成员函数.要调用非静态成员函数,您需要此类的对象才能将该函数应用到该成员.例如,当您致电

Your question is unclear to me. But it seems that you are trying to call a non-static (i.e. normal) member function from a static member function. To call a non-static member function you need an object of this class to apply the function to. For example, when you call

pObj->MyFunction ();



然后将MyFunction应用于pObj指向的对象.然后在MyFunction内部,this 指针指向该对象.

this 指针的好处是无需使用指针即可寻址类的其他成员变量或成员函数.例如,在MyFunction内部,对以下两个语句执行相同的操作:



then MyFunction is applied to the object that pObj points to. Inside MyFunction the this pointer then points to that very object.

The nice thing about a this pointer is that you address other member variables or member functions of a class without using a pointer. For example inside MyFunction to following two statements do the same:

this->m_counter = 0;
m_counter = 0; // does the same



或另一个示例:



or another example:

this->OtherFunctionOfThatClass();
OtherFunctionOfThatClass(); // does the same



现在,静态成员函数有所不同,因为它们不需要也没有此指针.这意味着尽管您没有将对象应用于的对象,但您仍可以从班级外部调用它们.

在静态成员函数内部,您不能在未明确告知要应用该函数的对象的情况下调用类的非静态成员函数,因为您的非静态成员函数没有此指针.

如果要从静态成员函数中调用非静态成员,则必须提供一个对象作为指针或引用:



Now, static member functions are different in that they don''t need and don''t have this pointer. That means you can call them from outside your class although you don''t have an object to apply the to.

Inside a static member function you cannot call a non-static member function of your class without explicitly telling the object you want to apply the function to, because your non-static member function has not this pointer.

If you want to call a non-static member from a static member function you must supply an object as a pointer or a reference:

void MyClass::MyStaticMemberFunction()
{
    MyNonStaticMemberFunction(); // this is an error
    ...
    MyClass* pOtherObj = ...
    pOtherObj->MyNonStaticMemberFunction(); // that will work
}


这篇关于从线程运行对话框类函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆