using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Google.Apis.Gmail.v1.Data;
using Google.Apis.Gmail.v1;
using Google.Apis.Gmail;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
using System.Threading;
using System.IO;
namespace Google_Gmail
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Task task = new Task(ListLabels());
task.Start();
task.Wait();
}
public async Task ListLabels()
{
UserCredential credential;
using (var stream = new FileStream("client_secrets_desktop.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
new[] { GmailService.Scope.GmailReadonly },
"user", CancellationToken.None);
}
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Gmail Test",
});
try
{
ListLabelsResponse response = service.Users.Labels.List("me").Execute();
foreach (Google.Apis.Gmail.v1.Data.Label label in response.Labels.OrderBy(p => p.Name))
{
Console.WriteLine(label.Id + " - " + label.Name);
}
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
我想从我的Gmail帐户中获取所有电子邮件,并将其列在listBox中。 我不确定如何使用异步任务方法。 我如何在构造函数中调用它?
我收到错误:
错误2参数1:无法转换为System.Threading.Tasks.Task'到' System.Action'
答案 0 :(得分:0)
我想知道你为什么这样做,你可以这样称呼:
public async void Start()
{
Task task = ListLabels().ContinueWith(task1 =>
{
//control returns to here
}, TaskScheduler.FromCurrentSynchronizationContext());
}
并从表单的构造函数中调用此start方法。