简体   繁体   English

在我的对象C#中异步传递参数

[英]Pass argument asynchronously in my object c#

I have got a button in windows form application c# code which calls a created object called capturer. 我在Windows窗体应用程序C#代码中有一个按钮,该按钮调用了一个称为捕获器的已创建对象。 My code is the following: 我的代码如下:

private void button1_Click(object sender, EventArgs e)
{
      obj = new Capturer(dirPath + name + "_" + surname, switcher);
}

The default value of switcher (boolean variable) is false. 切换器的默认值(布尔变量)为false。 I want to change this value of switcher using the right click and pass it to the object real-time. 我想使用右键单击更改切换器的此值,并将其实时传递给对象。 I want the object to be updated every time the click is pressed. 我希望每次单击鼠标时都更新对象。 In order to detect mouse click events I have the following code: 为了检测鼠标单击事件,我使用以下代码:

 private void mouseClick1(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            Trace.WriteLine("Mouse clicked");
            switcher = true; 
        }
    }

How can I pass the variable in my object asynchronously? 如何在我的对象中异步传递变量?

Creating a new object on every mouse click will not help performance. 每次单击鼠标创建一个新对象都不会提高性能。 You might want to create a Capturer in the Form constructor, then call a method on it on a right mouse click. 您可能要在Form构造函数中创建一个Capturer,然后在鼠标右键单击上调用一个方法。

The name Capturer suggests you're trying to implement an observer pattern, but that's not exactly what's happening here; 名称Capturer暗示您正在尝试实现观察者模式,但这不完全是这里所发生的事情。 the form is the observer here, and the Capturer should be responsible for your business logic - and have a name explaining its purpose. 表单是这里的观察者,捕获器应负责您的业务逻辑-并有一个名称说明其用途。


(unless your business case is capturing mouse clicks...) (除非您的业务案例正在捕获鼠标点击...)

There are a couple solutions to your problem. 有两种解决方案来解决您的问题。 The easiest one ist to make switcher accessible outside of Capturer after creation. 创建switcher ,最简单的方法就是使switcher可在Capturer外部访问。 For instance you could turn it into a property: 例如,您可以将其变成一个属性:

public class Capturer
{
    public Capturer(string path, bool switcher)
    {
        Switcher = switcher;
        ...
     }

    public bool Switcher { get; set; }

    ...
}

Inside your click handler you set the Switcher value: 在点击处理程序中,您设置Switcher值:

private void mouseClick1(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        Trace.WriteLine("Mouse clicked");
        obj.Switcher = true; 
    }
}

In case you don't want to share the Capturer instance obj between button1_Click() and mouseClick1() you can also pass in a custom type into the Capturer constructor instead of a primitive value. 如果您不想在button1_Click()mouseClick1()之间共享Capturer实例obj ,则还可以将自定义类型(而不是原始值mouseClick1()传递到Capturer构造函数中。 If you save the reference to that type in your form you can change it anytime and your Capturer instance will pick up the changed value. 如果您在表单中保存了对该类型的引用,则可以随时对其进行更改,并且Capturer实例将获取更改后的值。

public class Switcher
{
    public bool Value { get; set; }
}

Your Capturer constructor will look like this: 您的Capturer构造函数将如下所示:

public Capturer(string path, Switcher switcher)
{
    this.switcher = switcher;
    ...
}

Inside the Capturer make sure you always access the switcher value through the Switcher instance. Capturer确保始终通过Switcher实例访问switcher值。 Ie always use switcher.Value to get it. 即始终使用switcher.Value来获取它。

You can create the Switcher instance inside your button handler... 您可以在按钮处理程序中创建Switcher实例...

private void button1_Click(object sender, EventArgs e)
{
    switcher = new Switcher();
    obj = new Capturer(dirPath + name + "_" + surname, switcher);
}

...and set it in your click handler: ...并在点击处理程序中进行设置:

private void mouseClick1(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        Trace.WriteLine("Mouse clicked");
        switcher.Value = true; 
    }
}

This way you only have to share the Switcher instance between you handlers, but not the Capturer instance. 这样,您只需要在处理程序之间共享Switcher实例,而不必在Capturer实例之间共享。 Which solution is better depends on the way you want to structure your application and which parts should be coupled and which should be decoupled. 哪种解决方案更好取决于您构建应用程序的方式以及应耦合的部分和应分离的部分。


A third solution is to use a lambda, a delegate or an event. 第三种解决方案是使用lambda,委托或事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM