Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncTask is not terminated when program close #5

Open
hungthuanmk opened this issue Nov 7, 2018 · 0 comments
Open

AsyncTask is not terminated when program close #5

hungthuanmk opened this issue Nov 7, 2018 · 0 comments

Comments

@hungthuanmk
Copy link

I create a simple code to stream webcam source to ImageView, but when the program close it seems that the AsyncTask was not terminated all
Here is the code for my AsyncTask, i have already set thread deamon to True

public class StreamingThread extends AsyncTask {

    private boolean isPlaying = false;

    private VideoCapture vc;
    private Mat currentFrame;
    private int fps = 30;
    private ImageView imageViewer = null;

    public StreamingThread(int cameraIdx, ImageView imageViewer) {
        this.vc = new VideoCapture(cameraIdx);
        this.imageViewer = imageViewer;
        setDaemon(true);
        backGroundThread.setDaemon(true);
    }

    public void stopStream() {
        this.isPlaying = false;
    }

    public void startStream() {
        this.isPlaying = true;
        this.execute();
    }

    public void setFps(int fps) {
        this.fps = fps;
    }

    @Override
    public void onPreExecute() {
        System.out.println("Stated thread!");
    }

    @Override
    public Object doInBackground(Object[] objects) {
        currentFrame = new Mat();
        long beginTime, endTime, delay;

        while (isPlaying) {
            beginTime = System.nanoTime();
            vc.read(currentFrame);
            if (this.imageViewer != null) {
                ImageProcessing.setImage(this.imageViewer, this.currentFrame);
            }
            endTime = System.nanoTime();
            delay = (endTime - beginTime)/1000l;
            try {
                TimeUnit.MICROSECONDS.sleep(30000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public void onPostExecute(Object o) {
        vc.release();
        System.out.println("Stopping thread!");
    }

    @Override
    public void progressCallback(Object[] objects) {

    }

And here is the way i call AsyncTask in controller

streamingThread = new StreamingThread(0, imgCamFont);
streamingThread.startStream();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant