www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.dwt - Thread.start doesn't work?

reply Plumm <sailormoontw gmail.com> writes:
Hi:

  I just tried a DWT problem with a heavy file processing bound to a
button.That's why I chose to use Thread, or the background thread/GUI thread
would be locked and no refresh.

  The problem is the thread seems do nothing. And even the isRunning returns
false right after calling start.

  The environment is dwt-win 3.4.1 with tango 0.99.7 / DMD 1.033 which is right
the version downloaded from the DWT download page.

  Thanks for any answer.
Jul 31 2008
parent reply Frank Benoit <keinfarbton googlemail.com> writes:
Plumm schrieb:
 Hi:
 
   I just tried a DWT problem with a heavy file processing bound to a
button.That's why I chose to use Thread, or the background thread/GUI thread
would be locked and no refresh.
 
   The problem is the thread seems do nothing. And even the isRunning returns
false right after calling start.
 
   The environment is dwt-win 3.4.1 with tango 0.99.7 / DMD 1.033 which is
right the version downloaded from the DWT download page.
 
   Thanks for any answer.

can you post your code? Does your thread start if you execute the button handler code without GUI?
Jul 31 2008
parent reply Plumm <sailormoontw gmail.com> writes:
The code is as follows: 
The problem is if some exception happens, it should open up a
messagebox(messagebox works without being inside a thread), and I haven't
tested the code without the DWT environment, but I think it might be the Tango
problem, not the DWT problem. This code works if it's not in a thread though.

and there is a thread.start() right after the Thread definition. Note :
text_file_input, text_file_output, button_convert, and thread are all declared
as static scope.

           thread = new Thread(
           {
             int int_lines = 0;
             FileConduit file_input  = null;
             FileConduit file_output = null;
             DataOutput  file_out    = null;
             
             try
             {
               file_input  = new FileConduit (text_file_input.getText());
               file_output = new FileConduit (text_file_output.getText(),
FileConduit.WriteCreate);
               file_out    = new DataOutput (file_output);

               foreach (str_line; new LineInput(file_input))
               {
                 int_lines++;
             
                 int int_last_index = 0;
                 foreach ( int_index, int_item ; int_idn_locations)
                 {
                   int int_item1 = int_item -1 ;
                 
                   if (int_item1 + 10 > str_line.length)
                     break;
               
                   if ((int_item1 != 0) && (int_last_index != int_item1))
                     file_out.write (str_line[int_last_index..int_item1]);
                   
                   if (str_line[int_item1..int_item1+10] != "          ")
                    
file_out.write(cjidn_encode(str_line[int_item1..int_item1+10]));
                   else
                     file_out.write(cast(char[])("          "));
                   
                   int_last_index = int_item1 + 10;
                 }
                 file_out.write(str_line[int_last_index..length]);
                 file_out.write("\r\n");
                 
                 Thread.sleep(0.001);
               }
               
               auto sprint0 = new Sprint!(char);
               MessageBox messageBox0 = new
MessageBox(DWT.OK|DWT.ICON_INFORMATION);
               messageBox0.setText("Done");
               messageBox0.setMessage(sprint0("{} Lines", int_lines));
               messageBox0.open();
             }
             catch (Exception o)
             {
               auto sprint = new Sprint!(char);
               MessageBox messageBox = new MessageBox(DWT.OK|DWT.ICON_ERROR);
               messageBox.setText("Error");
               messageBox.setMessage(sprint("Error : {}", o.msg));
               messageBox.open();
             }
             finally
             {
               if (!(file_out is null))
                 file_out.flush();
               if (!(file_output is null))
                 file_output.close();
               if (!(file_input is null))
                 file_input.close();
               button_convert.setEnabled(true);
             }
           });
Jul 31 2008
parent reply Frank Benoit <keinfarbton googlemail.com> writes:
Plumm schrieb:
 The code is as follows: The problem is if some exception happens, it
 should open up a messagebox(messagebox works without being inside a
 thread), and I haven't tested the code without the DWT environment,
 but I think it might be the Tango problem, not the DWT problem. This
 code works if it's not in a thread though.
 

It is not allowed to access any DWT function from a thread that is not the "GUI thread". The calls to MessageBox in the thread might throw exception, because they detect an illegal thread access. To solve this, there is Display.syncExec and asyncExec. Display.getCurrent().syncExec( dgRunnable({ MessageBox messageBox0 = new MessageBox(DWT.OK|DWT.ICON_INFORMATION); messageBox0.setText("Done"); messageBox0.setMessage(sprint0("{} Lines", int_lines)); messageBox0.open(); }));
Jul 31 2008
parent reply Plumm <sailormoontw gmail.com> writes:
Thanks for the response.

However, I think it's the problem that threading doesn't work. Even if I remove
everything in the thread and simply but a static variable change in the thread.
It's not executed either.

A bit wierd....
Aug 01 2008
parent reply Frank Benoit <keinfarbton googlemail.com> writes:
Plumm schrieb:
 Thanks for the response.
 
 However, I think it's the problem that threading doesn't work. Even if I
remove everything in the thread and simply but a static variable change in the
thread. It's not executed either.
 
 A bit wierd....

ok, i would like to take a look at your problem. But i would need a simple to compile and execute example.
Aug 01 2008
parent reply Plumm <sailormoontw gmail.com> writes:
Ignore the previous post...I made a mistake by creating a new class that
extends Thread overriding the run function. 

And now it works. Problem solved...

Thanks for the help.
Aug 01 2008
parent Sean Kelly <sean invisibleduck.org> writes:
Plumm wrote:
 Ignore the previous post...I made a mistake by creating a new class that
extends Thread overriding the run function. 

Thread.run is private, it can't be overridden :-)
 And now it works. Problem solved...

Glad to hear it. Sean
Aug 04 2008