在VS2005中的Dialog类MFC应用程序中创建一个线程 [英] Creating a thread in Dialog Class MFC application in VS2005

查看:82
本文介绍了在VS2005中的Dialog类MFC应用程序中创建一个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我创建的线程例程正在执行,但永远不会返回,所以我的程序卡在我刚刚调用的线程中。

My problem is that the thread routine that I created is executing, but never returns so my program gets stuck in the thread I just called.

从长远来看,什么我正在尝试做的是创建一个打开网络套接字并监听接收数据的线程。

In the long run what I'm trying to do is create a thread that opens a network socket and listens to receive data.

我之前创建过线程并且它们工作正常......例如:

I have created threads before and they work fine...for example:

DWORD WINAPI Edas2015WorkThread(LPVOID lpThreadParameter)

{

  BOOL  exitProgram = FALSE;

  while(!exitProgram)

  {

   MessageBox(NULL,_T("OK to here ... 1"),_ T(" Edas2015WorkThread"),MB_OK);

   Sleep(2000);

 }

  return 0;

}

DWORD WINAPI Edas2015WorkThread( LPVOID lpThreadParameter )
{
 BOOL exitProgram = FALSE;
 while( !exitProgram )
 {
  MessageBox(NULL, _T("OK to here...1"), _T("Edas2015WorkThread"), MB_OK);
  Sleep(2000);
 }
 return 0;
}

HANDLE CreateEdas2015WorkThread(DWORD& threadID)

{

  MessageBox(NULL,_T ("OK to here ... 1"),_ T(" CreateEdas2015WorkThread"),MB_OK);

  HANDLE  threadHandle = CreateThread(NULL,0,

     Edas2015WorkThread,0,0,

    & edas2015WorkthreadID);

  MessageBox(NULL,_T("OK to here ... 2"),_ T(" CreateEdas2015WorkThread"),MB_OK);

  return threadHandle;

}

HANDLE CreateEdas2015WorkThread(DWORD &threadID )
{
 MessageBox(NULL, _T("OK to here...1"), _T("CreateEdas2015WorkThread"), MB_OK);
 HANDLE threadHandle = CreateThread( NULL, 0,
    Edas2015WorkThread, 0, 0,
    &edas2015WorkthreadID );
 MessageBox(NULL, _T("OK to here...2"), _T("CreateEdas2015WorkThread"), MB_OK);
 return threadHandle;
}

创建线程的例程是:

  Edas2015Workthread = CreateEdas2015WorkThread(WorkthreadID);

 Edas2015Workthread = CreateEdas2015WorkThread(WorkthreadID );

==========================================

以上例程正常。线程被创建并返回,在这种情况下只是永远循环...这是

==========================================
The above routine works fine. The thread is created and returns, and in this case just loops forever...which is

正是我现在想要它做的。

exactly what I want it to do for now.

==============================

==============================

但我有什么一个预先存在的VS2005 MFC基于对话框的MFC应用程序(参见附图显示它是如何构建的)

BUT what I have an preexisting VS2005 MFC Dialog Based MFC Application(See attached drawing showing how it is contructed)

如果我插入上面的例程它会编译并运行,但我无法访问任何变量在

If I insert the above routines it all compiles and works, but I can't access any variables from within the

CreateEdas2015WorkThread()线程中。

CreateEdas2015WorkThread() thread.

我猜这是因为我创建的线程不是成员的一部分什么类(?)所有的变量

都已定义在......也许?

I am guessing this is because the thread I created is not part of a member of what ever class(?) all the variables
have been defined in...maybe?

所以我有一个问题是: 如何获取上述线程中可访问的程序中的变量定义?
b $ b我猜测/希望如果它们可以被识别,那么事情应该可以正常工作。

===============================

So one question I have is:  How do I get the variables defines in the program accessible in the above thread?
I'm guessing/hoping if they can be recognized then things should work ok.
===============================

无论如何,我创建了一对新的线程例程,它们是Class的成员(?)

例如:

In any case, I create a new pair of thread routines that is a member of the Class(?)
For example:

LPTHREAD_START_ROUTINE CPC_SecuSearch_FdxDKProDlg :: ServerWorkThread( )b
{

  BOOL  exitProgram = FALSE;

  while(!exitProgram)

  ; {

   MessageBox(_T("OK to here ... 1"),_ T(" ServerWorkThread"),MB_OK);

  ; 睡眠(2000);

 }

 返回0;

}

LPTHREAD_START_ROUTINE CPC_SecuSearch_FdxDKProDlg::ServerWorkThread()
{
 BOOL exitProgram = FALSE;
 while(!exitProgram)
 {
  MessageBox(_T("OK to here...1"), _T("ServerWorkThread"), MB_OK);
  Sleep(2000);
 }
 return 0;
}

HANDLE CPC_SecuSearch_FdxDKProDlg :: CreateServerWorkThread()

{

  MessageBox(_T("OK to here ... 1"),_ T(" CreateServerWorkThread") ),MB_OK);

  HANDLE  threadHandle = CreateThread(NULL,0,

     ServerWorkThread(),0,0,

     NULL);
$
  MessageBox(_T("OK to here ... 2"),_ T(" CreateServerWorkT) hread"),MB_OK);

  return threadHandle;

}

HANDLE CPC_SecuSearch_FdxDKProDlg::CreateServerWorkThread()
{
 MessageBox(_T("OK to here...1"), _T("CreateServerWorkThread"), MB_OK);
 HANDLE threadHandle = CreateThread( NULL, 0,
    ServerWorkThread(), 0, 0,
    NULL);
 MessageBox(_T("OK to here...2"), _T("CreateServerWorkThread"), MB_OK);
 return threadHandle;
}



ServerWorkthread = CreateServerWorkThread(WorkthreadID);


ServerWorkthread = CreateServerWorkThread(WorkthreadID );

==============================

上面的线程也没有错误地运行并且运行,但是线程永远不会返回并且程序在ServerWorkThread()线程例程中永远停留在

==============================
The above thread also complies without error and runs, BUT the thread never returns and the program stays stuck

我看到:
$
 " MessageBox(_T("OK to here ... 1"),_ T("CreateServerWorkThread"),MB_OK );< b $ b但它永远不会返回并显示

 " MessageBox(_T("OK to here ... 2"),_ T(" ; CreateServerWorkThread"),MB_OK);"

I see the:
 "MessageBox(_T("OK to here...1"), _T("CreateServerWorkThread"), MB_OK);"
but it never returns back and displays
 "MessageBox(_T("OK to here...2"), _T("CreateServerWorkThread"), MB_OK);"

程序刚停留在"ServerWorkthread = CreateServerWorkThread();"例行公事。

The program just gets stuck at the "ServerWorkthread = CreateServerWorkThread();" routine.

=============================

=============================

所以我的问题是如何让这个线程例程返回并继续执行程序的其余部分?

So my question is how do I get this thread routine to return and continue on with the rest of the program?

在两者之间的'CreateThread'例程中,为什么一个只需将'Edas2015WorkThread'作为子程序的名称,另一个需要'ServerWorkThread()'。或者就此而言,我必须做出所有的改变才能获得
来获得编译的时间?

In the 'CreateThread' routine between the two, why does one take just 'Edas2015WorkThread' as the name of the Subroutine, and the other requires 'ServerWorkThread()'. Or for that matter all the changes I had to make
to get the routime to compile?

任何人都可以帮助我吗?

Can anyone help me out?

有没有办法让我上传简单​​的VS2005项目来证明这个问题?...谢谢。

Is there any way for me tro upload the simple VS2005 project that demonstrates this problem also?...Thanks.

谢谢; $
=============================

Thanks;
=============================

推荐答案

这不是托管代码问题。 ..

This isn't really a managed code question...

关于问题,请检查您的代码:

As for the problem, check your code:

HANDLE  threadHandle = CreateThread(NULL,0, 

     ServerWorkThread(),0,0,

     NULL);

请注意,您已经编写了"ServerWorkThread()"。这是做什么的?它* 调用 * ServerWorkThread()和
将返回值传递给CreateThread。所以你永远不会调用CreateThread,因为它的一个参数是一个永不返回的函数调用的结果。 
在没有()的情况下尝试

Note that you've written "ServerWorkThread()". What does that do? It *calls* ServerWorkThread() and passes the return value to CreateThread. So you've never getting to the call to CreateThread because one of its parameters is the result of a function call that never returns. Try without the ().

Paul T。


这篇关于在VS2005中的Dialog类MFC应用程序中创建一个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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