Quantcast
Channel: async / await / task
Viewing all articles
Browse latest Browse all 19

async / await / task

$
0
0
Hi ThankfulHeart,

Thanks for the response.

The project is empty. Do you have the full project?

is empty! You can see it here!

 Do you know an example where I see the possibilities, what is possible for synchronization?
    With --> Task.WaitAll(taskOne, taskTwo); I have it. See below.
 With ManualResetEvent
 With AutoResetEvent
I find it difficult to read and if the code gets longer, even more complicated. Can I write it more structured?
Like this.
public async Task Example1Async(PersonArguments personArguments, IProgress<string> progress)
{
	Task<Person> taskOne = Task.Factory.StartNew(async () =>
	{
	    Function1();
	}
	var taskTwo = Task.Factory.StartNew(async () =>
	{
	   Function2();
	}
	Task.WaitAll(taskOne, taskTwo);
	progress.Report("Both task done!");
}

How should I write function 1 and 2 correctly, the transfer parameters?
Can you publish a concrete example?

public async Task Example1Async(PersonArguments personArguments, IProgress<string> progress)
{
	Task<Person> taskOne = Task.Factory.StartNew(async () =>
	{
		var person = new Person() { Id = -1 };

		var lines = File.ReadAllLines(personArguments.FileName);
		for (var index = 0; index < lines.Length - 1; index++)
		{
			var lineParts = lines[index].Split(',');
			if (lineParts[1] == personArguments.FirstName && lineParts[2] == personArguments.LastName)
			{
				person.Id = Convert.ToInt32(lineParts[0]);
				person.Birthday = DateTime.Parse(lineParts[4]);
				if (lineParts[3] == "1" || lineParts[3] == "2")
				{
					person.Gender = lineParts[3] == "1" ? "Female" : "Male";
				}
				progress.Report($"Id is {lineParts[0]}");
				break;
			}

			await Task.Delay(1, _cancellationToken);
		}

		progress.Report("Task 1 complete");
		return person;

	}, _cancellationToken);

	var taskTwo = Task.Factory.StartNew(async () =>
	{
		for (var index = 0; index < 5; index++)
		{
			_cancellationToken.ThrowIfCancellationRequested();
			progress.Report(index.ToString());

			await Task.Delay(12, _cancellationToken);

			_cancellationToken.WaitHandle.WaitOne(500);
		}

		progress.Report("Task 2 complete");
		return true;

	}, _cancellationToken);

	Task.WaitAll(taskOne, taskTwo);
	progress.Report("Both task done!");/////////// M

My goal is this

my goal!
I have an assembly line that produces a product and continues in the process sequence.
It may only continue if the sub-processes that are executed in parallel are finished.
How can I implement this well?
Can you give me ideas

public void Step1()
{
	public void SubStep1_1()
	{
	}
	public void SubStep1_2()
	{
	}  
}

public void Step2()
{
	public void SubStep2_1()
	{
	
	}
	
	public void SubStep2_2()
	{
	
	}  
}

while (!UserPressStop)
{
	public void Controller()
	{
		Step1();
		Step2();
		Task.WaitAll(taskOne, taskTwo);
		progress.Report("Both task done!");
	}
}
  • What is the best way?
      - Task.WaitAll
      - ManualResetEvent
      - await
      -  ??
      
       Thanks in advance.
Best regards Markus


Viewing all articles
Browse latest Browse all 19

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>