c# - Winforms and Background Worker -


i have console app @ moment, monitors folder files, , based on rules , file name, copies new file location on network.

i have requirement make application more pretty, decided go simple winforms single form application displays status , 'last updated file' type information.

the console app written in such way console display information went through single method, called 'notify', taking 2 parameters. string display information want user see, , errorlevel enum, which, if 'normal' displayed in green text, if warning, yellow, , if error, red. point is, code did use 'notify' method output text.

i want change console app normal class, run background worker winforms project, , have notify method in thread send updates winforms app, safely. think can done events, not sure best way handle this. propose method working?

there's 'invoke' way of doing things. good? like:

this.begininvoke (new methodinvoker(() => updatelabel(s)); 

it seems basic, i'd still make use of notify method, , have send messages ui layer.

i need console app send messages thread. example, 'stop', run code gracefully quits thread... , also, 'refresh', logic within thread.

another option run processing class service? , have ui somehow connects system service , gets updates? have never done that, process meant run time...

at moment, have code running, no updated ui:

public partial class mainform : form {     backgroundworker _bw = new backgroundworker();     public mainform()     {         initializecomponent();     }      private void mainform_load(object sender, eventargs e)     {          _bw.dowork += bw_dowork;         _bw.runworkerasync();     }      private void bw_dowork(object sender, doworkeventargs e)     {         var bw = sender backgroundworker;         var monitor = new monitor();         monitor.runme();      } } 

i think need find way notify method in thread send message or (an object create, message string , errorcode properties?) ui, , process safely on ui.

and here code within class (thread)...

public class monitor     {         public void runme()         {             notify("checking network connectivity...", constants.errorlevel.information);             if (filemanagement.folderexists(constants.foldertomonitor) == false)             {                 notify("unable monitor folder - aborting.", constants.errorlevel.error);                 console.readkey();                 return;             }             notify("ok", constants.errorlevel.information); .... } 

note: readkey removed..

there may better approach, if notify method needs interact gui, try using reportprogress event on backgroundworker. can pas object state parameter, , ignore progress value.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -