C# 依次调用两个或多个函数
我想依次调用两个或多个方法。当一个函数执行完成时,我需要调用另一个方法。基本上,我正在尝试在我的应用程序中实现备份功能。我正在开发Windows Phone应用程序,用于备份联系人、图像和视频。我已经为每一个创建了方法。当联系人备份完成后,我想调用images方法。我已经为这些创建了不同的函数。如何一个接一个地调用这些函数 我试过这样的东西C# 依次调用两个或多个函数,c#,multithreading,windows-phone-8,C#,Multithreading,Windows Phone 8,我想依次调用两个或多个方法。当一个函数执行完成时,我需要调用另一个方法。基本上,我正在尝试在我的应用程序中实现备份功能。我正在开发Windows Phone应用程序,用于备份联系人、图像和视频。我已经为每一个创建了方法。当联系人备份完成后,我想调用images方法。我已经为这些创建了不同的函数。如何一个接一个地调用这些函数 我试过这样的东西 // Constructor public MainPage() { InitializeComponent();
// Constructor
public MainPage()
{
InitializeComponent();
UploadImagesThread = new Thread(UploadImages);
UploadContactsThread = new Thread(UploadContacts);
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
在上载按钮上,单击
if (chkContacts.IsChecked.Value)
{
Dispatcher.BeginInvoke(() =>
{
SystemTray.ProgressIndicator.Text = "Searching contacts...";
});
UploadContactsThread.Start();
}
if (chkImages.IsChecked.Value)
{
Dispatcher.BeginInvoke(() =>
{
SystemTray.ProgressIndicator.Text = "Compressing images...";
});
UploadImagesThread.Start();
}
但这对我没有帮助。我怎样才能找到工作?我的UploadContact方法有Async方法调用,如下所示
Contacts objContacts = new Contacts();
objContacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(objContacts_SearchCompleted);
objContacts.SearchAsync(string.Empty, FilterKind.None, null);
Contacts objContacts=新联系人();
objContacts.SearchCompleted+=新事件处理程序(objContacts\u SearchCompleted);
SearchAsync(string.Empty,FilterKind.None,null);
尝试使用任务,这将让您免费获得一个线程池。您不能在构造函数中执行此操作,但可以覆盖OnNavigatedTo方法:
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
await Task.Run(() => { UploadImages(); });
await Task.Run(() => { UploadContacts(); });
}
等待将确保联系人在您的图像完成后开始上传。您还可以检查,这意味着您的应用程序在操作完成时不必运行。尝试使用任务,这将使您免费获得一个线程池。您不能在构造函数中执行此操作,但可以覆盖OnNavigatedTo方法:
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
await Task.Run(() => { UploadImages(); });
await Task.Run(() => { UploadContacts(); });
}
等待将确保联系人在您的图像完成后开始上传。您还可以检查,这意味着您的应用程序在操作完成时不必运行。使用
Task.ContinueWith()
链接调用
您可以在这里查看:
应用于您的问题,这应该可以做到:
Task.Factory.StartNew(UploadImages).ContinueWith(UploadContacts);
使用
Task.ContinueWith()
链接调用
您可以在这里查看:
应用于您的问题,这应该可以做到:
Task.Factory.StartNew(UploadImages).ContinueWith(UploadContacts);
您对问题的描述(一个接一个地调用一个方法?)不是很清楚,但是看看您的代码,我想您应该等待
UploadImagesThread
线程完成,然后再启动UploadContactsThread
使用tasks和async/await关键字,如下所示:
private async void OnButtonClick(object sender, ...)
{
if (chkContacts.IsChecked.Value)
{
SystemTray.ProgressIndicator.Text = "Searching contacts...";
await Task.Run(() => UploadImages());
}
if (chkImages.IsChecked.Value)
{
SystemTray.ProgressIndicator.Text = "Compressing images...";
await Task.Run(() => UploadContacts());
}
}
注意:假设您的第二个代码块在UI线程上运行,您不需要使用BeginInvoke
编辑 为了回应你最近的变化:你需要重新设计。试试这个:
private async void OnButtonClick(object sender, ...)
{
bool uploadContacts = chkContacts.IsChecked.Value;
bool uploadImages = chkImages.IsChecked.Value;
//use this if the continuation runs on the UI thread
Action continuation = async () => {
if(uploadImages) {
SystemTray.ProgressIndicator.Text = "Compressing images...";
await Task.Run(() => UploadImages());
}
};
//OR this if it doesn't
Action continuation = () => {
if(uploadImages) {
Dispatcher.BeginInvoke(() => SystemTray.ProgressIndicator.Text = "Compressing images...");
UploadImages();
}
};
if (uploadContacts)
{
SystemTray.ProgressIndicator.Text = "Searching contacts...";
UploadContacts(continuation);
}
}
private void UploadContacts(Action continuation)
{
Contacts objContacts = new Contacts();
//when the search has finished, trigger your event handler AND the continuation task, which will upload the images
objContacts.SearchCompleted += objContacts_SearchCompleted;
objContacts.SearchCompleted += (sender, args) => continuation();
objContacts.SearchAsync(string.Empty, FilterKind.None, null);
}
您对问题的描述(一个接一个地调用一个方法?)不是很清楚,但是看看您的代码,我想您应该等待
UploadImagesThread
线程完成,然后再启动UploadContactsThread
使用tasks和async/await关键字,如下所示:
private async void OnButtonClick(object sender, ...)
{
if (chkContacts.IsChecked.Value)
{
SystemTray.ProgressIndicator.Text = "Searching contacts...";
await Task.Run(() => UploadImages());
}
if (chkImages.IsChecked.Value)
{
SystemTray.ProgressIndicator.Text = "Compressing images...";
await Task.Run(() => UploadContacts());
}
}
注意:假设您的第二个代码块在UI线程上运行,您不需要使用BeginInvoke
编辑 为了回应你最近的变化:你需要重新设计。试试这个:
private async void OnButtonClick(object sender, ...)
{
bool uploadContacts = chkContacts.IsChecked.Value;
bool uploadImages = chkImages.IsChecked.Value;
//use this if the continuation runs on the UI thread
Action continuation = async () => {
if(uploadImages) {
SystemTray.ProgressIndicator.Text = "Compressing images...";
await Task.Run(() => UploadImages());
}
};
//OR this if it doesn't
Action continuation = () => {
if(uploadImages) {
Dispatcher.BeginInvoke(() => SystemTray.ProgressIndicator.Text = "Compressing images...");
UploadImages();
}
};
if (uploadContacts)
{
SystemTray.ProgressIndicator.Text = "Searching contacts...";
UploadContacts(continuation);
}
}
private void UploadContacts(Action continuation)
{
Contacts objContacts = new Contacts();
//when the search has finished, trigger your event handler AND the continuation task, which will upload the images
objContacts.SearchCompleted += objContacts_SearchCompleted;
objContacts.SearchCompleted += (sender, args) => continuation();
objContacts.SearchAsync(string.Empty, FilterKind.None, null);
}
试试这样:
bool backContact = chkContacts.IsChecked.Value;
bool backImages = chkImages.IsChecked.Value;
Task.Factory.StartNew(() =>
{
if (backContact) {
Dispatcher.BeginInvoke(() =>
{
SystemTray.ProgressIndicator.Text = "Searching contacts...";
});
UploadContacts;
});
}).ContinueWith(() =>
{
if (backImages) {
Dispatcher.BeginInvoke(() =>
{
SystemTray.ProgressIndicator.Text = "Compressing images...";
});
UploadImages;
}
}
试试这样:
bool backContact = chkContacts.IsChecked.Value;
bool backImages = chkImages.IsChecked.Value;
Task.Factory.StartNew(() =>
{
if (backContact) {
Dispatcher.BeginInvoke(() =>
{
SystemTray.ProgressIndicator.Text = "Searching contacts...";
});
UploadContacts;
});
}).ContinueWith(() =>
{
if (backImages) {
Dispatcher.BeginInvoke(() =>
{
SystemTray.ProgressIndicator.Text = "Compressing images...";
});
UploadImages;
}
}
它给我一个错误
以下方法或属性之间的调用不明确:“System.Threading.Tasks.Task.Run(System.Func)”和“System.Threading.Tasks.Task.Run(System.Action)”
它给我错误只有赋值、调用、递增、递减、等待和新对象表达式可以用作语句。函数返回类型为void。@AjayPunekar在哪一行?UploadImages
和UploadContacts
的签名是什么?此函数的返回类型是void
@AjayPunekar在哪一行获得唯一分配,call…
error?它给我一个错误调用在以下方法或属性之间不明确:'System.Threading.Tasks.Task.Run'(System.Func)和“System.Threading.Tasks.Task.Run(System.Action)”“
它给我错误只有赋值、调用、递增、递减、等待和新对象表达式可以用作语句。函数返回类型为void。@AjayPunekar在哪一行?上传图像和上传联系人的签名是什么?此函数的返回类型为void
@AjayPunekar您在哪一行获得唯一赋值,call…
错误?它给我一个错误以下方法或属性之间的调用不明确:'System.Threading.Tasks.Task.Run(System.Func)'和'System.Threading.Tasks.Task.Run(System.Action)'“
@AjayPunekar抱歉,我的错误更新了我的anser。我猜UploadImages获得了返回类型为task的签名?这两个方法都有返回类型void
它给了我一个错误以下方法或属性之间的调用不明确:“System.Threading.Tasks.task.Run(System.Func)”'和'System.Threading.Tasks.Task.Run(System.Action)'
@AjayPunekar抱歉,我的错误更新了我的anser。我猜UploadImages得到了返回类型为Task的签名?这两种方法都有返回类型void