简体   繁体   English

如果花费太长时间,则停止线程

[英]Stop a thread if it takes too long

I am new in Windows Phone development and I am trying to create a windows phone app using C# 我是Windows Phone开发的新手,我正在尝试使用C#创建Windows Phone应用程序

Thread t = new Thread(doaheavywork);
t.Start();

if (!t.Join(1000)) // give the operation 1s to complete
{
    t.Abort();
}

I cannot alter the doaheavywork function. 我无法更改doaheavywork功能。

I just need the result to be omputed within 1 or 2 seconds, since sometimes it may run for very long time. 我只需要在1或2秒钟内将结果删除即可,因为有时它可能会运行很长时间。

I have read using abort is the wrong way. 我读过使用中止是错误的方法。

I am using the above in a PhotoChooserTask complete function. 我在PhotoChooserTask完整功能中使用以上功能。 First run executes fine. 第一次运行执行正常。 That is, when I click the button to select a photo, the doaheavywork function doesn't exceed 1 sec. 也就是说,当我单击按钮选择照片时,doaheavywork功能不会超过1秒。 But if I try for the second time, the PhotoChooserTask just throws an exception and stops. 但是,如果我第二次尝试,则PhotoChooserTask只会引发异常并停止。

Is it because I aborted the process in 1st run? 是因为我在第一次运行中中止了流程吗? Or anything else? 还是其他? Is there any other way to do it? 还有其他方法吗?

.Abort() causes the thread to be destroyed completely. .Abort()导致线程被完全破坏。 If you are generating a new thread each time like in your example, then it will work. 如果像示例一样每次都生成一个新线程,那么它将起作用。 However if you are using the same t object, then you need to create a new thread object, you can't run .Start() on an aborted thread. 但是,如果使用相同的t对象,则需要创建一个新的线程对象,因此无法在.Start()终止的线程上运行.Start()

However the fact you are aborting a thread is a concern. 但是,您正在中止线程这一事实令人担忧。 What happens with the application when it does take more than 2 seconds. 如果该应用程序花费了2秒以上的时间,将会发生什么情况。 Should you be showing the user, please wait, its taking longer than expected. 如果您要向用户显示,请等待,所需时间比预期更长。 Doing that in the if block is where to do it. if块中执行此操作即可。 .Join() won't stop the thread even if it doesn't manage to join. .Join()即使无法成功加入也不会停止线程。

Edit 编辑

You have 2 options you might want to consider in rewriting this section: 在重写本节时,您可能要考虑两个选项:

  1. BackgroundWorker - http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx BackgroundWorker- http://msdn.microsoft.com/zh-cn/library/system.threading.tasks.task.aspx
  2. Thread Task - http://msdn.microsoft.com/en-us/library/windowsphone/develop/cc221403(v=vs.105).aspx 线程任务-http: //msdn.microsoft.com/zh-cn/library/windowsphone/develop/cc221403(v=vs.105).aspx

Tasks seem to be the appropriate solution in your scenario. 任务似乎是您方案中的合适解决方案。

One approach maybe be to consider using a Task and having a CancellationToken passed in to it from a CancellationTokenSource instantiated against a specific timespan - 2 seconds. 一种方法可能是考虑使用一个Task,并从CancellationTokenSource传入一个CancellationToken,并在特定的时间间隔内实例化2秒。 That way when the the time specified has elapsed, the CancellationToken, passed to the Task, will be signaled and then appropriate action can be taken. 这样,当指定的时间过去后,将向传递给Task的CancellationToken发出信号,然后可以采取适当的措施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM