使用代码按下按钮,按钮功能在C#中是异步的 [英] Pressing a button using code, button function is async in C#

查看:377
本文介绍了使用代码按下按钮,按钮功能在C#中是异步的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个按钮和单击该函数时会运行的函数签名:

So I have a button and the function's signature that runs when clicking on it:

private async void StartButton_Click(object sender, RoutedEventArgs e)

我需要每天在特定时间按下此按钮.我当然使用的是DateTime.我只是想知道由于它得到的两个参数而使其运行.

I need this button pressed every day at a specific hour. I am using DateTime of course. I just want to know how to make it run becaue of the two parameters it's getting.

所以我尝试了一些答案,但每次都会遇到异常.有趣的是,当我实际单击按钮时,没有发生异常.就在我尝试调用它的事件时.

So I've tried some answers but I get an exception everytime. The intresting part is that the exception is not happening when I actually clickon the button. Just when I am trying to invoke it's event.

该代码实际上不是我的代码,它来自Microsoft的一个开源项目. 代码:

The code is not my code actually, it is froman opensource project of Microsoft. the code:

private async void StartCamera()
{
    if (!CameraList.HasItems)
    {
        MessageArea.Text = "No cameras found; cannot start processing";
        return;
    }

    // Clean leading/trailing spaces in API keys. 
    Properties.Settings.Default.FaceAPIKey = Properties.Settings.Default.FaceAPIKey.Trim();
    Properties.Settings.Default.EmotionAPIKey = Properties.Settings.Default.EmotionAPIKey.Trim();
    Properties.Settings.Default.VisionAPIKey = Properties.Settings.Default.VisionAPIKey.Trim();

    // Create API clients. 
    _faceClient = new FaceServiceClient(Properties.Settings.Default.FaceAPIKey);
    _emotionClient = new EmotionServiceClient(Properties.Settings.Default.EmotionAPIKey);
    _visionClient = new VisionServiceClient(Properties.Settings.Default.VisionAPIKey);

    // How often to analyze. 
    _grabber.TriggerAnalysisOnInterval(Properties.Settings.Default.AnalysisInterval);

    // Reset message. 
    MessageArea.Text = "";

    // Record start time, for auto-stop
    _startTime = DateTime.Now;


    await _grabber.StartProcessingCameraAsync(CameraList.SelectedIndex);
}

private async void StartButton_Click(object sender, RoutedEventArgs e)
{
    StartCamera();
}

我应该补充说,StartCamera方法最初不存在,而是被添加的,目的是按照建议的那样为该函数创建非参数用途.

I should add that the StartCamera method was not there originally but was added in order to create a non parameter use for the function as was suggested.

当我尝试使用例如以下方法调用它时,总是会收到一个invalidOperationException:

I always get an invalidOperationException on it when I try to invoke it using for example:

StartButton.RaiseEvent(new RoutedEventArgs(System.Windows.Controls.Primitives.ButtonBase.ClickEvent));

此异常的信息是:{调用线程无法访问此对象,因为另一个线程拥有它."}

The information to this exception is: {"The calling thread cannot access this object because a different thread owns it."}

所以从根本上讲,我认为该异常并不能真正帮助我理解为什么实际上按一下按钮就可以,但是调用它就不行了.

So basiclly I think this exception doesn't really help me understand why actually pressing the button is fine but invoking it isn't.

谢谢

问题继续于:

调用onclick on按钮在C#中将代码扔到另一个线程中

如果将来有人对我感兴趣.

if anyone is intrested in the future.

推荐答案

仅调用该方法怎么样?

StartButton_Click(this, new RoutedEventArgs());

您还可以定义一个不带从事件处理程序和计时器中调用的参数的方法:

You might also define a method without parameters that you call from both the event handler and your timer:

private async void StartButton_Click(object sender, RoutedEventArgs e)
{
    TheMethod();
}

那么您就不必在意参数了.

Then you don't need to care about the parameters.

第三个选择是以编程方式引发事件:

A third option is to raise the event programmatically:

button.RaiseEvent(new RoutedEventArgs(System.Windows.Controls.Primitives.ButtonBase.ClickEvent));

这篇关于使用代码按下按钮,按钮功能在C#中是异步的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆