090904项目进展:强制终止线程

一、添加控制遍历深度的Edit控件

1GUI控件,添加变量,限制取值范围。

 

2,初始化

 

3,传给进行搜索的工作线程

 

 

二、添加停止线程的方法

虽然前期也做过不少的效率改进:先ping在线,再检测SMB,速度已经比直接检测SMB快十倍以上,但是大IP段遍历的时候提供一个终止的功能还是很有得上的。

1,变换button的表现,添加状态开关。

 

2,停止线程的功能函数

方案一:使用AfxEndThread

正常退出就不说了,直接返回函数

For a worker thread, normal thread termination is simple: Exit the controlling function and return a value that signifies the reason for termination. You can use either the AfxEndThread function or a return statement. Typically, 0 signifies successful completion, but that is up to you.

 

现在的需求是强制退出,AfxEndThread必须在线程(线程函数)内部,内部怎么知道外部的变化呢?加一个全局的变量,每当检查一个新IP时,先看看这个global Flag是不是已经要求停止了。

Premature Thread Termination

Terminating a thread prematurely is almost as simple: Call AfxEndThread from within the thread. Pass the desired exit code as the only parameter.

AfxEndThread must be called from within the thread to be terminated. If you want to terminate a thread from another thread, you must set up a communication method between the two threads.

 

方案二:函数TerminateThread

BOOL TerminateThread(

  HANDLE hThread,

  DWORD dwExitCode

);

TerminateThread is a dangerous function that should only be used in the most extreme cases. You should call TerminateThread only if you know exactly what the target thread is doing, and you control all of the code that the target thread could possibly be running at the time of the termination

显然不应该作为首选。

 

方案三:set up a communication method

需要查找这个方法

 

方案四:改造成线程类再使用AfxBeginThread调用

CWinThread* AfxBeginThread(

   CRuntimeClass* pThreadClass,

   int nPriority = THREAD_PRIORITY_NORMAL,

   UINT nStackSize = 0,

   DWORD dwCreateFlags = 0,

   LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL

);

变成类调用后,参考条目“三、使用线程类begin一个线程时,这样退出”

 

3,加入开关逻辑

void CDlgSmbList::OnButtonSearch()

{

       // TODO: Add your control notification handler code here

    if (bSearching)

    {

        bSearching = FALSE;

        m_btnSearch.SetWindowText("Search");

        // 正在查找,想要暂停

        // 停止线程

    }

    else

    {

        bSearching = TRUE;

        m_btnSearch.SetWindowText("STOP");

        // 开始查找

           UpdateData(TRUE);

 

           tListSmb.pm_addrFrom = &m_addrFrom;

           tListSmb.pm_addrTo = &m_addrTo;

        tListSmb.pm_smbTree = &m_smbTree;

        tListSmb.pm_Edit = &m_editCurIp;

        tListSmb.bLevel = m_bLevel;

 

        pTrdConnect = AfxBeginThread(ThreadFuncListSmbRecource, &tListSmb);

 

        UpdateWindow();

        m_smbTree.UpdateWindow();

    }

}

 

三、使用线程类begin一个线程时,这样退出

线程类停止的方法:

1CwinThread   *   pThread   =   AfxBeginThread(RUNTIME_CLASS(YourClass));  

        //启动CwindThread派生类一般如此。  

  2、在线程类中定义消息  

        #define   WM_STOPTHREAD   USER+100;  

        BEGIN_MESSAGE_MAP()  

        //  

        //  

        ON_THREAD_MESSAGE(WM_STOPTHREAD,OnStopThread)  

        END_MESSAGE_MAP()  

  3、编写函数:  

        ....::OnStopThread()  

        {           ::PostQuitMessage();  

                    return   0;  

          }  

  4、最后,终止线程只需如下:  

        pThread->PostThreadMessage(WM_STOPTHREAD,0,0);

明天继续。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值