Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...