Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » asyncexec() and jobs and main UI blocking(RCP, asyncexec() and jobs)
asyncexec() and jobs and main UI blocking [message #785809] Sun, 29 January 2012 14:30 Go to next message
Jason Robinson is currently offline Jason RobinsonFriend
Messages: 2
Registered: January 2012
Junior Member
Hi All - thanks for the help in advance. To start I've been struggling with this for quite some time and have searched the web and forums for help to no avail. Also note that I am new to both RCP and threads.

The idea is simple. I have built a RCP application with a Viewpart1 class. Within the class it parses some data and builds two tables with DateTime objects.

I want a second class - on a separate thread to grab current date time from platform and compare to the TableItem DateTime - perform some checking (checking not important) and highlight the TableItem DateTime.

Anyway as you can see I am updating the UI from another thread - when looking on line I realize that I need to use asyncexec() and potentially the jobs class.

Finally - my problem is that I am blocking the main thread somewhere / somehow. And I don't know why or how to fix it. Keep in mind I'm new to this. I've posted the two relevant classes. All help is appreciated.

- Jason


public class ViewPart1 extends ViewPart {
	
	public ViewPart1() {
	
	}	
	
	Menu fileMenu;
	Menu helpMenu;
	
	public void createPartControl(Composite parent) {
		
		
		TableControl tc = new TableControl();
		TableControl mc = new TableControl();
		
		Table MyTable = tc.createATSTable(parent);
		Table YourTable = mc.createATSTable(parent);
		
		ParseMissionPlanningAutomatedTimeSequenceFile ParsedFile = new ParseMissionPlanningAutomatedTimeSequenceFile();
       
		ParsedFile.OpenFile();
		ParsedFile.ParseDates();		 
		ParsedFile.ParseCommands();
		
		int arraySize = ParsedFile.ReturnSize();
		
		for (int i=0; i<(arraySize); i++) {
			
			tc.addTableItem(MyTable, ParsedFile.ReturnElement(i).toString(), ParsedFile.ReturnCommand(i));
			mc.addTableItem(YourTable, ParsedFile.ReturnElement(i).toString(), ParsedFile.ReturnCommand(i).toString()); 	
		}
		
		
		//TimeJob ts = new TimeJob();
		//ts.Sweep(MyTable);
		
	}


	public void setFocus() {
		
	}

	
	
}




public class TimeFlow {

	DateFormat atsFormat = new  SimpleDateFormat ("yyyy/MM:dd:HH:mm:ss");
	
	public void run(final Table passedTable){
		Display.getDefault().asyncExec(new Runnable() {

	    	   public void run() {
	    	           
	
	    	    	   
	    	    	   int counter = 0; 
	    	    	   int  numberOfRows = passedTable.getItemCount();
	    	    	   Date returnDate;
	    			
	    	    	   while (counter != numberOfRows ) {  
	    			    
	    	    		   // This counter steps through the tableItems
	    	    		   counter = counter + 1; 
	    				
	    	    		   // Continually grab new Date and Time 
	    	    		   Date date = new Date();
	    				
	    	    		   // Format that Date and Time into what we see in the ATS
	    	    		   atsFormat.format(date);
	    			    
	    	    		   // Extract date from table and attempt to parse
	    	    		   try {
	    			        
	    	    			  // String StringDate = passedTable.getItem(counter).getText(1);
	    	    			   returnDate = atsFormat.parse(passedTable.getItem(counter).getText(1));
	    				    
	    	    			   System.out.println(returnDate);
	    				    
	    	    		   } catch (ParseException e1) {
	    					
	    	    			   e1.printStackTrace();
	    	    		   }
	    			     
	    	    		   System.out.println(atsFormat.format(date));
	    			   
	    	    		   try { 
	    	    			   Thread.sleep(1000);     
	    	    		   } catch (InterruptedException e) { 
	    	               
	    	    		System.out.println(e); 
	    	           
	    	    		} 
	    			
	    			}
	    	
	    	   }

		});
	    	   }

	       
	       
}





Re: asyncexec() and jobs and main UI blocking [message #785819 is a reply to message #785809] Sun, 29 January 2012 14:47 Go to previous messageGo to next message
David Wegener is currently offline David WegenerFriend
Messages: 1445
Registered: July 2009
Senior Member
On 01/29/2012 08:30 AM, Jason Robinson wrote:
> Hi All - thanks for the help in advance. To start I've been struggling
> with this for quite some time and have searched the web and forums for
> help to no avail. Also note that I am new to both RCP and threads.
> The idea is simple. I have built a RCP application with a Viewpart1
> class. Within the class it parses some data and builds two tables with
> DateTime objects.
> I want a second class - on a separate thread to grab current date time
> from platform and compare to the TableItem DateTime - perform some
> checking (checking not important) and highlight the TableItem DateTime.
> Anyway as you can see I am updating the UI from another thread - when
> looking on line I realize that I need to use asyncexec() and potentially
> the jobs class.
> Finally - my problem is that I am blocking the main thread somewhere /
> somehow. And I don't know why or how to fix it. Keep in mind I'm new to
> this. I've posted the two relevant classes. All help is appreciated.
>
> - Jason
>
>
>
> public class ViewPart1 extends ViewPart {
>
> public ViewPart1() {
>
> }
>
> Menu fileMenu;
> Menu helpMenu;
>
> public void createPartControl(Composite parent) {
>
>
> TableControl tc = new TableControl();
> TableControl mc = new TableControl();
>
> Table MyTable = tc.createATSTable(parent);
> Table YourTable = mc.createATSTable(parent);
>
> ParseMissionPlanningAutomatedTimeSequenceFile ParsedFile = new
> ParseMissionPlanningAutomatedTimeSequenceFile();
> ParsedFile.OpenFile();
> ParsedFile.ParseDates(); ParsedFile.ParseCommands();
>
> int arraySize = ParsedFile.ReturnSize();
>
> for (int i=0; i<(arraySize); i++) {
>
> tc.addTableItem(MyTable, ParsedFile.ReturnElement(i).toString(),
> ParsedFile.ReturnCommand(i));
> mc.addTableItem(YourTable, ParsedFile.ReturnElement(i).toString(),
> ParsedFile.ReturnCommand(i).toString());
> }
>
>
> //TimeJob ts = new TimeJob();
> //ts.Sweep(MyTable);
>
> }
>
>
> public void setFocus() {
>
> }
>
>
>
> }
>
>
>
>
>
> public class TimeFlow {
>
> DateFormat atsFormat = new SimpleDateFormat ("yyyy/MM:dd:HH:mm:ss");
>
> public void run(final Table passedTable){
> Display.getDefault().asyncExec(new Runnable() {
>
> public void run() {
>
> int counter = 0; int numberOfRows = passedTable.getItemCount();
> Date returnDate;
>
> while (counter != numberOfRows ) { // This counter steps through the
> tableItems
> counter = counter + 1;
> // Continually grab new Date and Time Date date = new Date();
>
> // Format that Date and Time into what we see in the ATS
> atsFormat.format(date);
> // Extract date from table and attempt to parse
> try {
> // String StringDate = passedTable.getItem(counter).getText(1);
> returnDate = atsFormat.parse(passedTable.getItem(counter).getText(1));
> System.out.println(returnDate);
> } catch (ParseException e1) {
>
> e1.printStackTrace();
> }
> System.out.println(atsFormat.format(date));
> try { Thread.sleep(1000); } catch (InterruptedException e) {
> System.out.println(e); }
> }
>
> }
>
> });
> }
>
> }
>
>
>
>
>
>
The run method you pass to asyncExec is called by the UI thread. This
means that the only thing you should be doing in the method is updating
the relevant UI elements and then returning. You have put an endless
loop in the run method. While the UI thread is in this endless loop, it
can't be updating any UI elements and your display locks up.

Your loop needs to be in a separate thread. When the loop needs to
update the display, your code needs to construct a runnable that you
then pass to asyncExec. The body of this runnable should update the
display.
Re: asyncexec() and jobs and main UI blocking [message #788288 is a reply to message #785819] Wed, 01 February 2012 15:57 Go to previous message
David Casas is currently offline David CasasFriend
Messages: 58
Registered: November 2011
Location: Barcelona
Member

The class TimeFlow should not be in the SWT UI Thread as it is doing nothing with the table graphics (it just gets some values), so it must be an independent Thread as David said.
Previous Topic:Add MRU to File menu defined in plugin.xml ?
Next Topic:RCP is not showing my intro page
Goto Forum:
  


Current Time: Sat Apr 27 05:15:59 GMT 2024

Powered by FUDForum. Page generated in 0.02906 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top