Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

.net - Pressing a button using code, button function is async in 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)

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.

Edit:

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.

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();
}

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.

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.

Thanks

Edit 2:

The question continues at:

Invoking an onclick on button threw code in a different thread in C#

if anyone is intrested in the future.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about just calling the method?

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));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...