Tag: async await

如何通过reflection运行挂起方法?

有一个协程块可以运行暂停function。 但我通过reflectioninvoke函数。 这是java风格的调用,显然一个简单的调用是行不通的。 有没有办法异步运行reflection的方法? 如何等待这个方法? import kotlin.coroutines.experimental.* class TestClass(val InString: String) { suspend fun printString() { println(InString) } } fun launch(context: CoroutineContext, block: suspend () -> Unit) = block.startCoroutine(StandaloneCoroutine(context)) private class StandaloneCoroutine(override val context: CoroutineContext): Continuation { override fun resume(value: Unit) {} override fun resumeWithException(exception: Throwable) { val currentThread = Thread.currentThread() currentThread.uncaughtExceptionHandler.uncaughtException(currentThread, exception) } } […]

在从webservice服务检索数据之前和之后,从ViewModel设置ProgressBar可见性

我正在使用MVVM设计模式和MVVM Light工具包创建Windows Phone 8.1应用程序。 我正在尝试创建一个简单的登录页面,从用户获取用户名和密码并使用web服务进行身份验证。 在这个身份验证过程中,我想在页面顶部显示一个ProgressBar(加载点),以指示正在发生的事情。 我已经成功创建了我的ViewModel并绑定了我的视图,以允许我通过连接到我的登录按钮的命令来控制ProgressBar的可见性。 这可以工作,但是,只有在整个登录过程完成后,UI才会更新以显示ProgressBar,从而使得进度指示符毫无意义。 如何首先设置ProgressBar的可见性(并有UI更新),然后关闭执行我的登录过程? 这里是一些代码: XAML <ProgressBar IsIndeterminate="True" Height="1" Visibility="{Binding Login.ProgressVisibility, UpdateSourceTrigger=PropertyChanged, FallbackValue=Collapsed}" /> 视图模型 public class LoginViewModel : ViewModelBase { private IDialogService dialogService; private INavigationService navigationService; private string _username; private string _password; private Visibility _progressVisibility = Visibility.Collapsed; /// <summary> /// User login name / email address /// </summary> public […]

是否有可能有C#异步方法返回类型是不是任务<T>或无效?

看来任何实现GetAwaiter需求的对象都可以等待, Task<>类型只是满足这个需求的许多可能的类型之一。 然而,就我所知,一个async方法似乎只能返回Task<>或void 。 是否有可能连接我的自定义任务一样的等待类型,所以我可以指定我的自定义类型作为异步返回类型,当然,我可以随后链接通常调用链的await 。 像这样的东西:(伪代码) async MyAwaitable MyAwaitableMethod() { await myAwaitable; } async Task MyTask() { await MyAwaitableMethod(); } 背景信息 :整个想法实际上源于C ++ 1z / 2x协程提案,它基本上具有相同的基本模型,但是允许在定义任何具有嵌入的promise_type类型中进行更多的控制,从协程返回(作为co_await后果)。 [编辑] 除了答案中提供的链接之外,我还在以下链接中找到了更多的信息,这似乎确实引起了一些关于此主题的热烈讨论: https://github.com/dotnet/roslyn/issues/7169 https://github.com/dotnet/roslyn/issues/10902 https://github.com/ljw1004/roslyn/blob/features/async-return/docs/specs/feature%20-%20arbitrary%20async%20returns%20-%20discussion.md [EDIT2] 令人惊讶的是,经过一些进一步挖掘,这实际上是C#7.0的一个预期功能: http : //intellitect.com/generalized-async-return-types/

异步不等待等待

我是Kotlin和协程的新手。 但是我想用它来初始化Android ThreeTen backport库,这是一个长期运行的任务。 我正在使用Metalab Async / Await库( co.metalab.asyncawait:asyncawait:1.0.0 )。 这是我的代码: override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val application = this async { //non-blocking initialize ThreeTen await { AndroidThreeTen.init(application) } //initialize UI on UI thread which uses the ThreeTen library initUI() } } 现在我有初始化UI时库没有初始化的问题。 从我的理解initUI不应该在调用initUI之前调用initUI 。

如何在kotlin协议上指数退避重试

我正在使用扩展方法在网络请求中使用kotlin协同程序来调用类 public suspend fun <T : Any> Call<T>.await(): T { return suspendCancellableCoroutine { continuation -> enqueue(object : Callback<T> { override fun onResponse(call: Call<T>?, response: Response<T?>) { if (response.isSuccessful) { val body = response.body() if (body == null) { continuation.resumeWithException( NullPointerException("Response body is null") ) } else { continuation.resume(body) } } else { continuation.resumeWithException(HttpException(response)) } } […]

在Android服务中的Kotlin协程

我有一个android服务,它启动并同步服务器上的不同类型的数据,当它在线。 我是Kotlin协程新手,我试图完成以下任务: fun syncData{ //Job1 make retrofit call to server //Job2 make retrofit call to server after job1 is done. //Job3 make retrofit call to server after job 2 is done and so on. //After all jobs are done I'll stop service. } 我正在关注这篇文章: Kotlin Coroutines是Android的正确方法 这给我带来了这个解决方案 fun syncData() = async(CommonPool){ try{ val sync1 = […]

Kotlin Coroutines是Android的正确方法

我正在尝试使用异步更新适配器内的列表,我可以看到有太多的样板。 这是使用Kotlin协同程序的正确方法吗? 这可以优化更多? fun loadListOfMediaInAsync() = async(CommonPool) { try { //Long running task adapter.listOfMediaItems.addAll(resources.getAllTracks()) runOnUiThread { adapter.notifyDataSetChanged() progress.dismiss() } } catch (e: Exception) { e.printStackTrace() runOnUiThread {progress.dismiss()} } catch (o: OutOfMemoryError) { o.printStackTrace() runOnUiThread {progress.dismiss()} } }