0

I'm trying to do multi-threading in a flutter, but I don't know how to achieve it or which package I should use since I'm new. I'm looking for something like this. Package

It's a C# code if someone knows how I can achieve this functionality in flutter if it's possible

 static async Task MainAsync(string[] args)
    {
        // This func takes an input type of 'int', a cancellation token, and an output type of `Task` of `bool`
        Func<int, CancellationToken, Task<bool>> parityCheck = new(async (number, token) => 
        {
            // This is the body of your work function
            await Task.Delay(50, token);
            return number % 2 == 0;
        });

        var parallelizer = ParallelizerFactory<int, bool>.Create(
            type: ParallelizerType.TaskBased, // Use task-based (it's better)
            workItems: Enumerable.Range(1, 100), // The work items are all integers from 1 to 100
            workFunction: parityCheck, // Use the work function we defined above
            degreeOfParallelism: 5, // Use 5 concurrent tasks at most
            totalAmount: 100, // The total amount of tasks you expect to have, used for calculating progress
            skip: 0); // How many items to skip from the start of the provided enumerable

        // Hook the events
        parallelizer.NewResult += OnResult;
        parallelizer.Completed += OnCompleted;
        parallelizer.Error += OnException;
        parallelizer.TaskError += OnTaskError;

        await parallelizer.Start();

        // It's important to always pass a cancellation token to avoid waiting forever if something goes wrong!
        var cts = new CancellationTokenSource();
        cts.CancelAfter(10000);

        await parallelizer.WaitCompletion(cts.Token);
    }

    private static void OnResult(object sender, ResultDetails<int, bool> value)
        => Console.WriteLine($"Got result {value.Result} from the parity check of {value.Item}");
    private static void OnCompleted(object sender, EventArgs e) => Console.WriteLine("All work completed!");
    private static void OnTaskError(object sender, ErrorDetails<int> details)
        => Console.WriteLine($"Got error {details.Exception.Message} while processing the item {details.Item}");
    private static void OnException(object sender, Exception ex) => Console.WriteLine($"Exception: {ex.Message}");
  • Your question is very difficult to understand. If you create new pages each time you navigate make sure you use the same view model instance otherwise your data will be lost (not sure if this what you mean). Also be very careful with static fields or properties. If you are not careful you will create memory leaks. In general avoid static fields or properties when possible. Backingfields don't have to be static. Since you are decorating the parameter of OnPropertyChanged() with t he CallerMemberName attribute, you don't have to pass in any arguments: the nameof argument is redundant. – BionicCode Jun 22 '22 at 20:11
  • Get rid of the static backing fields and ensure to reuse the Stats view model instance when navigating. – BionicCode Jun 22 '22 at 20:14
  • Please add some more code, the code you provided can not be from your actual project since `OnPropertyChanged(Bad);` won't work at all. – Hanebu Jun 23 '22 at 05:38
  • No Singleton here. We want to get rid of static fields and properties. Just add the view model instances that require to hold their state to a collection like a Dictionary or what serves you best. – BionicCode Jun 23 '22 at 07:57
  • That can't be. The field has nothing to do with the change notification. It's the property that raises the change event. Change the value of the property and not of the backing fields. I can't give you examples because I don't know how your code works. You only showed that you create an instance of Settings. I can only guess that this is a Page control. Don't know how you create view models. That's why I voted to close this question. It lacks crucial details. Not die if you get notified about the close request, but you should improve it to allow others to help you. – BionicCode Jun 23 '22 at 09:31
  • https://stackoverflow.com/a/58849975/3141792 – BionicCode Jun 23 '22 at 09:33
  • https://stackoverflow.com/a/61323201/3141792 – BionicCode Jun 23 '22 at 09:33
  • [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – BionicCode Jun 23 '22 at 09:36

0 Answers0