<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META content="MSHTML 5.50.4616.200" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial color=#0000ff size=2></FONT> </DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial size=2>I did run into a
very similar problem myself when writing my VTK-based Java
application and was able to find a </FONT></SPAN></DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial size=2>solution to
</FONT></SPAN><SPAN class=301013917-01042002><FONT face=Arial size=2>solve
</FONT></SPAN><SPAN class=301013917-01042002><FONT face=Arial size=2>my
problem. </FONT></SPAN></DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial size=2>Basically, as you
have found out, the main reason that your idea doesn't work is that VTK is
not thread safe.</FONT></SPAN></DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial size=2>One way that you can
try is, once you enter the method associated with A, you essentially take over
the</FONT></SPAN></DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial size=2>system event queue.
Somewhere inside the infinite loop, you check the system event queue to see
if button </FONT></SPAN></DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial size=2>B has been
pressed. For my </FONT></SPAN><SPAN class=301013917-01042002><FONT
face=Arial size=2>application, this strategy works well with JDK1.3.1, but
somehow doesn't </FONT></SPAN></DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial size=2>work
with JDK1.4.0. It seems </FONT></SPAN><SPAN
class=301013917-01042002><FONT face=Arial size=2>to me that the system event
queue is not implemented correctly in JDK1.4.0.</FONT></SPAN></DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial size=2>Another way to solve
your problem is to get rid of the infinite loop in the method
associated with A. When</FONT></SPAN></DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial size=2>button A is
pressed, what you do is that you create and start a thread,
say MyThread. After that, method A </FONT></SPAN></DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial size=2>executes
</FONT></SPAN><SPAN class=301013917-01042002><FONT face=Arial size=2>ONLY ONCE
the code that you have in the original infinite loop. Now, when button B is
pressed, </FONT></SPAN></DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial
size=2>you </FONT></SPAN><SPAN class=301013917-01042002><FONT face=Arial
size=2>basically set some boolean flag in your main application.
The key idea is how you design MyThread. </FONT></SPAN></DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial size=2>What
</FONT></SPAN><SPAN class=301013917-01042002><FONT face=Arial
size=2>I would do is let the thread sleep for approximately the time
which the code in the original infinite loop </FONT></SPAN></DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial size=2>takes
</FONT></SPAN><SPAN class=301013917-01042002><FONT face=Arial size=2>to execute.
When the thread wakes up, it takes one of the following two
choices. If the boolean flag </FONT></SPAN></DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial size=2>in the main
application has </FONT></SPAN><SPAN class=301013917-01042002><FONT face=Arial
size=2>been set by method B, MyThread terminates without doing
anything. On the </FONT></SPAN></DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial size=2>other hand, if the
boolean flag has not been </FONT></SPAN><SPAN class=301013917-01042002><FONT
face=Arial size=2>changed, you basically ARTIFICIALLY generate a button
A </FONT></SPAN></DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial size=2>pressed event and
add it to the system event </FONT></SPAN><SPAN class=301013917-01042002><FONT
face=Arial size=2>queue of the main application. The system event queue
</FONT></SPAN></DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial size=2>will then do the
scheduling job for you automatically and execute the code in method A.
</FONT></SPAN><SPAN class=301013917-01042002><FONT face=Arial size=2>Personally,
I </FONT></SPAN></DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial size=2>found coding in this
way is more </FONT>elegant, since you may not want to mess up with the
system event</SPAN></DIV>
<DIV><SPAN class=301013917-01042002>queue.</SPAN></DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial color=#000000 size=2>Hope
this helps.</FONT></SPAN></DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=301013917-01042002><FONT face=Arial size=2>Lichan
Hong</FONT></SPAN></DIV>
<BLOCKQUOTE>
<DIV class=OutlookMessageHeader dir=ltr align=left><FONT face=Tahoma
size=2>-----Original Message-----<BR><B>From:</B> lian jiang
[mailto:jl_vtk@yahoo.com]<BR><B>Sent:</B> Friday, March 29, 2002 9:00
PM<BR><B>To:</B> vtkusers<BR><B>Subject:</B> [vtkusers] a problem about
multithread and VTK again<BR><BR></FONT></DIV>
<P>Hi, all: </P>
<P> I posted
the mail once but did not receive any response. So I have to post it
again and hope someone can give me a help. Sorry to bother you all.
<BR> I met a problem about VTK
rendering window and multithread. In my JAVA-VTK program, I have two
button, A and B. In the method associated with A, there is an infinite loop,
in which the actors are rendered from the first frame to last frame repeatedly
in a rendering window. I set a globale variable "boolean jump=false". The
infinite loop should exit by judging the value of "jump". In the method
associated with button B, I set the variable as
true.<BR> When I click A, the
program will enter the infinite loop. I intend to exit the loop whenever I
click the button B. But here is a problem. Once the program enters the dead
loop, the GUI seems to be stucked and cann't respond to any user action. So
the method of B can not set any global variable.
<BR> From the view of thread,
it is easy to understand--because the two buttons are both in the GUI thread,
the dead loop will surely block the GUI thread so that button B is
disabled. I tried to solve the problem by multithread.
Although the loop can exit as I imaged, there still are some
problems. If I put the dead loop in a thread, the rendering window can
not be refreshed. IF I put the method of B into a thread, button B will also
be disable once the program goes into the dead loop. I don't know
whether it is a common problem for all kinds of window refreshing. I had
studied multithread used in VTK for some time. As far as I know, VTK window
will cause some problems in multithread because VTK calls opengl which is not
safe for thread. But I am not sure whether the refreshing problem described
above is caused by VTK or other reasons. Is it a common problem for all
kinds window refreshing? Is there a way to solve this problem?
Thank you very much. </P>
<P><BR>BEST WISHES </P>
<P>Lian <BR></P>
<P><BR>
<HR SIZE=1>
<B>Do You Yahoo!?</B><BR><A
href="$rd_url/welcome/?http://greetings.yahoo.com">Yahoo! Greetings</A> - send
greetings for <A
href="$rd_url/welcome/?http://greetings.yahoo.com/browse/Holidays/Easter/">Easter</A>,
<A
href="$rd_url/welcome/?http://greetings.yahoo.com/browse/Holidays/Passover/">Passover</A></BLOCKQUOTE></BODY></HTML>