C# 如何从外部代码更新表单控件

发布于 2024-11-25 05:29:27 字数 1411 浏览 2 评论 0原文

我正在尝试优化我的代码,以便从无 UI 命令行 调用或从 UI 调用。

问题是我已经在 Form 类中编写了让我们称之为 worker-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

}

现在我想重新编写工作程序代码以在表单类之外工作,但能够报告 UI 上发生的事情(如果有)或调用工作程序代码而无需UI 并向不同的目标执行其他报告(与向服务器报告结果的其他库进行通信)

是这样的:

 public void AutomaticTaskHandler()
 {
   string[] cmdline = Environment.GetCommandLineArgs();
   Arguments args = new Arguments(cmdline);

   if (args["automatic"] != null)
   {
      doSomeWork();
   }
 }

在这种情况下,我不必向 UI 报告消息,而是发送一些其他消息(不是相同的消息!!)到 服务器。

所以我的问题是如何使这是最好的方法,而不必编写 doSomeWork - 代码两次,但能够仅发送当前场景中需要的消息?

我考虑过代表事件,但我对此不太熟悉,无法完成这项工作。

任何帮助将不胜感激。

谢谢 Meister_Schnitzel

I'm trying to optimize my code to be called from both an UI-less commandline call or call it from the UI.

The problem is that I have is I have written the lets call It worker-code inside the Form-class.
Now I want to pull out that worker code into a separate class.

Lets make a small sample to make my needs clearer:

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

}

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)

Something like this:

 public void AutomaticTaskHandler()
 {
   string[] cmdline = Environment.GetCommandLineArgs();
   Arguments args = new Arguments(cmdline);

   if (args["automatic"] != null)
   {
      doSomeWork();
   }
 }

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.

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?

I thought about Delegates and Events, but I'm not too familiar to this to make this work.

Any help will be appreciated.

Thanks Meister_Schnitzel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

李不 2024-12-02 05:29:27

基本上,您将使用 SendMessage 方法创建一个接口 IMessageTarget。您的 UI 代码将创建该接口的实现,将消息输出到 UI,而控制台代码将创建该接口的实现,将消息发送到服务器。在调用 doWork 方法时,您将提供 IMessageTarget 的实例。

Basically, you would create an interface IMessageTarget with a method SendMessage. Your UI code would create an implementation of that interface that outputs the messages to the UI and your console code would create an implementation of that interface that sends the messages to a server. On calling the doWork method, you would supply an instance of IMessageTarget.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文