-1

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.

Community
  • 1
  • 1
NotSure
  • 651
  • 2
  • 7
  • 24
  • 3
    Do you actually need the button to be pressed - or do you just need to run the code in the click event? – PaulF Apr 12 '17 at 14:06
  • why not just set a timer to run `StartButton_click(null,null)`? – vipersassassin Apr 12 '17 at 14:07
  • Just the code, I think I'm just new to this kind of header, if I'll pass null, null it should be fine ? – NotSure Apr 12 '17 at 14:08
  • As long as you are not trying to use the parameters you will be fine using null for both. But you could write a separate method that is called from within the click event and also from the code you use to run it at the predefined time. – PaulF Apr 12 '17 at 14:09
  • Yes, assuming you don't do anything with the arguments inside the event handler. – mm8 Apr 12 '17 at 14:10
  • You don't need the button pressed. You need to extract the code in a proper method and call the method when needed, not the click handler. You can use a timer that runs eg once a minute and decides whether to execute the method or not – Panagiotis Kanavos Apr 12 '17 at 14:28
  • I figuerd out that I actually need the button pressing option too as all of the results here caused an exception but when I actually press it everything is fine. – NotSure Apr 12 '17 at 15:19

1 Answers1

1

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));
mm8
  • 163,881
  • 10
  • 57
  • 88
  • I guess this might work, as I said I wasn't sure what is passed in the parameters when I actually click it instead of dynamiclly running it. – NotSure Apr 12 '17 at 14:10
  • You could pass any values you want provided that you don't use the parameters in your event handler. – mm8 Apr 12 '17 at 14:10
  • 1
    Calling `async void` methods like that will lead to bugs. They are *not* meant to be called like other methods, can't be awaited and their exceptions can't be caught. The OP shoudl *not* try to call the button handler – Panagiotis Kanavos Apr 12 '17 at 14:30
  • Panagiotis Kanavos is right. I try calling StartButton_Click(this, new RoutedEventArgs()); but did get bug. So there is no way to invoke it ? – NotSure Apr 12 '17 at 14:31
  • Define a method and call this one from both your event handler and your timer as I suggested in my edited answer. – mm8 Apr 12 '17 at 14:32
  • You might also use the RaiseEvent method. I edited my answer again. – mm8 Apr 12 '17 at 14:36
  • "Calling async void methods like that will lead to bugs..". It of course depends on how the event handler is implemented. How do you think WPF invokes it? – mm8 Apr 12 '17 at 14:37
  • tried all 3 of those, each is causing an exception. But when I click on the button itself everything is fine. I guess its not just the code that I need. It's the button pressing action too. any ideas ? thanks ! – NotSure Apr 12 '17 at 15:17
  • What does the exception message say? It is obviously your code that cause the exception and it should be even more obvious that you should post this code if you want anyone to be able to point out what you are doing wrong... – mm8 Apr 12 '17 at 15:18
  • 1
    You somehow again forgot to mention what the exception message actually says. – mm8 Apr 12 '17 at 15:30
  • You can only access a UI element from the thread on which it was originally created, i.e. the UI thread. So you cannot run your code on a background thread. This has *nothing* to do with your original question about how to invoke the event handler though. Please ask a new question if you have another issue. – mm8 Apr 12 '17 at 15:52