Note that there are some explanatory texts on larger screens.

plurals
  1. POC# How can I update a form Control from foreign code
    text
    copied!<p>I'm trying to optimize my code to be called from both an UI-less <code>commandline</code> call or call it from the UI.</p> <p>The problem is that I have is I have written the lets call It <code>worker-code</code> inside the Form-class. Now I want to pull out that worker code into a separate class.</p> <p>Lets make a small sample to make my needs clearer:</p> <pre><code>public partial class form1 :Form { void AddLogmessage(String msg) { // update an listview ListViewItem item = new ListViewItem(); item.Text = msg; // Add the item to the ListView LogView.Items.Add(item); } // button on ui to start working private void btnStartTestRun_Click(object sender, EventArgs e) { try { DoSomeWork(); } catch(Exception ex) {} } private void DoSomeWork() { // do some really generic hard work.... AddLogMessage("working"); // do some more generic long lasting hard work.... AddLogMessage("working goes on..."); // in case of an error throw Exception } </code></pre> <p>Now I want to refcator the worker code to work outside the form class, but be able to report the things that happen to the UI (if there is one) or to call the workercode without UI and do other reportings to an different target (communicate with other library which reports the results to an server)</p> <p>Something like this:</p> <pre><code> public void AutomaticTaskHandler() { string[] cmdline = Environment.GetCommandLineArgs(); Arguments args = new Arguments(cmdline); if (args["automatic"] != null) { doSomeWork(); } } </code></pre> <p>In this case I don't have to report the Messages to the UI, but send some other messages (NOT the same Messages!!) to an server.</p> <p>So my question is how do I make this the best way not having to write the doSomeWork - code twice but be able to send only the messages which are in the current scene are needed?</p> <p>I thought about <code>Delegates</code> and <code>Events</code>, but I'm not too familiar to this to make this work.</p> <p>Any help will be appreciated.</p> <p>Thanks Meister_Schnitzel</p>
 

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