Confusing code [message #769863] |
Thu, 22 December 2011 20:00 |
Bill Brandley Messages: 12 Registered: October 2011 |
Junior Member |
|
|
I wrote a program and I got it to work but don't know why it works. Also, I don't think that what I did is the correct way to code.
Here's what is going on.
I click on a RUN button and execute the code below which is in the MouseUp event handler.
1-2: A thread object is initialized and the thread is started.
5 The RUN button is disabled.
6-7 A LABEL is displayed that will be updated to indicate file name as the thread progresses.
9-20 This is the code I don't understand:
In it, I fetch the latest file name from the thread (line 14) and update the LABEL (line 17).
It runs correctly (for the most part). The code in lines 9-20 I do not understand nor do I think this is the recommended method to monitor the progress of a thread.
1 running Scan = new Thread(scanFiles);
2 runningScan.start();
3
4
5 btnRun.setEnabled(false);
6 lblProgress.setVisible(true);
7 lblProgress.setText("");
8
9 while(runningScan.isAlive()) {
10 getShell().getDisplay().sleep();
11
12 while(getShell().getDisplay().readAndDispatch()) {
14 String currentFile = scanFiles.getCurrentFileName();
15
16 if (currentFile != null) {
17 lblProgress.setText(currentFile);
18 }
19 }
20 }
21
22 btnRun.setEnabled(true);
23 lblProgress.setVisible(false);
I couldn't find an idle loop in which I could monitor the progress of the thread and I said that this works for the most part since I found that the LABEL will not update if I add any additional controls to the window though this is an unrelated problem.
I'd appreciate any insight that can be provided.
Thanx,
Bill
|
|
|
Powered by
FUDForum. Page generated in 0.08418 seconds