简体   繁体   English

分离/解耦AsyncTask和UI-Thread

[英]Separating/Decoupling AsyncTask and UI-Thread

Is there any way to separate the UI-Thread and the AsyncTask? 有没有办法分离UI-Thread和AsyncTask? I would like to execute the AsyncTask, register some Method and simply wait. 我想执行AsyncTask,注册一些方法,然后等待。 If the AsyncTask finishes I would like that the UI-Threads starts a Function which simply fills the Data from the AsyncTask into the Forms. 如果AsyncTask完成,我希望UI-Threads启动一个函数,它只是将数据从AsyncTask填充到Forms中。

One way would be using Broadcasts, but that seems a bit overkill for me. 一种方法是使用广播,但这对我来说似乎有点矫枉过正。 Is there no other way to seperate the Logic and the UI? 是否没有其他方法来分离逻辑和用户界面? I know that there is protected void onPostExecute(RSSFeed result) but that would is in the AsyncTask-Class so UI-Logic and Backgroundlogic is mixed again. 我知道有一个protected void onPostExecute(RSSFeed result)但那将在AsyncTask-Class中,因此UI-Logic和Backgroundlogic再次混合在一起。

I ask this because in C# its very easy to accomplish this, just add an Event and I personally find it very chique if a good separation is existent. 我问这个是因为在C#中很容易实现这一点,只需添加一个事件,如果存在良好的分离,我个人觉得非常好。

You can use a separate custom Thread , and pass a Handler when you instantiate it. 您可以使用单独的自定义Thread ,并在实例化时传递Handler When the thread finish, let it call the handler. 线程完成后,让它调用处理程序。

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

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