如何暂停和恢复我的应用程序中的线程?
我正在开发一个游戏。在玩游戏时,如果用户按下设备后退按钮,我必须暂停线程并显示一个警报框,它确认用户是否真的想退出。如果用户想退出,只需退出游戏。但是当用户不想退出时就会出现问题。然后我必须恢复线程。但我不能这样做..下面给出的是我的代码片段..
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常您不想使用恢复和挂起,因为它们会使线程处于不一致的状态。完成您想要做的事情的最佳方法是使用信号量:
在某处声明一个布尔值,让您指定游戏是否暂停:
在游戏循环中检查此布尔值,如果暂停则等待:
从主线程设置和取消设置暂停的布尔值:
这是示意性代码,例如,它不处理异常,但您明白了。
Usually yo do not want to use
resume
andsuspend
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:
Check this Boolean in the game loop and wait if paused:
Set and unset the paused Boolean from the main thread:
This is schematic code, which does for example not treat exceptions, but you get the idea.
我认为你的问题出在你的
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.
我有同样的问题,但我找到了解决方案,只需替换线程中的这段代码即可。
}
I have same problem but i find out the solution just replace this code in the thread.
}