Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to access functions within a thread?  RSS feed

 
Andreas Balzer
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I assign a new thread to the Thread-object called a.
Within the thread there is a function called b. I'd like to do a.b(); but it doesn't work. The error message is that b can't be found. How to access function b?

Greetings,
Andreas
 
Henry Wong
author
Sheriff
Posts: 23277
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the Thread class doesn't have a b() method. How are you adding this b() method?

I am assuming you subclassed the Thread class to add the b() method. Anyway, to access this b() method, you need to cast the Thread object to the subclass first.

Henry
 
Andreas Balzer
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please help me finding the place? Thanks
[code]import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;


public class swttictacto {
public boolean[] spielfeld = new boolean[34];
public boolean aktuellerspieler = true;
public Thread referenz;

public static void main (String[] args) {
swttictacto spiellogik = new swttictacto();
}

public swttictacto () {
referenz = new Thread(new guithread(this));
referenz.start();
}

public boolean gewonnen() {
boolean ok = false;
/*if ((spielfeld[11] && spielfeld[12] && spielfeld[13]) || (spielfeld[21] && spielfeld[22] && spielfeld[23]) || (spielfeld[31] && spielfeld[32] && spielfeld[33]) || (spielfeld[11] && spielfeld[22] && spielfeld[33]) || (spielfeld[31] && spielfeld[22] && spielfeld[13]) || (spielfeld[11] && spielfeld[21] && spielfeld[31]) || (spielfeld[31] && spielfeld[32] && spielfeld[33])) { ok = true; referenz.gewonnen(true); }
if ((!spielfeld[11] && !spielfeld[12] && !spielfeld[13]) || (!spielfeld[21] && !spielfeld[22] && !spielfeld[23]) || (!spielfeld[31] && !spielfeld[32] && !spielfeld[33]) || (!spielfeld[11] && !spielfeld[22] && !spielfeld[33]) || (!spielfeld[31] && !spielfeld[22] && !spielfeld[13]) || (!spielfeld[11] && !spielfeld[21] && !spielfeld[31]) || (!spielfeld[31] && !spielfeld[32] && !spielfeld[33])) { ok = true; referenz.gewonnen(false); }
if (!ok && spielfeld[11]!=null && spielfeld[12] != null && spielfeld[13]!=null && spielfeld[21]!=null && spielfeld[22]!=null && spielfeld[23]!=null && spielfeld[31]!=null && spielfeld[32]!=null && spielfeld[33]!=null) { referenz.gleichstand(); ok = true; }
*/
return ok;
}

public void clicked(int buttonnumber) {
if (aktuellerspieler) { spielfeld[buttonnumber] = true; } else { spielfeld[buttonnumber] = false; }
referenz.klick(buttonnumber);
if (!gewonnen()) {
aktuellerspieler = !aktuellerspieler;
referenz.showPlayerMsg(aktuellerspieler);
}
}
}

class guithread implements Runnable {
public swttictacto referenz;
public Label information;
public Composite oben;
public Composite unten;

public guithread(swttictacto ref) {
referenz = ref;
}

public void showPlayerMsg(boolean player) {
if (player) information.setText("Spieler 1 ist dran.");
if (!player) information.setText("Spieler 2 ist dran.");
}

public void gewonnen(boolean a) {
if(a) { information.setText("Spieler 1 hat gewonnen"); } else { information.setText("Spieler 2 hat gewonnen"); }
unten.dispose();
}
public void gleichstand() {
information.setText("Gleichstand");
unten.dispose();
}

public void klick(int buttonzahl) {

}

public void run() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Titel in der Titelleiste");
shell.setSize(200,200);


GridLayout layoutshell = new GridLayout();
layoutshell.numColumns = 1;
shell.setLayout(layoutshell);

oben = new Composite(shell,SWT.NO_BACKGROUND);
Composite unten = new Composite(shell,SWT.NO_BACKGROUND);

GridLayout layoutoben = new GridLayout();
layoutoben.numColumns = 1;
oben.setLayout(layoutoben);

GridLayout layout = new GridLayout();
layout.numColumns = 3;
unten.setLayout(layout);

information = new Label(oben,SWT.LEFT);
information.setText("Spieler 1 ist dran.");

Button b11 = new Button(unten,SWT.PUSH | SWT.CENTER);Button b12 = new Button(unten,SWT.PUSH | SWT.CENTER);Button b13 = new Button(unten,SWT.PUSH | SWT.CENTER);
Button b21 = new Button(unten,SWT.PUSH | SWT.CENTER);Button b22 = new Button(unten,SWT.PUSH | SWT.CENTER);Button b23 = new Button(unten,SWT.PUSH | SWT.CENTER);
Button b31 = new Button(unten,SWT.PUSH | SWT.CENTER);Button b32 = new Button(unten,SWT.PUSH | SWT.CENTER);Button b33 = new Button(unten,SWT.PUSH | SWT.CENTER);

b11.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
switch (e.type) {
case SWT.Selection:
referenz.clicked(11);
break;
}
}
});
b12.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
switch (e.type) {
case SWT.Selection:
referenz.clicked(12);
break;
}
}
});
b13.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
switch (e.type) {
case SWT.Selection:
referenz.clicked(13);
break;
}
}
});
b21.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
switch (e.type) {
case SWT.Selection:
referenz.clicked(21);
break;
}
}
});
b22.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
switch (e.type) {
case SWT.Selection:
referenz.clicked(22);
break;
}
}
});
b23.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
switch (e.type) {
case SWT.Selection:
referenz.clicked(23);
break;
}
}
});
b31.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
switch (e.type) {
case SWT.Selection:
referenz.clicked(31);
break;
}
}
});
b32.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
switch (e.type) {
case SWT.Selection:
referenz.clicked(32);
break;
}
}
});
b33.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
switch (e.type) {
case SWT.Selection:
referenz.clicked(33);
break;
}
}
});

shell.open();
while(!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}[/code
 
Henry Wong
author
Sheriff
Posts: 23277
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please help me finding the place? Thanks


Find what? What place? To call a method, any method, you need to be in a context that access the method. (that has the method in scope)

So, to answer your question... which method are you trying to call? and from which location you are trying to that method from? ... And is the method in scope?

Henry
 
Andreas Balzer
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The function public void clicked(int buttonnumber) tries to call referenz.klick(buttonnumber); and referenz.showPlayerMsg(aktuellerspieler); where referenz is referenz = new Thread(new guithread(this));referenz.start();

But I get an error message that referenz.blablabla is not found. However if you look into the class guithread: public void showPlayerMsg(boolean player) is there, as well as public void klick(int buttonzahl)
If you try to compile that (SWT) Programm, you'll see the two error messages.

What is wrong in this program?

Update: I solved the problem partly: It works if I create my thread through extends Thread instead of implement runnable
[ April 12, 2008: Message edited by: Andreas Balzer ]
 
Henry Wong
author
Sheriff
Posts: 23277
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update: I solved the problem partly: It works if I create my thread through extends Thread instead of implement runnable


The reason it originally didn't work was because the method was not part of the Thread object, it was part of the Runnable class that the thread object called. Another solution would have been to save both the Runnable and Thread object. Then the clicked(int buttonnumber) method can call runnablerefz.klick(buttonnumber) -- call the method through the other reference.

Henry
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!