Note that there are some explanatory texts on larger screens.

plurals
  1. POClasses calling a common method - delegate?
    primarykey
    data
    text
    <p>I have a form which creates a BackgroundWorker. The worker then does some work, and does a ReportProgress to send messages back to the UI while it's busy. I then log those messages to the UI. The ReportProgress is done by a single method in the thread, which I called 'Notify'.</p> <p>However, within the thread, I call a static class to do file access type work. That static class needs to also report progress... so I need to call the Notify method of the thread class that called the static class - but, I have no access to the methods within the calling class. </p> <p>So, UI creates thread... and thread uses a class, but I need to call the Notify method in the class, from the static class. How can I do this?</p> <p>This is my attempt to far. The idea is to try use a delegate... but, I am stuck at actually using the delegate. :)</p> <p>In my thread class, I have a method:</p> <pre><code>public void Notify(string message, Constants.ErrorLevel errorLevel) { var su = new StatusUpdate {StatusMessage = message, ErrorLevel = 0}; _bw.ReportProgress(0, su); } </code></pre> <p>This is working. It reports well to the calling UI.</p> <p>I now created a delegate in this class:</p> <pre><code>public delegate bool NotificationsDelegate(object MessageHolder); </code></pre> <p>I have changed my static class for handing file management, to non-static, and am wanting to pass that delegate to the file manager class when I create it:</p> <pre><code>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); } </code></pre> <p>So, I create it, and pass the Notification delegate... and then in my 'SendMessageBack' method, hope to call the delegate (called _notifications).</p> <p>But that's where I am stuck. The delegate isn't assigned to the notify method yet. I'm new to events, so guessing my way around. But, can someone help me get this right?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload