如何暂停和恢复我的应用程序中的线程?

发布于 2024-12-22 09:49:00 字数 2092 浏览 2 评论 0原文

我正在开发一个游戏。在玩游戏时,如果用户按下设备后退按钮,我必须暂停线程并显示一个警报框,它确认用户是否真的想退出。如果用户想退出,只需退出游戏。但是当用户不想退出时就会出现问题。然后我必须恢复线程。但我不能这样做..下面给出的是我的代码片段..

             public boolean onKeyDown(int keyCode, KeyEvent event) {
     if (keyCode == KeyEvent.KEYCODE_BACK) {

         MyGamePanel.thread.setRunning(false);
         AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setTitle("Exit Alert");
                        alertDialog.setMessage("Do you really want to exit the Game?");
            alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                 finish();
                 return;
            } }); 
            alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                  dialog.cancel();
                      MyGamePanel.thread.resume();
                      MyGamePanel.thread.setRunning(true);
                return;
            }}); 
             alertDialog.show();

         return true;
     }
     return super.onKeyDown(keyCode, event);
 }

下面给出的是线程部分。

       @Override
public void run() {
    Canvas canvas;
    Log.d(TAG, "Starting game loop");

    while (running) {
        canvas = null;
        try {
            canvas = this.surfaceHolder.lockCanvas();
            synchronized (surfaceHolder) {
                // update game state 
                this.gamePanel.update();
                // render state to the screen
                // draws the canvas on the panel
                this.gamePanel.render(canvas);  


            }
        } finally {
            // in case of an exception the surface is not left in 
            // an inconsistent state
            if (canvas != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }   // end finally
    }
}

update() 和 render 是 MyGamePanel 类中编写的两个函数。

我尝试了很多方法,但没有一个有效。请帮助我......

i am developing a game.while playing the game if the user presses the device back button i have to pause the thread and display an alert box it confirms whether the user really wants to exit or not.if the user wants to exit it simply exit the game.but the problem arise when the user does not want to exit.then i have to resume the thread. but i cannot do that..given below is my code snippet..

             public boolean onKeyDown(int keyCode, KeyEvent event) {
     if (keyCode == KeyEvent.KEYCODE_BACK) {

         MyGamePanel.thread.setRunning(false);
         AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setTitle("Exit Alert");
                        alertDialog.setMessage("Do you really want to exit the Game?");
            alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                 finish();
                 return;
            } }); 
            alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                  dialog.cancel();
                      MyGamePanel.thread.resume();
                      MyGamePanel.thread.setRunning(true);
                return;
            }}); 
             alertDialog.show();

         return true;
     }
     return super.onKeyDown(keyCode, event);
 }

and the given below is the thread part.

       @Override
public void run() {
    Canvas canvas;
    Log.d(TAG, "Starting game loop");

    while (running) {
        canvas = null;
        try {
            canvas = this.surfaceHolder.lockCanvas();
            synchronized (surfaceHolder) {
                // update game state 
                this.gamePanel.update();
                // render state to the screen
                // draws the canvas on the panel
                this.gamePanel.render(canvas);  


            }
        } finally {
            // in case of an exception the surface is not left in 
            // an inconsistent state
            if (canvas != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }   // end finally
    }
}

update() and render are two functions written in MyGamePanel class.

i have tried many things but none is working..please help me...

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

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

发布评论

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

评论(3

可是我不能没有你 2024-12-29 09:49:00

通常您不想使用恢复和挂起,因为它们会使线程处于不一致的状态。完成您想要做的事情的最佳方法是使用信号量:

  • 在某处声明一个布尔值,让您指定游戏是否暂停:

    布尔暂停;
    
  • 在游戏循环中检查此布尔值,如果暂停则等待:

    同步(已暂停){
        while(暂停){
            等待();
        }
    }
    
  • 从主线程设置和取消设置暂停的布尔值:

    public void setPaused(boolean WantToPause) {
        同步(暂停){
            暂停=想要暂停;
            通知();
        }
    }
    

这是示意性代码,例如,它不处理异常,但您明白了。

Usually yo do not want to use resume and suspend because they can leave the thread in an inconsistent state. The best way to do what you are trying to do is with a semaphore:

  • Declare a Boolean somewhere that lets you specify if the game is paused:

    Boolean paused;
    
  • Check this Boolean in the game loop and wait if paused:

    synchronized(paused) {
        while(paused) {
            wait();
        }
    }
    
  • Set and unset the paused Boolean from the main thread:

    public void setPaused(boolean wantToPause) {
        synchronized(paused) {
            paused = wantToPause;
            notify();
        }
    }
    

This is schematic code, which does for example not treat exceptions, but you get the idea.

寄意 2024-12-29 09:49:00

我认为你的问题出在你的 MyGamePanel.thread.resume(); 中。尝试删除该代码,看看会发生什么。

如果您认为 thread.resume() 是您出于不同目的所需的函数(您被限制删除它),请向我们展示 thread.resume() 函数代码,以便我们可以尝试帮助您修复恢复函数代码。

I think your problem is in your MyGamePanel.thread.resume();. Try to remove that code and see what happen.

If you think thread.resume() is the function you needed for different purpose(that you are restricted to delete it), then show us the thread.resume() function code so we can try to help you fix the resume function code.

∞觅青森が 2024-12-29 09:49:00

我有同样的问题,但我找到了解决方案,只需替换线程中的这段代码即可。

while (true) {
if(running){
Log.d("running", "true");
    canvas = null;
    try {
        canvas = this.surfaceHolder.lockCanvas();
        synchronized (surfaceHolder) {
            // update game state 
            this.gamePanel.update();
            // render state to the screen
            // draws the canvas on the panel
            this.gamePanel.render(canvas);  


        }
    } finally {
        // in case of an exception the surface is not left in 
        // an inconsistent state
        if (canvas != null) {
            surfaceHolder.unlockCanvasAndPost(canvas);
        }`enter code here`
    }   // end finally
}

}

I have same problem but i find out the solution just replace this code in the thread.

while (true) {
if(running){
Log.d("running", "true");
    canvas = null;
    try {
        canvas = this.surfaceHolder.lockCanvas();
        synchronized (surfaceHolder) {
            // update game state 
            this.gamePanel.update();
            // render state to the screen
            // draws the canvas on the panel
            this.gamePanel.render(canvas);  


        }
    } finally {
        // in case of an exception the surface is not left in 
        // an inconsistent state
        if (canvas != null) {
            surfaceHolder.unlockCanvasAndPost(canvas);
        }`enter code here`
    }   // end finally
}

}

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