vb.net - Tracking elapsed time for a set of Tasks -
i trying write little vb.net application conduct cross team time in motion study in work. need ability have multipul timers running single tasks, , when user has finished tasks can end , stored in csv file. can single tasks @ once, , has been running while within business. best method have multipul tasks running @ once. had thought of adding tasks listview , having double click event end task function. can't seem timers run independantly. can help? create sense?
this main mass of workload in class. if doesnt create sense appologise in advance. i'm not programmer trade, it's side hobby practising in work.
another thing have elapsed seconds counting live, can label need unlitmited amount of task in theory...
public class runningtask dim timenow datetime = dim elapsed dim localtask = globalvars.task public sub additem() createtimer() dim str(3) string dim itm listviewitem str(0) = localtask str(1) = timenow str(2) = elapsed itm = new listviewitem(str) frmtime.lvtimers.items.add(itm) end sub private sub createtimer() dim tmr new timer tmr.interval = 1000 tmr.enabled = true addhandler tmr.tick, addressof globaltimertick end sub private sub globaltimertick(byval sender object, byval e eventargs) dim t timespan = - timenow elapsed = string.format("{0:00}:{1:00}:{2:00}:{3:00}", math.floor(t.totalhours), t.minutes, t.seconds, t.milliseconds) end sub end class
i took more oop approach may more economical far timers, has simple interface. core taskitem tracks job , time, collection class expose them, start them , stop them. burying much functionality @ low of level possible things simpler.
i used 1 timer , cant see why you'd need multiple timers single task. if taskitem records datetime when starts (this happens automatically when created in mine), don't need timers @ all. elapsed calculated using startedtime , either datetime.now (for running task) or completedtime completed tasks. cant see timer adds anything.
you'll have create many mods purposes. not save completed items, nor remove them list, may give place start. first-pass thing; there certainly issues here , there.
imports system.collections.objectmodel public class tasks ' slight overkill private enum lvgroups running completed end enum public enum taskjobs sweepfloor buildrobot cleanoven repairrobot emptytrash teachrobottotalk makecoffee teachrobottowalk washdishes teachrobotvisualbasic end enum ' class single task public class taskitem ' these ought readonly; no cheating public property id string public property empname string public property startedtime datetime public property completedtime datetime ' or job code... public property job taskjobs ' set in classlib or namespace friend sub new(sname string, tj taskjobs) empname = sname job = tj startedtime = datetime.now completedtime = datetime.maxvalue id = system.guid.newguid.tostring end sub public function isactive() boolean homecoming (completedtime = datetime.maxvalue) end function public sub endtask() completedtime = datetime.now end sub public function getelapsed() string dim t timespan if isactive() t = datetime.now - startedtime else t = completedtime - startedtime end if homecoming string.format("{0:00}:{1:00}:{2:00}", math.floor(t.totalhours), t.minutes, t.seconds) end function public overrides function tostring() string homecoming string.format("{0} {1} {2}", empname, job.tostring, getelapsed) end function end class private col collection(of taskitem) private mylv listview ' assign lv when tasks created public sub new(lvtask listview) mylv = lvtask col = new collection(of taskitem) end sub ' add together new task empname , jobcode public sub newtask(sname string, tj taskjobs) dim ti new taskitem(sname, tj) col.add(ti) ' lv layout: empname, job, elapsed, id ' 3 columns id "hidden" dim lvi new listviewitem(ti.empname) lvi.subitems.add(ti.job.tostring) lvi.subitems.add(ti.getelapsed) lvi.subitems.add(ti.id) lvi.group = mylv.groups(lvgroups.running.tostring) mylv.items.add(lvi) end sub ' called dbl click on running task item public sub stoptask(lvi listviewitem) dim ti taskitem = (from t in col t.id = lvi.subitems(3).text).first ' form doesnt check status, must if ti.isactive ti.endtask() lvi.group = mylv.groups(lvgroups.completed.tostring) end if end sub ' called form timer tick event public sub updatedisplay() dim ti taskitem ' iterate items id of runners each lvi listviewitem in mylv.groups(lvgroups.running.tostring).items ' todo add together error checking ti = (from t in col t.id = lvi.subitems(3).text).first lvi.subitems(2).text = ti.getelapsed next end sub public function runnerscount() integer homecoming getactivelist.count end function public function getcompletedlist() list(of taskitem) ' long form dim tlist new list(of taskitem) each ti taskitem in col if ti.isactive = false tlist.add(ti) end if next homecoming tlist ' short form 'dim list = (from t in col t.isactive = false).tolist end function public function getactivelist() list(of taskitem) ' long form 'dim tlist new list(of taskitem) 'for each ti taskitem in col ' if ti.isactive ' tlist.add(ti) ' end if 'next 'return tlist ' short form dim list = (from t in col t.isactive).tolist homecoming list end function end class usage:
public class form1 private mytasks tasks private sub form1_load(sender object, e eventargs) handles mybase.load mytasks = new tasks(me.lvtask) cbotask.items.addrange([enum].getnames(gettype(tasks.taskjobs))) end sub add task:
mytasks.newtask(cboemp.text, ctype(cbotask.selectedindex, tasks.taskjobs)) stop task:
' called lv mousedoubleclick, determine item dim lvi listviewitem = lvtask.getitemat(e.x, e.y) mytasks.stoptask(lvi) obviously, stopping task might automatically append info export file. functionality in task collection class, done before task removed collection (optional - arent hurting thing there long can tell active completed ones).
timer tick event:
mytasks.updatedisplay() finally, turn on option strict since people making decisions based on project's output.
vb.net timer stopwatch elapsedtime
No comments:
Post a Comment