c# - Classes calling a common method - delegate? -


i have form creates backgroundworker. worker work, , reportprogress send messages ui while it's busy. log messages ui. reportprogress done single method in thread, called 'notify'.

however, within thread, call static class file access type work. static class needs report progress... need call notify method of thread class called static class - but, have no access methods within calling class.

so, ui creates thread... , thread uses class, need call notify method in class, static class. how can this?

this attempt far. idea try use delegate... but, stuck @ using delegate. :)

in thread class, have method:

public void notify(string message, constants.errorlevel errorlevel) {     var su = new statusupdate {statusmessage = message, errorlevel = 0};     _bw.reportprogress(0, su); } 

this working. reports calling ui.

i created delegate in class:

public delegate bool notificationsdelegate(object messageholder); 

i have changed static class handing file management, non-static, , wanting pass delegate file manager class when create it:

public class filemanager { private readonly notificationsdelegate _notifications;  public filemanager(notificationsdelegate notifications) {     _notifications = notifications; }  private void sendmessageback(string p, consolecolor consolecolor) {     var su = new statusupdate {errorlevel = 0, statusmessage = p};     _notifications(su); } 

so, create it, , pass notification delegate... , in 'sendmessageback' method, hope call delegate (called _notifications).

but that's stuck. delegate isn't assigned notify method yet. i'm new events, guessing way around. but, can me right?

if plan share 1 instance of filemanager class between threads, yes absolutely have pass in delegate instance every time call it. if create new instance of filemanager class in each "thread class", can give filemanager delegate in ctor have written.

public delegate void notifydelegate(string message, constants.errorlevel errorlevel);  public class backgroundworker {     public backgroundworker() {         _filemgr = new filemanager(notify);     }      public void notify(string message, constants.errorlevel errorlevel) {         // stuff     } }  public class filemanager {     public filemanager(notifydelegate notification) {         _notification = notification;     }      public void sendmessageback() {         _notification("foo", 0);     } } 

if want can use lambda's , avoid having create delegates directly:

public class filemanager {     public filemanager(action<string, constants.errorlevel> notifyaction) {         _notification = notifyaction;     }      public void sendmessageback() {         _notification("foo", 0);     } }  public class backgroundworker {     public backgroundworker() {         _filemgr = new filemanager((a, b) => notify(a, b));     } } 

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 -