简体   繁体   中英

Stop execution of a thread when a panel is removed from another panel

I have a panel (B) inside a panel (A). Panel (B) starts executing a thread and updating its own GUI.

But there is a case where user logs out of panel (B) and some other panel ( say Panel (C) ) comes in place of (B), while the thread keeps on executing.

I want to interrupt (stop) this thread when Panel (B) is no more visible ...any suggestions ?

One option would be to add a ContainerListener to Panel A. Then check if Panel B is being removed and call "stopThread()" on Panel B. What I'd suggest is creating an interface with a "stopThread()" method (call it interface C), create your own Panel class for B such that B extends Panel implements C.

Then in the ContainerListener.componentRemoved method, test the removed component:

if (component instanceof C)
{
  C c = (C)component;
  c.stopThread();
}

You could even include a startThread() and call that when added using a similar technique.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM