c# - Backgroundworker updating log on the ui -


i have wpf application text box in main window supposed used display logging information while user runs long process.

<textbox grid.row="1" margin="10,10,10,10" acceptsreturn="true" name="txtlogging" textwrapping="wrapwithoverflow"                   text="{binding path=logtext, mode=twoway}" scrollviewer.horizontalscrollbarvisibility="auto" scrollviewer.verticalscrollbarvisibility="auto" />  public string logtext {     { return _logtext; }     set     {         _logtext = value;         onpropertychanged();     } } 

one of buttons on ui kicks off process takes minimum of 30 seconds, , few hours. needless say, running on background worker preferred. issue logging class in program being created on ui thread , has accessed during worker's execution update ui log of happening.

the logger looks this;

using system; using system.io;  namespace batchinvoice {     public enum logginglevel     {         verbose = 0,         info = 1,         warning = 2,         error = 3     }     public sealed class logger     {          string _logfile;         static logger() { }         public bool logtodatabase = false;         public bool logtofile = true;         public bool logtoscreen = false;         private logger()         {             //string filepath = environment.getfolderpath(environment.specialfolder.desktop);             string filepath = directory.getcurrentdirectory();             filepath = filepath + @"\logfiles";             string extension = ".log";             if (!directory.exists(filepath))             {                 directory.createdirectory(filepath);             }             /*string currentdir = environment.currentdirectory;             directoryinfo directory = new directoryinfo(currentdir);             string fulldirectory = directory.fullname;*/             string date = (datetime.now).tostring("yyyymmddhhmmss");             _logfile = filepath + "\\" + date + extension;             minimumlogginglevel = logginglevel.info;         }         private logginglevel minimumlogginglevel;         public static void setminimumlogginglevel(logginglevel minimum)         {             instance.minimumlogginglevel = minimum;         }         public static logginglevel getminimumlogginglevel()         {             return instance.minimumlogginglevel;         }         private static readonly logger instance = new logger();         public static logger instance         {                         {                 return instance;             }         }         public static void write(string content)         {             using (streamwriter filewriter = file.appendtext(instance._logfile))             {                 filewriter.writeline(content);             }         }         public static void write(string content, logginglevel warninglevel)         {             if (instance.minimumlogginglevel <= warninglevel)             {                 if (instance.logtofile)                 {                     using (streamwriter filewriter = file.appendtext(instance._logfile))                     {                         filewriter.writeline(warninglevel.tostring() + ": " + content);                     }                 }                 if (instance.logtoscreen)                     screenlogging.write(content, warninglevel);                 if (instance.logtodatabase)                 {                     //enter database loggign code here.                 }             }         }     } }  using system.windows; using system.windows.controls;  namespace batchinvoice {     public class screenlogging     {         private static screenlogging _instance;         private screenlogging() { }         public static screenlogging instance         {                         {                 if(_instance == null)                 {                     _instance = new screenlogging();                 }                 return _instance;             }         }         private textbox _target;         public static void settarget(textbox target)         {             instance._target = target;         }         public static void write(string content, logginglevel warninglevel)         {             //messagebox.show(content, warninglevel.tostring());             instance._target.appendtext(warninglevel.tostring() + ": " + content + "\n");         }     } } 

(yes there reason screenlogging separated different class, hope don't have change that) can make calls logging class reflect on ui within background worker? should change logtext property read external file or along lines? don't have background worker implemented, logging shows after task completed, need able monitor progress while running. when tried putting background worker errored when hit line of code tried access logger.

as problem seems not rewrite log calls, post way of doing it, changing screenlogging.write method. hope works you, since not need change calls logger.write method.

public class screenlogging {     private static screenlogging _instance;     private screenlogging() { }     public static screenlogging instance     {                 {             if (_instance == null)             {                 _instance = new screenlogging();             }             return _instance;         }     }     private textbox _target;     public static void settarget(textbox target)     {         instance._target = target;     }     public static void write(string content, logginglevel warninglevel)     {         var appendtextaction = new action(() =>         {             var text = warninglevel.tostring() + ": " + content + "\n";             instance._target.appendtext(text);         });          // thread dispatcher created on may access         // dispatcherobject directly. access dispatcherobject          // thread other thread dispatcherobject created on,         // call invoke , begininvoke on dispatcher dispatcherobject          // associated with.         // can set priority background, guarantee         // key operations processed first, , screen updating          // operations happen after operations done.         instance._target.dispatcher.invoke(appendtextaction,              dispatcherpriority.background);     } } 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -