出于某种原因,我无法从FileSystemWatcher中删除事件处理程序.
这就是我所拥有的
void Start()
{
ivFileSystemWatcher = new FileSystemWatcher();
ivFileSystemWatcher.Changed +=
new FileSystemEventHandler(ivFileSystemWatcher_Changed);
}
void Stop()
{
ivFileSystemWatcher.Changed -=
new FileSystemEventHandler(ivFileSystemWatcher_Changed);
ivFileSystemWatcher.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
当我打电话给开始时,我开始接收更改事件,但是当我呼叫停止时,我希望事件停止,但它们仍在被提升.
好吧,这是我遇到的一个非常奇怪的问题,而且我很确定我在某个地方搞乱了,但我无法弄清楚在哪里.
我在想的是 -
Timer执行TimerTask一次TimerTask又执行一个AsyncTask(在这种情况下,简单的休眠的第二返回AsyncTasks的数目的静态计数之前).当然,已经使用适当的Handlers和Runnables将异步消息从其他线程发布到UI.
此代码仅执行一次.我希望它每5秒发射一次.这是代码.
注意:我不知道如何处理Looper.经过反复试验,我把它放在那里!
public class TimerAsyncMixActivity extends Activity {
public static final String TAG = "TimerAsyncMix";
static int executionCount = 0;
Handler mHandler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Timer().schedule(new MyTimerTask(this), 0, 5000);
}
class MyAsyncTask extends AsyncTask<String, Void, Integer>{
@Override
protected Integer doInBackground(String... params) {
try {
Thread.sleep(1000);
} catch (InterruptedException …Run Code Online (Sandbox Code Playgroud) 我试图更新我的UI FirstActivity当我收到通知,但被迷惑runOnUiThread,Runnable和Handler.这就是我所拥有的:我正在运行FirstActivity和NotificationService.当NotificationService收到通知时,它将更新FirstActivity UI.
我还有另一个服务AlarmService正在运行.第一项活动
@Override
public void onResume() {
super.onResume();
//some other code for alarm service
}
Run Code Online (Sandbox Code Playgroud)
NotificationService
//on receiving notification
private void showNotification(String text) {
//Get activity
Class<?> activityClass = null;
try {
activityClass = Class.forName("com.pakage.FirstActivity");
contextActivity = (Activity) activityClass.newInstance();
//Update UI on FirstActivity not working
contextActivity.runOnUiThread(new Runnable() {
public void run()
{
Looper.prepare();
TextView tv = (TextView ) contextActivity.findViewById(R.id.notifyTest);
Looper.loop();
}
});
} catch (Exception e) {
e.printStackTrace();
} …Run Code Online (Sandbox Code Playgroud) 我想在Perl中指定一个信号处理程序,但是使用数字,而不是名称.这可能是一种简洁的方式吗?与杀戮缺乏对称特别突出.例如,而不是
$SIG{USR2} = \&myhandler;
Run Code Online (Sandbox Code Playgroud)
我想说
$SIG{12} = \&myhandler;
Run Code Online (Sandbox Code Playgroud)
我目前最好的是"使用Config"并根据perldoc perlipc中的代码在$ Config {sig_name}中查找.这很冗长,似乎不必要地复杂化.
理由:我最近在两个案例中想要这个.
1:我是由一个错误的父进程启动的,他错误地设置了我忽略的信号,我想将所有内容重置为默认值.例如http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=679630 目标是简单而蛮力的东西:
foreach my $i (1..32) { $SIG{$i} = 'DEFAULT'; }
Run Code Online (Sandbox Code Playgroud)
2:我正在编写一个薄的,尽可能不可见的包装脚本.如果我正在包装的程序以信号退出,我想以相同的信号退出.但是,我捕获了一些信号,所以我需要清除自己的信号处理程序以确保我实际退出而不是进入我的信号处理程序.我的目标是写一些这样简短的东西:
$ret = system("./other-program");
$SIG{$ret & 127} = 'DEFAULT';
kill $ret & 127, $$;
Run Code Online (Sandbox Code Playgroud) 我有以下代码.
public class SplashScreen extends Activity {
private int _splashTime = 5000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
new Handler().postDelayed(new Thread(){
@Override
public void run(){
Intent mainMenu = new Intent(SplashScreen.this, MainMenu.class);
SplashScreen.this.startActivity(mainMenu);
SplashScreen.this.finish();
overridePendingTransition(R.drawable.fadein, R.drawable.fadeout);
}
}, _splashTime);
}
}
Run Code Online (Sandbox Code Playgroud)
我在分析这段代码时遇到了问题.至于知道处理程序在主线程中运行.但它有在其他线程中运行的线程.
MainMenu.class将在主线程或第二个线程中运行?如果主线程停止5秒,ANR将被提升.为什么当我停止延迟(_splashTime)ANR时不显示(即使我将它增加到超过5秒)
我在android中有一个水平进度条.我需要在60秒内完成它.
为什么以下代码不起作用?
int progress = 0;
progressBarHorizontal.setMax(60);
while(progress < 60) {
progressHandler.postDelayed((new Runnable() {
public void run() {
progressBarHorizontal.setProgress(progress);
}
}),progress * 1000);
progress++;
}
Run Code Online (Sandbox Code Playgroud)
请提出一些其他方法.我试过这个:
new Thread(new Runnable() {
int progress = 0;
public void run() {
long timerEnd = System.currentTimeMillis() + 60 * 1000;
while (timerEnd > System.currentTimeMillis() ) {
progress = (int) (timerEnd - System.currentTimeMillis()) / 1000;
// Update the progress bar
progressHandler.post(new Runnable() {
public void run() {
progressBarHorizontal.setProgress(progress);
}
});
try {
Thread.sleep(500);
} catch …Run Code Online (Sandbox Code Playgroud) 我正在为Android应用程序工作并使用AsyncTask类实现ProgressBar.
问题是在某些设备上,它会导致"CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图." 在onPostExecute中.在这些设备上,问题发生在100%.在其他设备上,它工作正常.
public final class MyAsyncTask extends AsyncTask<String, Integer, String>
{
private ProgressBar progress;
private ListActivity activity;
public MyAsyncTask(ListActivity activity, ProgressBar progress)
{
this.progress = progress;
this.activity = activity;
}
protected void onPreExecute()
{
this.progress.setVisibility(view.VISIBLE);
}
protected String doInBackground(String[] arg0)
{
// getting xml via httpClient
return string;
}
protected void onPostExecute(String result)
{
this.progress.setVisibility(view.GONE);
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么onPostExecute不能在那些特定设备上的UI线程上运行.
接下来,我尝试用runOnUiThread调用它,以确保它在UI线程上运行.
runOnUiThread(new Runnable() {
@Override
public void run() {
ProgressBar progress = (ProgressBar)findViewById(R.id.some_view_progressbar);
MyAsyncTask task = new MyAsyncTask(activity, progress);
task.execute();
}
} …Run Code Online (Sandbox Code Playgroud) 对于我的游戏,我实施了一个库存系统.单击屏幕时,a MousePressedEvent将通过layers游戏中的所有对象传递给所有继承EventListener(My EventListener)的对象.该EventListener级工作正常,并使用它,如下图所示,我已经成功地让我的库存,这样你可以删除一个槽物品,将它们放回.然而,我想要做的是能够将它们从任何包含项目的插槽中取出,并将它们放在任何其他插槽中(只要目标插槽为空).我认为我所拥有的将允许这样做,因为在if声明中我没有检查如果选择了插槽,我将其添加到插槽中.但这实际上并不奏效.有任何想法吗?
Slot.java课堂代码:
public boolean onMousePressed(MousePressedEvent e) {
Point p = new Point(Mouse.getX(), Mouse.getY());
if (!this.getBounds().contains(p)) return false;
boolean left = (e.getButton() == MouseEvent.BUTTON1);
boolean right = (e.getButton() == MouseEvent.BUTTON3);
boolean hasItems = (items.size() > 0);
if (this.getBounds().contains(p)){
if (right && !selected && hasItems){
select(true);
s = new Slot(new Vector2i(Mouse.getX(), Mouse.getY()));
addComponent(s);
s.add(items.get(0));
remove(items.get(items.size() - 1));
} else if (right && selected){
s.add(items.get(0));
remove(items.get(items.size() - 1));
if (items.size() …Run Code Online (Sandbox Code Playgroud) 如果在套接字上为同一个函数多次调用'on'方法会发生什么?多次调用它只是覆盖了最后一个注册的函数,还是使用了更多的资源?
如果是后者,那么如何确定处理程序是否已经注册?
我想每100毫秒更新一次UI.在StackOverflow中搜索后,我找到了一个使用Runnable和Handler这样的解决方案
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
//update UI here
handler.postDelayed(this, 100);
}
};
runnable.run();
Run Code Online (Sandbox Code Playgroud)
有用!但我有一些问题:
Handler是附加MainThread,所以Runnable在MainThread 上运行?
Runnable在MainThread上运行,为什么需要Handler?据我所知,Handler用于在两个线程之间发送消息