gpt4 book ai didi

c# - Timer Elapsed Event 不执行所有操作

转载 作者:太空宇宙 更新时间:2023-11-03 11:09:45 27 4
gpt4 key购买 nike

我有一个 Backgroundworker,它从数据库中收集大量数据。我想在线程上设置一个超时限制,这样如果它在设定的时间后没有返回结果,进程就会被取消。

我在启动 BackgroundWorker 的同时在主线程上启动了一个计时器。

如果我故意让 BGW 休眠,计时器就会计时结束并调用 .Elapsed 事件。然后它取消了后台工作人员,但是它没有成功完成我希望它完成的其他操作,这些操作是更新 GUI 上的状态栏并抛出 MessageBox。我不明白为什么不行,有人可以帮忙吗?

超时和 sleep 是为了测试特意设置的。

        /// <summary>
/// loads Assets by group ID
/// Populates Grid on asset listing page
/// </summary>
/// <param name="groupId"></param>
/// <param name="user"></param>
internal void PopulateGridAssetsByGroup(int groupId, User user)
{
//update statusbar
AfMainWindow.MainWindow.UpdateStatusBar("Loading Assets...");

//setup BG worker
populateGridAssetsWorker = new BackgroundWorker {WorkerSupportsCancellation = true, WorkerReportsProgress = false};
populateGridAssetsWorker.DoWork += populateGridAssetsWorker_DoWork;
populateGridAssetsWorker.RunWorkerCompleted += populateGridAssetsWorker_RunWorkerCompleted;

//setup timer which will cancel the background worker if it runs too long
cancelTimer = new Timer {Interval = 2000, Enabled = true, AutoReset = false};
cancelTimer.Elapsed += cancelTimer_Elapsed;
cancelTimer.Start();

populateGridAssetsWorker.RunWorkerAsync(groupId); //start bg worker
}


void cancelTimer_Elapsed(object sender, ElapsedEventArgs e)
{
populateGridAssetsWorker.CancelAsync();
cancelTimer.Stop();

AfMainWindow.MainWindow.UpdateStatusBar("Could not load assets, timeout error");

MessageBox.Show("Operation Timed Out\n\nThe Server did not respond quick enough, please try again",
"Timeout", MessageBoxButton.OK, MessageBoxImage.Exclamation, MessageBoxResult.OK);

AfMainWindow.MainWindow.Busy.IsBusy = false;

}

/// <summary>
/// when bg worker complete, update the AssetGrid on Asset Listing Page with results
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void populateGridAssetsWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{

cancelTimer.Stop();

//my thread complete processing

}
/// <summary>
/// Perform the DB query and collect results for asset listing
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void populateGridAssetsWorker_DoWork(object sender, DoWorkEventArgs e)
{
var assetListByGroup = new List<AssetLinked>();

Thread.Sleep(5000);

if (populateGridAssetsWorker.CancellationPending)
{
return;
}

try
{

//My SQL actions

if (populateGridAssetsWorker.CancellationPending)
{
return;
}
}
catch (Exception ex)
{
Globals.AFWideSettings.GeneralErrorMessage(ex.Message);
}
}

最佳答案

假设当您尝试从另一个线程更新 GUI 时,您的cancelTimer_Elapsed 方法无提示地崩溃了。如果不将 Visual Studio 设置为中断所有异常进行调试,有时很难找到在后台线程中抛出的异常 - 您可以在菜单 Debug > Exceptions 中进行设置并检查所有 CLR 异常。或者,您可以将方法主体包装在 try block 中,并将断点放在 catch block 中。

void cancelTimer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
populateGridAssetsWorker.CancelAsync();
cancelTimer.Stop();

AfMainWindow.MainWindow.UpdateStatusBar("Could not load assets, timeout error");

MessageBox.Show("Operation Timed Out\n\nThe Server did not respond quick enough, please try again",
"Timeout", MessageBoxButton.OK, MessageBoxImage.Exclamation, MessageBoxResult.OK);

AfMainWindow.MainWindow.Busy.IsBusy = false;
}
catch(Exception ex)
{
int breakpoint = 42;
}
}

我不确定情况是否如此,但我认为值得一试。

关于c# - Timer Elapsed Event 不执行所有操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14511937/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com