WPF GUI Performance with a large number of parallel tasks

后端 未结 3 503
清歌不尽
清歌不尽 2021-01-28 07:23

I developed a small client (WPF) to make some stress test on our systems. Essentially it has to call various methods on an Asp.Net WebApi endpoint in parallel.

Each time

相关标签:
3条回答
  • 2021-01-28 07:36

    Are you handling those API events by invoking to the UI context? If you have many invocations occurring you will flood the dispatcher with operations and cause the UI to hang and lag behind user input.

    Try batching the UI updates.

    0 讨论(0)
  • 2021-01-28 07:47

    My first question is: why opening one instance with x tasks is worse in terms of responsivness than opening n instances with x/n tasks?

    Possibly because you are getting more events to handle on the UI thread. I guess your ErrorBetCountChanged, GoodRequestCountChanged and TryRequestCountChanged event handlers are invoked on the UI thread and a lot of events being raised may flood the UI thread.

    As Gusdor suggets you should probably find a way of batching the updates. Take a look at the reactive extensions (Rx): http://www.introtorx.com/content/v1.0.10621.0/01_WhyRx.html.

    It has a Buffer method that may come in handy: http://www.introtorx.com/content/v1.0.10621.0/13_TimeShiftedSequences.html.

    It also has en Obervable.FromEvent method that you can use to convert an event into an IObservable: https://msdn.microsoft.com/en-us/library/hh229241(v=vs.103).aspx.

    My second questions is: how can I address the problem to make everything work on a single GUI?

    You need to find a way - one or anoher - of updating the UI less frequently. Batching the updates and events should be a good starting point. Raising less notifications is another option. Maybe you need to both.

    0 讨论(0)
  • 2021-01-28 07:52

    how can I address the problem to make everything work on a single GUI?

    Send API requests in "proper" async-await manner with only one thread.

    private async Task SendStressRequests()
    {
        var tasks = new List<Task>();
        for (int i = 0; i < 4000; i++)
        {
            var task = SendApiRequestAsync();
            tasks.Add(task);
        }
    
        await Task.WhenAll(tasks);
        // Update UI with results
    }    
    
    0 讨论(0)
提交回复
热议问题