Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Pattern for downloading data form a web service

  • 11-05-2016 10:14AM
    #1
    Closed Accounts Posts: 2,337 ✭✭✭


    Hi, C# question-

    I want to download data continuously from a web service, but stop at some point.

    I also want to see some of the data, or do something with it using an object owned by my main form, e.g. a class that updates a spreadsheet.

    I thought of using a checkbox and a while look that checks if the checkbox is checked.
    private void button1_Click(object sender, EventArgs e)
            {
              while (checkBox1.Checked)
                {
                    listBox1.Items.Add("hello");
                }
            }
    

    Unfortunately this blocks the UI thread completely. If I use a thread then I have no access to the UI checkbox to see if its checked or not.

    Is there any general pattern that would allow me to query a web service continuously but then still be able to access items on the same UI thread (but not also block it completely)?


Comments

  • Registered Users, Registered Users 2 Posts: 2,062 ✭✭✭Colonel Panic


    Background worker, Task Parallel Library, the Async/Await pattern, loads of different ways.


  • Closed Accounts Posts: 2,337 ✭✭✭aphex™


    Background would work.

    But with the rest of those I can't really use the checkbox for flow control to switch the data off access. Task might create a new thread. Then the UI elements would be inaccessible.

    I want to show some of the data in a listbox.

    I will also use a private timestamp field and check that the current time is 5 minutes bigger than previously saved. But, my while field could be constantly looping to check this every second. Is that normal practice?

    If there is any general patterns for this I would appreciate links.


  • Registered Users, Registered Users 2 Posts: 2,062 ✭✭✭Colonel Panic


    You can access UI elements from another thread view the control.Invoke pattern, you can use the checkbox to set a cancellation token that a worker task could check while it's downloading.

    For WinForms:
    private void OutputToLog(ImportInformation info)
    {
    	if (log.InvokeRequired)
    	{
    		log.Invoke(new Action<ImportInformation>(OutputToLog), new object[]{ info });
    	}
    	else
    	{
    		log.BeginUpdate();
    		var item = new ListViewItem(new string[]
    		{
    			info.Status == UploadStatus.Success ? "Success" : "Error",
    			info.Filename.Split(new char[] {'\\'}).Last(),
    			info.Message,
    			info.Url
    		});
    		item.ImageIndex = info.Status == UploadStatus.Success ? 0 : 1;
    		log.Items.Add(item);
    		log.EndUpdate();
    	}
    }
    


Advertisement