5-8万年薪顶级嵌入式,京沪深就业地 浅谈并行编程中的任务分解模式
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  进程/线程/DLL

在DLL中能使用线程吗,要怎么做才行?在线等待....

楼主linfeng()2004-08-18 14:10:28 在 VC/MFC / 进程/线程/DLL 提问

我在DLL程序中:  
  CCxComm   *m_Com;  
   
  BOOL   CCx_App::InitInstance()  
  {  
  CWinApp::InitInstance();  
  m_Com=new   CCxComm();  
  return   TRUE;  
  }  
  int   CCx_App::ExitInstance()    
  {  
  if   (   m_Com   )  
  m_Com->StopMonitor();    
  delete   m_Com;  
  return   CWinApp::ExitInstance();  
  }  
  其中类CCxComm中使用了线程,在2003XP中没有什么问题,但是在win   98中系统退出时,我在类CCxComm中运行的线程没有退出,请问我怎么做才行? 问题点数:50、回复次数:10Top

1 楼linfeng()回复于 2004-08-18 14:19:08 得分 0

自己upTop

2 楼huwei001982(凶猛的小狗)回复于 2004-08-18 14:39:11 得分 0

那要看你自已写的线程代码有没有正常退出Top

3 楼oyljerry(【勇敢的心】→ ㊣提拉米苏√㊣)回复于 2004-08-18 14:40:01 得分 0

是不是98里缺少什么东西   啊?Top

4 楼linfeng()回复于 2004-08-18 14:53:00 得分 0

在XP中都正常的,如果不用DLL,CXComm类在win98中也运行正常的。Top

5 楼jiangsheng(蒋晟.Net[MVP])回复于 2004-08-19 23:08:03 得分 50

For   Regular   DLLs,   which   have   a   CWinApp-derived   object,   the   CWinApp::InitInstance   override   is   called   from   MFC's   supplied   DllMain   when   a   process   is   attaching   to   the   DLL.   That   is,   DllMain   is   entered   with   a   Reason   For   Call   of   DLL_PROCESS_ATTACH,   and   in   handling   this,   MFC   calls   in   to   InitInstance   before   cleaning   up   and   leaving   DllMain.    
   
  For   Extension   DLLs,   the   startup   of   the   DLL   is   the   same,   except   Extension   DLLs   do   not   have   a   CWinApp-derived   object   and   therefore   have   no   InitInstance.    
   
  Whenever   new   threads   are   created   that   use   code   in   the   DLL,   DllMain   is   called   with   a   Reason   For   Call   of   DLL_THREAD_ATTACH   to   announce   to   the   DLL   that   a   new   thread   is   attaching   to   it.   If   a   new   thread   is   created   in   the   InitInstance   of   an   MFC   Regular   DLL   or   in   the   DllMain   of   any   MFC   DLL   during   DLL_PROCESS_ATTACH,   this   second   thread   will   attempt   to   re-enter   the   DllMain,   which   has   not   yet   been   exited   from   in   an   effort   to   announce   DLL_THREAD_ATTACH.    
   
  DllMain,   however,   is   not   re-entrant.   That   means   that   the   second   thread   will   not   start   executing   until   the   initial   creating   thread   has   finished   its   work   in   InitInstance,   returned   to   and   left   DllMain.    
   
  In   versions   of   MFC   included   with   32-bit   Visual   C++   versions   2.2   and   earlier,   MFC   allowed   threads   to   be   created   during   startup,   and   usually   DLLs   that   did   this   would   work   acceptably.   The   second   thread   would   get   created   without   problem   but   would   not   start   executing   until   the   first   thread   left   DllMain.   However,   it   has   never   been   a   good   idea   to   create   threads   in   DllMain   during   DLL_PROCESS_ATTACH.    
   
  As   of   MFC   4.0,   the   CWinThread::CreateThread   function,   which   is   called   by   AfxBeginThread,   now   waits   on   the   created   thread   to   start   up   and   initialize   MFC   specific   data   before   it   returns   control   to   the   calling   thread.   Because   the   second   thread   is   waiting   on   the   first   thread   to   leave   DllMain,   both   threads   crash   headlong   into   deadlock   and   the   application   hangs.    
   
  Note   that   this   information   is   equally   valid   for   the   RawDllMain   function.   Moreover,   MFC   DLLs   should   not   use   a   RawDllMain   function   at   all.    
   
  Regular   DLLs   that   create   threads   should   only   do   so   in   functions   exported   from   the   DLL   and   called   by   client   applications.   Furthermore,   no   MFC   DLL   --   neither   Extension   nor   Regular   --   should   create   an   MFC   thread   in   the   DllMain   or   RawDllMain   function.   This   ensures   that   the   thread   will   not   be   created   in   the   middle   of   any   critical   startup   code.    
   
  The   recommended   solution   for   MFC   DLLs   that   need   to   create   a   thread   when   the   DLL   starts   is   to   add   a   specific   exported   initialization   function   and   create   the   thread   in   it.   Applications   that   use   the   DLL   would   need   to   call   this   function   sometime   during   startup,   most   likely   during   the   application's   InitInstance   if   it   uses   MFC.   Or,   if   the   application   is   loading   the   DLL   explicitly,   the   application   should   call   the   initialization   function   immediately   after   the   call   to   load   the   library.    
   
  The   practice   of   exporting   an   initialization   function   for   a   DLL   is   not   uncommon.   Nevertheless,   there   may   be   situations   where   DLLs   created   with   earlier   versions   of   MFC   are   being   ported   but   the   client   application   cannot   be   changed   to   include   a   call   to   an   initialization   function.   The   alternative   to   an   initialization   function   is   to   create   the   thread   in   one   of   the   pre-   existing   exported   functions.   Any   of   the   DLL's   exported   functions   that   require   a   running   thread   should   be   responsible   for   first   checking   to   see   if   that   thread   exists   and   then   creating   it   if   it   does   not.    
   
  Top

6 楼linfeng()回复于 2004-08-24 11:32:11 得分 0

upTop

7 楼linfeng()回复于 2004-10-30 15:02:22 得分 0

upTop

8 楼huangxiaoke2000(小小)回复于 2004-10-30 19:27:57 得分 0

UpTop

9 楼linfeng()回复于 2004-11-10 13:48:38 得分 0

upTop

10 楼gg137zeus(分在脚下)回复于 2004-11-10 13:53:18 得分 0

关注Top

相关问题

  • 线程等待消息??
  • 线程等待问题
  • DLL&线程问题
  • 多线程和DLL
  • 求助:主线程中用WaitForSingleObject()等待子线程,等不到线程退出
  • 主线程怎么等待辅助线程呢?(立刻给分!!!)
  • 线程同步问题(在线等待)
  • !!!!!在线等待,线程问题…………
  • 线程的作用域(在线等待)
  • 多线程的问题???(在线等待)

关键词

  • mfc
  • dll
  • application
  • dllmain
  • ccxcomm
  • initinstance
  • thread
  • rawdllmain
  • cwinapp
  • attach

得分解答快速导航

  • 帖主:linfeng
  • jiangsheng

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
x 提问