Answer the question
In order to leave comments, you need to log in
How to start a Task after it's done?
Hello, I ran into a problem that I still can’t solve:
There is a button, when pressed, a method is called:
private void StartButton_Click(object sender, RoutedEventArgs e)
{
OnlyLike();
}
public void OnlyLike()
{
Task<string> LikeTurbo = VK.NakrytkaLike(KeyAntigateTextBox.Text);
LikeTurbo.ContinueWith(task =>
{
TextBox.Text += LikeTurbo.Result.ToString();
}, TaskScheduler.FromCurrentSynchronizationContext());
}
Answer the question
In order to leave comments, you need to log in
I did not understand a little, do you need to run tasks sequentially or in parallel? And if in parallel - then process the results at once or together?
1. Sequential start:
private async void OnlyLike()
{
for (var i=0; i<5; i++)
TextBox.Text += await VK.NakrytkaLike(KeyAntigateTextBox.Text);
}
private async void OnlyLike()
{
var tasks = Enumerable.Range(0, 5).Select(i => VK.NakrytkaLike(KeyAntigateTextBox.Text));
foreach (var result in await Task.WhenAll(tasks))
TextBox.Text += result;
}
private async void OnlyLike()
{
TextBox.Text += await VK.NakrytkaLike(KeyAntigateTextBox.Text);
}
private void StartButton_Click(object sender, RoutedEventArgs e)
{
for (var i=0; i<5; i++)
OnlyLike();
}
public void OnlyLike()
{
var scheduler = TaskScheduler.Current ?? TaskScheduler.FromCurrentSynchronizationContext();
Task task = TaskEx.FromResult(false);
var text = KeyAntigateTextBox.Text;
for (var i=0; i<5; i++) {
task = task.ContinueWith(_ => {
var LikeTurbo = VK.NakrytkaLike(text);
LikeTurbo.ContinueWith(_ => {
TextBox.Text += LikeTurbo.Result;
}, scheduler);
return LikeTurbo;
}, TaskContinuationOptions.ExecuteSynchronously).Unwrap();
}
}
...
public static class TaskEx {
public static Task<T> FromResult<T> (T result) {
var tcs = new TaskCompletionSource<T>();
tcs.SetResult(result);
return tcs.Task;
}
public static Task<T> Unwrap<T>(this Task<Task<T>> task) {
var tcs = new TaskCompletionSource<T>();
task.ContinueWith(_ => {
if (task.IsCanceled) tcs.SetCancelled();
else if (task.IsFaulted) tcs.SetException(task.Exception);
else task.Result.ContinueWith(innerTask => {
if (innerTask.IsCanceled) tcs.SetCancelled();
else if (innerTask.IsFaulted) tcs.SetException(task.Exception);
else tcs.SetResult(innerTask.Result);
}, TaskContinuationOptions.ExecuteSynchronously);
}, TaskContinuationOptions.ExecuteSynchronously);
return tcs.Task;
}
}
The problem here is rather architectural, if something like this is required, then something is wrong here, but in general, nothing prevents wrapping it all in a separate Task or Thread and sequentially calling tasks in it as many times as you want.
for(var i=1; i<=5; i++)
{
Console.WriteLine("Сейчас будем запускать продолжающиеся потоки в " + i + " раз");
Task<string> LikeTurbo = Task.Factory.StartNew<string>(()=>
{
Console.WriteLine("Выполнение в первом потоке №"+i);
return "somestring";
});
Task cont_task=LikeTurbo.ContinueWith(task =>
{
Console.WriteLine("Выполнение во втором потоке №" + i);
});
cont_task.Wait();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question