Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Pattern for downloading data form a web service

Options
  • 11-05-2016 10:14am
    #1
    Closed Accounts Posts: 2,338 ✭✭✭


    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 Posts: 2,018 ✭✭✭Colonel Panic


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


  • Closed Accounts Posts: 2,338 ✭✭✭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 Posts: 2,018 ✭✭✭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