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;
}


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

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

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

试试看GC.Clollect()

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