Sale!

Cpt S 322 Threading Homework Assignment 12 solved

$35.00 $21.00

Category: You will receive a download link of the .ZIP file upon Payment

Description

5/5 - (1 vote)

In this assignment you will build a new WinForms application that utilizes different aspects of
threading and asynchronous methods. The two parts are two unrelated tasks, but put them into a single
application. Design an interface with everything visible at once, such that part one is on the left half of
the form and part two is on the right half. Whatever the interface layout is, it must allow the user to run
both parts 1 and 2 at the same time (see the very end of the assignment describing the last of the 10
points).
Part 1: Asynchronous download from web (4.5 points)
Provide a way for the user to type a URL into a text box and you will download that file as a
string from the web. When testing your app, the TA(s) will use a http:// web link (you don’t have to
worry about https://). Provide a single-line text box for the URL and a multi-line text box that stores the
result.
Use the WebClient (https://msdn.microsoft.com/en-us/library/system.net.webclient.aspx) class.
You will have to read a bit on MSDN to figure out how it works. Fetch the data from the web on a new
thread so that the UI does not freeze while it is downloading. The WebClient class has asynchronous
methods, but it is recommended that you manually create a new thread and then call the synchronous
download function. Remember that anything done on the non-UI thread will require you to Invoke
(https://msdn.microsoft.com/en-us/library/system.windows.forms.form.invoke.aspx) a method if you
want an interface update at the end. Make sure of the following:
• Disable the URL text box, result-data text box, and start-download-button while the download is
taking place. The text box and button controls have a Boolean “Enabled” property.
• Your interface must not freeze when large downloads are taking place. You are disabling
interface components but your UI must still be responding. We went over the difference
between an unresponsive interface and an interface with disabled components in class.
• When the download completes, re-enable the UI components.
• Do NOT use busy-waiting loops or timers to check for download completion. The thread
downloading the data from the web should notify the UI thread when it’s done. The UI thread
shouldn’t have to be repeatedly checking if the download is done.
• For the text box that will display the downloaded data (as a string) set the Multiline property to
true and WordWrap to true as well. Size the text box to take up a fairly large portion of the
2
interface so it’s easy to look at the downloaded string (which will be HTML data in the TAs’
tests).
Part 2: Sorting a collection of lists in 2 ways (4.5 points)
Implement code that generates 8 lists of random numerical values (double, float, or int). Make
sure each list has a decent number of items so the computation actually takes some time. 1,000,000
items for each is probably good. Have a button or menu item in your interface for kicking off the
following actions:
1. Sort all 8 lists on a single thread, one after the other
2. Create a thread for each of the lists, so 8 threads if you have 8 lists, and sort each list on its own
thread.
Measure the time taken for each of the two actions and report it somewhere in the interface. A multiline text box should work fine for the report. Keep the following points in mind:
• To make it clear, you will do part 1 in its entirety (and measure the time) and after it completes
do part 2 in its entirety (and measure that time as well). The idea is that your doing an (almost)
identical task in two ways. One way uses a single thread and the other uses 8 threads. Based on
the number of processors you have on your system, you may see a significant increase in speed
if it can sort multiple lists in parallel.
• Make sure you use random lists at the beginning of each test. Don’t sort all lists on one thread
and then have the multi-threaded method just sorting already sorted lists.
• You can use the Stopwatch (https://msdn.microsoft.com/enus/library/system.diagnostics.stopwatch.aspx) class for measuring the elapsed time of each
method. Another option is to use DateTime:
o DateTime start = DateTime.Now;
o // Do computation
o DateTime end = DateTime.Now;
o // DateTime overloads – operator so elapsed time is: end – start
• Display the elapsed time in milliseconds. There should be two time values reported, one for the
single-threaded method and another for the multi-threaded method.
• Use built-in sorting for each list. You don’t need to implement quicksort or anything like that.
The List class has a Sort (https://msdn.microsoft.com/en-us/library/3da4abas(v=vs.110).aspx)
method.
• Disable interface components while the sorts occur, make sure the interface is still responding
during the sorting, and re-enable interface components after the last of the 8 threads
completes. You’ll have to figure out a way to reliably determine when the last of the 8 threads
completes so that you can invoke a call on the UI thread.
• You don’t have to display the contents of the lists in the interface, just the computation times.
3
Remaining 1 point:
This comes pretty much for free if you do the two above parts correctly, but your app must
support downloading data from the web while also running the sorting tests concurrently. So make sure
that when you start the download from web that you only disable interface components relating to part
1 and the stuff for part 2 stays enabled so that you can start the sorting while the download happens.
The same should be true the other way around so if you start the sorting tests first you can then start a
download that will run concurrently.
Notes:
• If you want to use some of the other threading stuff in .NET to achieve parallel execution then
you may. Just using the Thread (https://msdn.microsoft.com/enus/library/system.threading.thread.aspx) class is all you really need, and you should understand
it for the exams..
• Putting a very large string from the download into the text box can be a time consuming
operation. Unlike the actual download of the data, putting the string in the text box must be
done on the UI thread. So a slight pause in UI refreshing might occur in that case, but the actual
download should happen in a way that doesn’t freeze the UI.
• The image below shows what your interface might look like. You should be able to click both
buttons and move around the window while they’re processing their tasks.