sundonga
sundonga
2015-02-09 09:09
采纳率: 83.3%
浏览 5.3k

C#程序关闭,进程还在运行

写了一个C#的程序,调用了一个C++的dll,在C++的DLLMain中下面这样写的,特意在DLL_PROCESS_DETACH中关闭了线程,但那个MessageBox在关闭C#程序时并没有弹出来,是不是意味着我所有在dll中开的线程都没关闭?为什么会这样呢?


BOOL APIENTRY DllMain( HMODULE hModule,             // handle to DLL module
                       DWORD  ul_reason_for_call,   // reason for calling function
                       LPVOID lpReserved            // reserved
                     )
{
    // Perform actions based on the reason for calling.
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:        // Initialize once for each new process.Return FALSE to fail DLL load.
        {

            SetDllNCCallback(rtm_InterpStartEnd);
            SendNCDriverUserDecodeEvent();

            pctr_dataShm = (LPCTRDATA)malloc(sizeof(Shmctr_data));//初始化全局控制变量
            if(pctr_dataShm == NULL)
            {
                MessageBox(NULL,TEXT("can not alloc heapmemory in pctr_dataShm"),TEXT("Interpolation Error"),NULL);
                return 1;
            }

            hrtm_PositCtrl_Trd = CreateThread(NULL, 0, rtm_PositCtrl_Trd, NULL,CREATE_SUSPENDED, NULL);
            SetThreadPriority(hrtm_PositCtrl_Trd, THREAD_PRIORITY_TIME_CRITICAL);
            ResumeThread(hrtm_PositCtrl_Trd);

            hrtm_VelCtrl_Trd = CreateThread(NULL, 0, rtm_VelCtrl_Trd, NULL,CREATE_SUSPENDED, NULL);
            SetThreadPriority(hrtm_VelCtrl_Trd, THREAD_PRIORITY_HIGHEST);
            ResumeThread(hrtm_VelCtrl_Trd);

            hrtm_DecodFifo_Trd = CreateThread(NULL, 0, rtm_DecodFifo_Trd, NULL,CREATE_SUSPENDED, NULL);
            SetThreadPriority(hrtm_DecodFifo_Trd, THREAD_PRIORITY_ABOVE_NORMAL);
            ResumeThread(hrtm_DecodFifo_Trd);

            hrtm_Intrp_Trd = CreateThread(NULL, 0, rtm_Intrp_Trd, NULL,CREATE_SUSPENDED, NULL);
            SetThreadPriority(hrtm_Intrp_Trd, THREAD_PRIORITY_NORMAL);
            ResumeThread(hrtm_Intrp_Trd);

            hrtm_Prd_2ms = CreateThread(NULL,0,rtm_Prd_2ms,0,0,NULL);

            break;
        }

    case DLL_THREAD_ATTACH:
        //MessageBox(NULL,TEXT("Enter DLL_THREAD_ATTACH"),TEXT("Interpolation Inform"),NULL);
        break;
    case DLL_THREAD_DETACH:
        //MessageBox(NULL,TEXT("Enter DLL_THREAD_DETACH"),TEXT("Interpolation Inform"),NULL);
        break;
    case DLL_PROCESS_DETACH:
        CloseHandle(hrtm_PositCtrl_Trd);

        free(pctr_dataShm); //释放全局控制量
        pctr_dataShm = NULL;
        CloseHandle(hrtm_PositCtrl_Trd);
        CloseHandle(hrtm_VelCtrl_Trd);
        CloseHandle(hrtm_DecodFifo_Trd);
        CloseHandle(hrtm_Intrp_Trd);
        CloseHandle(hrtm_Prd_2ms);

        MessageBox(NULL,TEXT("Leave Interpolation dll"),TEXT("Interpolation Inform"),NULL);
        break;
    }
    return TRUE;
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • devmiao
    devmiao 2015-02-09 10:55
    已采纳


    Process.GetCurrentProcess().Kill();
    关闭下看看

    点赞 评论
  • sundonga
    sundonga 2015-02-09 09:11

    另外,我在dll中开的线程都是while(1)循环的,会不会有影响?

    点赞 评论
  • oyljerry
    oyljerry 2015-02-09 10:05

    dll中的线程要退出,while循环要有一个标志位
    此外DLL_PROCESS_ATTACH 中不要做太复杂的操作,不然容易造成死锁等。

    点赞 评论
  • lgfcs
    lgfcs 2015-02-10 01:32

    试试看GC.Clollect()

    点赞 评论
  • qq_32190559
    Csharp爱好者007 2015-12-23 10:35

    应该是有线程没有关闭造成的

    点赞 评论

相关推荐