C# (.NET) 中的类似 Android AsyncTask 的功能 [英] Android AsyncTask-like functionality in C# (.NET)

查看:31
本文介绍了C# (.NET) 中的类似 Android AsyncTask 的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可能知道,Android 的 SDK 具有 AsyncTask 类,该类允许在单独的线程上执行代码并在主 (UI) 线程中获取结果.简而言之,是这样的:

As you might know, Android's SDK features a AsyncTask class which allows for code execution on a separate thread and result acquisition in the main (UI) thread. In short, something like this:

class AsyncTask
{
   void onPreExecute(...)
   {
       //Executed on the main thread BEFORE the secondary thread gets to work on anything.
   }

   void execute(...)
   {
       //This runs on the secondary thread.
   }

   void onPostExecute(...)
   {
       //This runs on the main thread AFTER the secondary thread finishes work. You get the result here in the form of some data type.
   }
}

当然,这只是一个粗略的方案,但如果您不熟悉它,它应该提供有关 AsyncTask 的足够信息.基本上,我正在 Microsoft 的 .NET Framework 中寻找相同的功能.

Of course, this is just a rough scheme, but it should provide enough info on the AsyncTask if you're not familiar with it. Basically, I'm looking for the same functionality in Microsoft's .NET Framework.

在我开始使用自己的类来执行此操作之前,我想确保没有任何东西可以让我在框架中获得所需的结果.我正在使用 .NET 4.0.也许是对 System.Threading.Tasks.Task 的某种聪明"使用?不知道,我把这个留给你.

Before I start working on my own classes to do this, I want to be sure there's nothing that could allow me to get the desired result already in the framework. I'm using .NET 4.0. Perhaps some sort of 'clever' use of System.Threading.Tasks.Task? Have no idea, I'll leave this to you.

简而言之,我想将一些输入传递给一个函数,在辅助线程上运行代码,并在完成时通过主线程更新一些 UI 元素.锁定主线程(例如通过 Task.Wait())不能很好地满足我的要求.

In short, I want to pass some input to a function, run the code on a secondary thread, and on completion, update some UI elements via the main thread. Locking the main thread (via Task.Wait() for example) doesn't work well with my requirements.

推荐答案

您可以使用 .Net 中的 Task 类实现您想要的.

You can achieve what you're looking for using the Task class in .Net.

下面是一些可以帮助您入门的代码:

Here's what some code looks like that should help get you started:

var task = Task.Factory.StartNew(() => YourMethodGoesHere());

task.ContinueWith(t => UpdateYourUiInThisContinuation(),
                  TaskScheduler.FromCurrentSynchronizationContext());

task.ContinueWith(t => HandleAnExceptionWhichTheTaskMayThrow(), 
                   TaskContinuationOptions.OnlyOnFaulted);

这将安排 YourMethodGoesHere() 离开 UI 线程.延续,UpdateYourUiI​​nThisContinuation() 将被安排在初始任务完成后运行,我使用的重载将迫使它在同一同步上下文中继续(UI 线程,假设正在调用此代码最初的 UI 线程).

That will schedule the YourMethodGoesHere() to run off the UI thread. The continuation, UpdateYourUiInThisContinuation() will be scheduled to run once the initial task completes, and the overload I used will force it to continue within the same synchronization context (UI thread, assuming this code is being called on the UI thread initially).

最后一个延续是处理任务中的代码可能抛出的任何异常的好习惯.如果你不处理它(除了使用这个延续还有其他方法),你最终会得到一个未处理的 AggregateException.

The last continuation is good practice to handle any exceptions which code within the task may thrown. If you don't handle it (there are other ways other than using this continuation), you'll end up with an unhandled AggregateException.

这篇关于C# (.NET) 中的类似 Android AsyncTask 的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆