简体   繁体   中英

Handling repetitive input from the user

My WPF C# program lets the user browse items: When the user clicks "Next Items" I disable the command button, load the next X items, and enable the command button. Executing the command takes a few seconds.

Now, assume the user is impatient and repeatedly clicks the "Next Items" button (or holds down the corresponding key). How do I take advantage of this, using this as a cue to behave differently and "fast forward" item groups? Display not group N+1 and N+2 but, for example, N+5 and N+10 and, if the user persists, n+30 and N+40?

I could delegate the processing to a background task, keep the button enabled, and interpret a "click while previous command had not yet been processed" as an indication to hurry things up and batch a few invocations together. But I'm afraid I'm complicating things: This seems like common need and hopefully there're established solutions.

So, my question is: How do I identify that the user wants me to hurry things up, and is there a common practice for responding?

Edit : Two great answers. Please forgive me, I can mark only one as the "selected" answer, bu thank you both!

You have to start working with Async methods and work with cancellation tokens. But work your way towards it and don't do everything in one step. First implement async, then and only then add cancellation.

There are some interesting documents on the internet from Steven Cleary to get you started with Async.

The point of doing it this way is that since you are delegating to a background thread by using async, your GUI remains responsive at all times. When your user presses the button fast and a long running process is still busy, you can cancel it by checking the cancellation token, and then simply issue a new task.

But if async is new for you, you will have plenty of reading to do.

http://blog.stephencleary.com/2012/02/async-and-await.html http://blog.stephencleary.com/2014/10/a-tour-of-task-part-5-wait.html

I'm you're already aware you'll need the fetching of data to be asynchronous ; I'd focus on how you'll identify the user's desire to fast forward and how to handle it from a User Experience perspective. To preserve the user experience I would recommend eliminating all doubt as to what they want by:

  1. Registering a complete click as a request for the next set of records by wiring your click event to the Mouse.MouseUp event
  2. Give a visual indicator as to what happened and where they are within the complete dataset

An example would be if the user clicks once and your application pages @ 10 records per click, you would display "Fetching records 1 through 10" and for each subsequent click: cancel the previous Async request, increase/decrease the record range (reflected on the UI as well) and submit the new request. This should also work in reverse (in case the user overshoots their goal range).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM