0

I am trying to insert an event on my Google calendar. Everything is working fine but only problem is that when I change the value of my datetimepicker, it doesn't change at the time of execution. for example, if I change the value of datetimepicker to "2014-07-25", it still will show on MessageBox as current date.

Here is my code :

private void btnsubmitevent_Click(object sender, EventArgs e)
{
    try
    {
        new frm_addeventcal().Run().Wait();
    }
    catch (AggregateException ex)
    {
        foreach (var exi in ex.InnerExceptions)
        {
            MessageBox.Show("ERROR: " + exi.Message);
        }

        MessageBox.Show(ex.InnerException.Message);
    }
}

private async Task Run()
{            
    UserCredential credential;
    credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
        new ClientSecrets
        {
            ClientId = "my cliend id",
            ClientSecret = "my secret key"
        },
        new[] { CalendarService.Scope.Calendar},
        "my gmail id",
        CancellationToken.None
    );

    var service = new CalendarService(new BaseClientService.Initializer() {
        HttpClientInitializer = credential,
        ApplicationName = "project name",
    });

    Event newevent = new Event();
    EventDateTime start = new EventDateTime();
    EventDateTime end = new EventDateTime();

    start.DateTimeRaw = dateTimePicker1.Value.ToString("yyyy-MM-dd") + "T" + dateTimePicker3.Value.ToString("HH:mm:ss");
    MessageBox.Show(start.DateTimeRaw); // here it shows current date although I changed the value of it.

    end.DateTimeRaw = dateTimePicker2.Value.ToString("yyyy-MM-dd") + "T" + dateTimePicker4.Value.ToString("HH:mm:ss");
    MessageBox.Show(end.DateTimeRaw); // same thing happens here too.

    newevent.Summary = "Hello World";
    newevent.Location = "my location";
    newevent.Description = "Random Description";
    newevent.Start = start;
    newevent.End = end;
    var calendarstry = service.Events.Insert(newevent, "calendar id").ExecuteAsync();
}

G

I am new to google api and async task. but I think this problem is because I am using async task.

GusP
  • 2,454
  • 2
  • 23
  • 32
Malav Shah
  • 143
  • 2
  • 16
  • 2
    First off, don't do `new frm_addeventcal().Run().Wait();`. Make your event handler `async` and `await` the `Run` method. – Daniel Mann Jul 14 '14 at 22:50
  • In your example you do `new frm_addeventcal().Run()...` Wouldn't that call the run method as soon as the form is created so that you never have the chance to change the DateTimePickers' values before they are used? – Neil Smith Jul 14 '14 at 23:14

1 Answers1

2

I think your issue is that you're calling the Run method on frm_addeventcal immediately when you create the form:

new frm_addeventcal().Run()...

That doesn't give you a chance to change the values of the DateTimePickers' before their values are used which is confusing because you say you do change the values.

You should create frm_addeventcal like this:

var form = new frm_addeventcal();
form.ShowDialog();

And then you'd want to wait until the datetime's are set before calling Run. I think you'd want to display the form while letting the user change the date values and only when the user clicks submit will you execute Run:

public class frm_addeventcal : Form
{
    public frm_addeventcal() {
        InitializeComponent();
    }

    private async void btnSubmit_Click(object sender, EventArgs e) {
        await Run();
    }

    private async Task Run() {
        // the DateTimePickers' values should be correct here since this wont 
        // run until submit is clicked
    }
}
Neil Smith
  • 2,565
  • 1
  • 15
  • 18