b-g / processing-sublime

A Sublime Text package for the programming language Processing
MIT License
448 stars 60 forks source link

java.lang.Thread issues (not in Processing) #120

Closed ubidefeo closed 6 years ago

ubidefeo commented 6 years ago

hi folks

In order to support some students on a project, I had to write a class extending Thread so they could run it when an event occurred (serial message). I don't mean a Processing thread(String) but a proper Java Thread.

While in Processing the Thread.sleep(ms) function gets called and the thread sleeps, when in Sublime I get this error

Thread.pde:49:0:49:0: The function sleep(int) does not exist.

anyone else bumped into this? I have tried import java.lang.Thread (which in Processing gives me no issue) but I get what looks like a duplicate import error:

The import java.lang.Thread conflicts with a type defined in the same file

Hope someone can shine a light on this.

here's a Class I'm using to test this behaviour

class SpeakThread extends Thread {
  public boolean isRunning;
  private int counter;
  public void stopRunning() {
    this.isRunning = false;
  }

  public void run() {
    this.isRunning = true;
    this.counter = 0;
    println("running");
    while (this.isRunning) {
      println("Sleeping...");
      try {
        counter++;
        Thread.sleep(100);
      }
      catch(Exception ie) {
        println("trouble sleeping");
      }
      if (counter > 30) {
        this.isRunning = false;
      }

      println("counter: " + counter);
    }
    println("done");
    return;
  }
}
ybakos commented 6 years ago

Please post the entire contents of Thread.pde, since the error is being reported on line 49, and your snippet doesn't reflect this.

ubidefeo commented 6 years ago

@ybakos the whole program is not relevant to the specific error, but here it is. as I said, this works inside Processing but not in Processing Sublime

//import java.lang.Thread;
SpeakThread runningThread;
void setup() {
  runningThread = new SpeakThread();

  // thread("readKeys");
}
void draw() {
}
void checkKeys() {
  println("checkKeys");
  if (keyPressed) {
    if (key == 'q') {

      println("QQQ");
      runningThread.isRunning = false;
      ;
    }
  }
}
void keyPressed() {

  println("Key");
  if (key == 'q') {
    runningThread.isRunning = false;
  }
  if (key == 't') {
    runningThread.start();
    //runningThread.run();
  }
}

class SpeakThread extends Thread {
  public boolean isRunning;
  private int counter;
  public void stopRunning() {
    this.isRunning = false;
  }

  public void run() {
    this.isRunning = true;
    this.counter = 0;
    println("running");
    while (this.isRunning) {
      checkKeys();
      println("Sleeping...");
      try {
        counter++;
        Thread.sleep(100);
      }
      catch(InterruptException ie) {
        println("trouble sleeping");
      }
      if (counter > 30) {
        this.isRunning = false;
      }

      println("counter: " + counter);
    }
    println("done");
    return;
  }
}
ybakos commented 6 years ago

@ubidefeo Runs just fine for me in both Processing and from Sublime - assuming I fix the syntax error of InterruptException to InterruptedException.

ubidefeo commented 6 years ago

@ybakos even after fixing that I get the same error.

Thread.pde:49:0:49:0: The function sleep(int) does not exist.

I added "Interrupt" at a later time, the compiler won't complain about it being just an Exception, thinking it would help but it didn't. On which OS are you? Java version?

maybe it can help me figure out why I have this issue. I'd much rather write my processing code in ST than the Processing editor :)

ubidefeo commented 6 years ago

@ybakos I found my issue, and I think it's relevant to everyone using Processing-Sublime

The compiler is not able to find imports/definitions for source which is not in the standard Processing documents folder. On Mac, I keep my original Processing sketches in my Documents folder (same for Arduino), but when I use ST I tend to keep my projects in another folder. I think the processing-java command fails to fully recognise things outside of its designated document folder (not sure why).

Now that I know I'll work in the standard sketch folder, but it would be great if a project could reside everywhere.

Thank you for your test

ybakos commented 6 years ago

@ubidefeo What operating system do you use?

We have never had a problem using the Processing Sublime plugin for projects that are not in the default sketches directory.

For example, I created an empty sketch on my Desktop (MacOS), pasted your code, ran it with the sublime plugin, and do not receive any error.

ubidefeo commented 6 years ago

I'm on Mac OS 10.12.6 (Sierra). Java 8 (1.8.0_162)

same exact code works in Processing 3.3.6 and ST as long as the project resides in the ~/Documents/Processing folder. If I move it anywhere else then imports fail and some classes are not found. Not sure what to make of it. I don't use Processing that often and never tried creating a Thread in Processing-Sublime before so it worked great for me so far.

I don't mind keeping the projects in the Documents folder now that I know, but I still wonder what causes this