0

下面给出的是使用WorkingBlock函数的代码。该应用程序使用 C# 3.0。

方法代码运行良好,WorkingBlock除非在某些情况下应用程序冻结。

最初,该WorkingBlock方法是直接在CallerFunction. 建立了以下结构来缓解应用程序冻结的问题。

但是,即使在此之后,控件仍将存在于 的WorkingBlock方法中,DoAction Thread并且应用程序仍然会卡住。我什至尝试设置DoAction.IsBackgroundtrue但没有任何效果。

有没有办法强制中止,DoAction Thread这样应用程序就不会卡住?控件应该完全离开该CallerFunction方法。

private void CallerFunction(Record record)
{
    //some work done
    bool SeeResult = CalledFunction(record);
} 

private bool CalledFunction(Record record)
{
    Waithandles CheckWaithandles = new Waithandle [] {new ManualResetEvent(false), new ManualResetEvent(false)};

    Thread TimeOutCheck = new Thread(() => {
        //Has a StopWatch to check timeout. Thread will stop once StopWatch has reached an Elapsed Limit 
        (ManualResetEvent)CheckWaithandles[1]).Set();
    });

    Thread DoAction = new Thread(() => {
        WorkingBlock(record);
        (ManualResetEvent)CheckWaithandles[0]).Set();
    });

    TimeOutCheck.Start();
    DoAction.Start();

    int WaitHandleCalled = WaitHandle.WaitAny(CheckWaithandles);
    if(WaitHandleCalled == 0)
        return true;
    else
    {
        return false;
        DoAction.Abort();
    }
}
4

0 回答 0