Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two likely options here:</p> <p>The "other class" needs to update the textbox as soon as it's "done" with it's work. If this is the case it should't ever know about the form, any textbox, an interface, nothing. It should just <em>return a value</em> and let the form use that return value to set a textbox, or do whatever.</p> <p>In most situations this is what you want to do, and it's both the easiest and most effective means of solving this type of problem. Don't use something more complex if you don't have to.</p> <p>If the information doesn't happen when the task is "done", but instead through periodic intervals then you can use the <code>Progress</code> class with the <code>IProgress</code> interface.</p> <p>Have the <code>Form</code> create an instance: <code>Progress&lt;string&gt; progress = new Progress&lt;string&gt;();</code>. have the form attach an event handler for that instance. Note that the <code>Progress</code> instance will "capture" the current sync context, which is a fancy way of saying it runs in the UI thread. This can be done like so:</p> <pre><code>progress.ProgressChanged += (_, data) =&gt; textBox1.Text = data; </code></pre> <p>Then just have the other class accept an <code>IProgress&lt;string&gt;</code> instance and periodically call:</p> <pre><code>progress.Report(someString); </code></pre> <p>If you need a pre-4.5 implementation of <code>Progress</code> and <code>IProgress</code>, here is an implementation that will compile and run in .NET 3.5+:</p> <pre><code>public interface IProgress&lt;T&gt; { void Report(T data); } public class Progress&lt;T&gt; : IProgress&lt;T&gt; { SynchronizationContext context; public Progress() { context = SynchronizationContext.Current ?? new SynchronizationContext(); } public Progress(Action&lt;T&gt; action) : this() { ProgressReported += action; } public event Action&lt;T&gt; ProgressReported; void IProgress&lt;T&gt;.Report(T data) { var action = ProgressReported; if (action != null) { context.Post(arg =&gt; action((T)arg), data); } } } </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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