c# - Trying to understand a logreader -


i trying understand code. small program prints out log data. done creating form datagridview filled datatable. form class has refresh function (refreshpresentation). businesslogic class actual work of updating datatable , calling refresh function in form. pretty understand functionality, not why program structured way is.

  1. why businesslogic.dowork run thread instead of normal method call?
  2. can explain refreshpresentation function me? (begininvoke , delegate)
  3. is idea/practice pass mainform parameter businesslogic?

this main entry point application.

public class program     {         [stathread]         static void main()         {             application.enablevisualstyles();             application.setcompatibletextrenderingdefault(false);             application.run(new mainform());                     }     } 

this relevant part of form.

public partial class mainform : form {     private businesslogic businesslogic;     private datatable viewdatatable;      public mainform()     {         initializecomponent();          businesslogic = new businesslogic(this);         thread t = new thread(new threadstart(businesslogic.dowork));         t.start();     }      public delegate void refreshpresentationdelegate(datatable datatable);      public void refreshpresentation(datatable datatable)    {         if (this.invokerequired)         {             this.begininvoke(new refreshpresentationdelegate(refreshpresentation), new object[] { datatable });             return;         } ... 

this business logic.

internal class businesslogic     {         private mainform form;         private logging.dal.logger loggerdal;         private int lastid;          internal datatable datatable { get; private set; }         internal bool isrunning { get; set; }          public businesslogic(mainform form)         {             this.form = form;             this.loggerdal = new logging.dal.logger();             this.isrunning = true;             datatable = new datatable();         }          public void dowork()         {             while (this.isrunning)             {                 // new log messages.                 if (datatable.rows.count > 0)                     this.lastid = (int)datatable.rows[datatable.rows.count - 1]["id"];                  this.datatable = loggerdal.getlogmessagessincelastquery(lastid);                  // callback gui update.                 form.refreshpresentation(this.datatable);                  // wait next refresh.                 thread.sleep(1000);             }         }      } 

q1.why businesslogic.dowork run thread instead of normal method call?

a1. dowork needs on separate thread main gui thread, since main gui thread needs free pump message queue (which allows redraw itself, handle different gui events, etc.) try create simple gui program has while(true) in main thread , see gui gets stuck , doesn't redraw itself.

q2.can explain refreshpresentation function me? (begininvoke , delegate)

a2. though dowork needs done on thread doesn't block gui thread, updating gui needs done gui thread. in order make happen, can call begininvoke, posts message message queue , causes delegate executed on gui thread.

q3.is idea/practice pass mainform parameter businesslogic?

a3. no. mainform can know business logic, business logic should not aware of gui. google "mvc" more information on separating gui business logic.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -