Author Topic: MonitorThread  (Read 13212 times)

chai_thing05

  • Newbie
  • Posts: 10
  • I'm a llama!
    • View Profile
MonitorThread
« on: November 13, 2012, 06:07:06 AM »
Hi,

I'm developing a java GUI for PLC monitoring ...

I using "MonitorThread" for monitor the status of PLC connected.

       MonitorThread mt = new MonitorThread();
       mt.start();

But, May i know how to stop/close the thread as well ?

I tried to use "mt.stop();" , but can't ...

Pls advise. Thanks.

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:MonitorThread
« Reply #1 on: November 13, 2012, 11:32:18 PM »
You can define a variable in the thread and the main program or another thread can set the variable to cause the thread run() function to return and therefore end the thread.
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

chai_thing05

  • Newbie
  • Posts: 10
  • I'm a llama!
    • View Profile
Re:MonitorThread
« Reply #2 on: November 14, 2012, 02:10:41 AM »
Thanks for your reply.

my result:
1st test- connection ok.
2nd test- connection ok.
3rd teset- Log In Using Socket Connection
Failed

even i closed the application after 3rd test, there still have "javaw.exe" in running .. hence i need to manual "end process" and continue it.

attached is the processes runnning on CPU backend.
« Last Edit: November 14, 2012, 02:11:05 AM by chai_thing05 »

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:MonitorThread
« Reply #3 on: November 14, 2012, 09:46:29 PM »
That is a Java programming question. To properly exit an application in Java you need to execute the statement: System.exit(1);

Merely closing a windows does not exit the application which could explain why the program appear to be still running.
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

chai_thing05

  • Newbie
  • Posts: 10
  • I'm a llama!
    • View Profile
Re:MonitorThread
« Reply #4 on: November 14, 2012, 10:46:55 PM »
thanks for your reply.

We can't use the system.exit(0) or exit(1), because it will close all my program.

There are more than one screen in this program.

That's y we looking for how to close the socket connection while the screen disposed.

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:MonitorThread
« Reply #5 on: November 15, 2012, 11:26:02 AM »
This is really a Java programming question and not related to the PLC operation. Please google search for Java help for Java related programming question as it is outside of normal tech support.

What we can offer you here is an example:


    final class Simulate implements Runnable {
        Thread t;
        public boolean stopThread;
       
        public Simulate() {
            stopThread = false;
            t = new Thread(this, "Simulate Thread");
            t.setPriority(3);
            t.start();
        }
       
        public void run() {
            while (true) {
                if (stopThread) {
                    return;
                }
                ......................
             }
         }



So your main program can set the "stopThread" variable to "true" and the thread will end after it return from the run() function.
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

chai_thing05

  • Newbie
  • Posts: 10
  • I'm a llama!
    • View Profile
Re:MonitorThread
« Reply #6 on: November 20, 2012, 12:59:29 AM »
thank a lot !!  ;)