回调/委托?

发布于 2024-11-28 07:31:41 字数 559 浏览 1 评论 0原文

我有一个疑问..

我想创建一个函数,它看起来像这样...

public class A        //this is just a class file
{
    function dowork()
    {
        //work 1

        INPUT = here in this line it should call a delegate function or raise event etc...

        //work 2 using INPUT
    }
}

public class B
{
    function myfn()
    {
        A objA = new A();
        objA.dowork();

    }
}

在“A类”中,我们将引发事件左右&它将向用户显示一个窗口窗体,然后用户将输入一些值&我们需要将该值返回给 A 类 -> dowork 方法...那么只有我们应该继续“work 2”,

这也应该支持多线程...任何人都知道我们如何实现这个?

谢谢 :)

i have a doubt..

i would like to create a function and it will look like this...

public class A        //this is just a class file
{
    function dowork()
    {
        //work 1

        INPUT = here in this line it should call a delegate function or raise event etc...

        //work 2 using INPUT
    }
}

public class B
{
    function myfn()
    {
        A objA = new A();
        objA.dowork();

    }
}

In the "Class A" we will raise event or so & it will display a windows form to user and then user will input some value & we need to return that value to Class A -> dowork method.... then only we should continue "work 2"

this should also support multi threading... anyone have idea how we can implement this??

thanks :)

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

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

发布评论

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

评论(1

成熟的代价 2024-12-05 07:31:41

您可以使用 ManulResetEvent 达到此目的:您运行输入表单,完成后该表单设置事件,以便您可以从 A.dowork 方法捕获它。当输入运行时,您运行无限循环,检查事件状态并处理应用程序事件,以使您的应用程序在此时负责:

public class A        //this is just a class file
{
  private ManualResetEvent _event;

  public void dowork()
  {
    //work 1

    _event = new ManualResetEvent(false);
    //INPUT = here in this ...
    Worker worker = new Worker();
    worker.DoInput(_event);

    while(true)
    {
      if(_event.WaitOne())
        break;
      Application.DoEvents();
    }

    //work 2 using INPUT
  }
}

class Worker
{
  private ManualResetEvent _event;

  public void DoInput(ManualResetEvent @event)
  {
    _event = @event;

    // Show input form here. 
    // When it done, you call: _event.Set();
  }
}

另外,我建议您(如果可以)使用异步库(它可作为独立设置使用)。在那里,您可以以更直接的方式实现它:

public class A        //this is just a class file
{
  public async void dowork()
  {
    //work 1

    //INPUT = here in this ...
    Worker worker = new Worker();
    wait worker.DoInput();

    //work 2 using INPUT
  }
}

class Worker
{
  public async void DoInput()
  {
    InputForm form = new InputForm();
    wait form.ShowInput();
  }
}

public class B
{
  async void myfn()
  {
    A objA = new A();
    wait objA.dowork();
  }
}

正如您所看到的,您只需等待其他代码片段的执行,而无需任何 UI 锁定和事件。
如果您需要,我可以在这里提供有关异步/等待如何工作的更深入的解释。

You can use ManulResetEvent for this purpose: You run your input form and when it done that form set the event so you can catch it from A.dowork method. While the input in action you run the infinite loop, check event state and process application event to make you app responsible in this time:

public class A        //this is just a class file
{
  private ManualResetEvent _event;

  public void dowork()
  {
    //work 1

    _event = new ManualResetEvent(false);
    //INPUT = here in this ...
    Worker worker = new Worker();
    worker.DoInput(_event);

    while(true)
    {
      if(_event.WaitOne())
        break;
      Application.DoEvents();
    }

    //work 2 using INPUT
  }
}

class Worker
{
  private ManualResetEvent _event;

  public void DoInput(ManualResetEvent @event)
  {
    _event = @event;

    // Show input form here. 
    // When it done, you call: _event.Set();
  }
}

Also, I suggest you (if you can) use Async library (it is available as a standalone setup). There you can implement it in much more straightforward way:

public class A        //this is just a class file
{
  public async void dowork()
  {
    //work 1

    //INPUT = here in this ...
    Worker worker = new Worker();
    wait worker.DoInput();

    //work 2 using INPUT
  }
}

class Worker
{
  public async void DoInput()
  {
    InputForm form = new InputForm();
    wait form.ShowInput();
  }
}

public class B
{
  async void myfn()
  {
    A objA = new A();
    wait objA.dowork();
  }
}

As you see you just wait while other piece of code get executed without any UI locking and events.
I can provide deeper explanation of how async/wait works here if you need.

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