我可以按顺序链接异步任务吗(在上一个异步任务完成后开始一个)

发布于 2024-12-05 18:56:24 字数 511 浏览 0 评论 0原文

每次我执行 httpRequest 时,在代码执行时屏幕都会被锁定几秒钟。因此,我使用 AsyncTask 在单独的线程中执行所有 httpRequest 操作,同时放置 ProgressDialog,以便用户知道正在发生某些事情。

我最近遇到了以下情况:我的一个 httpRequest 的输入依赖于前一个 httpRequest (+parse) 操作的结果。我不能只是按顺序放置两个 AsyncTask,因为 Android 会将它们放入两个线程中,并在第一个线程尚未完成的情况下启动第二个线程。如果没有适当的输入(第一个 httpRequest 的结果),我的第二个 httpRequest 将使应用程序崩溃。

有没有办法可以放入 wait() 来强制第二个 AsyncTask 在第一个任务完成之前不启动?

Every time I do a httpRequest, the screen will appear to be locked up for a few seconds while the code is executing. Hence I used AsyncTask to do all my httpRequest stuff in a separate thread while putting up a ProgressDialog so the user knows something is happening.

I recently encountered the following situation: the input of one of my httpRequest is dependent on the result from a previous httpRequest (+parse) action. I can't just put the two AsyncTasks sequentially cause Android will put them in two threads and start the second one without the first one being finished. And without an appropriate input (the result of the first httpRequest), my second httpRequest will crash the app.

Is there way I can put-in a wait() to force the second AsyncTask not to start until the first one finishes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

红颜悴 2024-12-12 18:56:24

前几天我也遇到了同样的情况。
我是这样解决的:
将您的活动的引用传递给异步类的构造函数并在后台函数中执行 do 。现在,在执行后函数中,从异步类调用您的活动的公共方法以再次执行任务...或尝试以下操作:

            if (asynclass.getStatus() == android.os.AsyncTask.Status.PENDING) {
                asynclass.execute();
            } else if (RF.getStatus() == android.os.AsyncTask.Status.FINISHED) {
                asynclass = new asyncclass();
                asynclass.execute();
            } else {
                Toast.maketoast(this, "Plz wait", 1).show();
            }

干杯

I also had some same situation the other day.
I had solved it in this way:
Pass the reference of your activity to the constructor of async class and execute the do in background function. Now in post execute function call a public method of ur activity from async class to execute the task again... or try this:

            if (asynclass.getStatus() == android.os.AsyncTask.Status.PENDING) {
                asynclass.execute();
            } else if (RF.getStatus() == android.os.AsyncTask.Status.FINISHED) {
                asynclass = new asyncclass();
                asynclass.execute();
            } else {
                Toast.maketoast(this, "Plz wait", 1).show();
            }

Cheers

寄意 2024-12-12 18:56:24

所以我认为(不确定),您需要在执行后方法上调用第二个 asyncTask

protected void onPostExecute(final Void unused) {
            if (this.dialog.isShowing()) {
                this.dialog.dismiss();
                     new secTask().execute();
            }}

so i think(not sure),you need to call second asyncTask on post execution method

protected void onPostExecute(final Void unused) {
            if (this.dialog.isShowing()) {
                this.dialog.dismiss();
                     new secTask().execute();
            }}
千鲤 2024-12-12 18:56:24

是的,你可以。但它只能在 onProgressUpdate()onPostExecute() 中实现。尝试这样做:

private class Task1 extends AsyncTask<Void, Void, Void> {
...
...
    protected void onPostExecute(Void unused) { 
        dialog1.dismiss();
        new Task2().execute();
    }

}

同样,您必须将 new Task3().execute(); 放入 Task2 的 AsyncTask< 的 onPostExecute() 方法中/code>

不要在主代码中这样调用它们。如果这样做,那么所有这些都将一起运行:

// This is wrong
new Task1().execute();
new Task2().execute();
new Task3().execute();

参考: 异步任务

Yes, you can. But it is only possible in onProgressUpdate() or onPostExecute(). Try doing it like this:

private class Task1 extends AsyncTask<Void, Void, Void> {
...
...
    protected void onPostExecute(Void unused) { 
        dialog1.dismiss();
        new Task2().execute();
    }

}

Similarly, you'll have to put new Task3().execute(); in the onPostExecute() method of Task2's AsyncTask

Don't call them like this in your main code. If you do, then all of them will run together:

// This is wrong
new Task1().execute();
new Task2().execute();
new Task3().execute();

Reference: AsyncTask

一江春梦 2024-12-12 18:56:24

如何使用 executeOnExecutor 方法:

public void runTasks() {

        AsyncTask<Void,Void,Void> aTask = myTask();
        AsyncTask<Void,Void,Integer> aTask1 = myTask1();
        AsyncTask<Void,Void,Void> aTask2 = myTask2();

        aTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
        aTask1.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
        aTask2.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);

    }

How about using executOnExecutor method :

public void runTasks() {

        AsyncTask<Void,Void,Void> aTask = myTask();
        AsyncTask<Void,Void,Integer> aTask1 = myTask1();
        AsyncTask<Void,Void,Void> aTask2 = myTask2();

        aTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
        aTask1.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
        aTask2.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);

    }
那一片橙海, 2024-12-12 18:56:24

偶然发现这个线程正在寻找比我当前使用的更好的解决方案,但它似乎仍然是最好的...

我所做的就是在每个异步任务的构造函数中传递一个可运行的对象。

无论是在执行后还是在后台执行结束时,我都会启动下一个任务(如果我有一个通过执行可运行的任务)。

 if(doOnPostExec != null)
        doOnPostExec.run();

这样我就可以通过我传递的可运行对象来改变异步任务的顺序,从而保持代码的灵活性,如果我没有传递可运行对象,它们会正常完成。可运行对象仅包含一行调用下一个异步任务。

我只是不喜欢制作所有这些可运行的东西。希望存在像 vb.net 中那样用于链接代表的东西。

Stumbled on this thread looking for a better solution than what I currently use, but it still seems the best...

What I do is pass a runnable in the constructor of each of my async tasks.

Either in the post execute or at the end of the do in the background, I launch my next task, if I have one by executing the runnable.

 if(doOnPostExec != null)
        doOnPostExec.run();

This way I can changeup the order of my asynctasks by the runnables I pass keeping the code flexible, and if I pass no runnable they complete normally. The runnables just contain one line calling the next asynctask.

I just don't like making all those runnables. Was hoping something existed like in vb.net for chaining delegates.

薄凉少年不暖心 2024-12-12 18:56:24

我以前遇到过这个问题。我所做的是使用回调接口类并在 onPostExecute() 方法内调用其方法。这样你就可以从回调方法中调用另一个AsyncTask。

I encountered this problem before. What I did is use a call back interface class and call its method inside the onPostExecute() method. This way you can call another AsyncTask from the callback method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文