Superior ways to a better UI - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Superior ways to a better UI

Description:

Facebook (http://developers.facebook.com ... There are proxy generators (wsdl.exe) that take the WSDL file and generate a Web ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 40
Provided by: Tatham
Category:

less

Transcript and Presenter's Notes

Title: Superior ways to a better UI


1
Developing Windows and Web Applications using
Visual Studio.NET
Presented by Paul Sorauer
2
Admin Stuff
  • Attendance
  • You initial sheet
  • Hands On Lab
  • You get us to initial sheet
  • Homework
  • Certificate
  • At end of 10 sessions
  • If we say you have completed successfully ?

3
Session 5 Web Services Threading
  • Agenda
  • What are Web Services?
  • WCF Services
  • Consuming Web Services
  • Threading

4
What are Web Services?
  • A web service is a collection of protocols and
    standards used for exchanging data between
    applications or systems. Software applications
    written in various programming languages and
    running on various platforms can use web services
    to exchange data over computer networks like the
    Internet in a manner similar to inter-process
    communication on a single computer. (Wikipedia)
  • Technologies that underpin Web Services are XML
    and SOAP.
  • Cross Platform
  • E.g. Java Web Service (AXIS) can by consumed by a
    .NET application and vica versa

5
Web Services
  • There are many web services available on the Web,
    and there are Web Services APIs for popular
    applications such as
  • Google (http//code.google.com/apis/)
  • Facebook (http//developers.facebook.com/)
  • Virual Earth (http//msdn2.microsoft.com/en-us/vir
    tualearth/default.aspx)
  • TV Guides (http//d1.com.au/D1xmltv.asmx)

6
Creating a Web Service
  • Built in Web Services Project
  • WebMethod attribute to expose methods
  • WSDL File (wiz-dull)

7
Web Services
8
Web Service Interface - SOAP
  • Built in test site for simple data types
  • Shows the expected SOAP request and SOAP response

SOAP Request
SOAP Response
9
Telnet Demo
  • Craft a SOAP packet and send it to the web
    service via telnet
  • See the response

10
Web Services Definition Language
  • Defines the methods, results and types in an XML
    document
  • There are proxy generators (wsdl.exe) that take
    the WSDL file and generate a Web Service Client
    based on the WSDL

11
Consuming Web Services
  • Automatic - Add a Web Reference to your client
  • Dynanically generates a web service client class
    with all the exposed methods
  • Synchronous and Asynchronous methods
  • Manual - Run wsdl.exe

12
Windows Communication Foundation (WCF)
  • Part of the .NET Framework 3 (Formerly WinFX)
  • Windows Presentation Foundation (WPF)
  • Windows Communication Foundation (WCF)
  • Windows Workflow (WF)
  • Cardspace
  • WCF isnt just a web service
  • Supports many transport protocols, HTTP, UDP, TCP
  • Supports queues, transactions, reliable sessions,
    better security model

13
Windows Communication Foundation (WCF)
  • WCF Sites for more info
  • http//wcf.netfx3.com/

14
WCF Demo
  • Creating a WCF Service
  • Hosting a WCF Service
  • Consuming a WCF Service

15
Threading
  • Why do we need threading?
  • Blocking calls (e.g. Disk IO, Network)
  • Responsive UI
  • Better performance (especially on multi-core
    processors)

16
How do I create a Multi threaded application?
  • Application.DoEvents
  • Thread.Start
  • Delegate.BeginInvoke
  • BackgroundWorker

17
Application.DoEvents()
  • Call this method inside code that is being
    processed
  • What it does is it issues a command to the UI to
    process events on the message queue (e.g. Mouse
    Clicks, Redraw Window)

18
Application.DoEvents()
  • Not appropriate
  • Ideal scenario is

Worker 1
SearchGoogle()
UI
Return
FetchData
Worker 2
Merge
SearchYahoo()
Done
Return
19
Creating a new thread
  • Threading control through the System.Threading
    namespace
  • Create a delegate for the method you want to set
    as the start location of threads execution
  • Create a new thread using this delegate
  • Start the thread

20
Creating a new thread
  • using System.Threading
  • private void btnSearch_Click(object
    sender, EventArgs e)
  • // Tell the thread starter which
    method to use (i.e. StartSearch)
  • ThreadStart ts new
    ThreadStart(StartSearch)
  • // Create a new thread
  • Thread SearchThread new Thread(ts)
  • // Start the search on the new thread
  • SearchThread.Start()
  • public void StartSearch()

21
System.Threading.Thread
  • System.Threading.Thread is the main class for
    creating threads and controlling them. A few
    interesting methods are shown below
  • Start() starts the execution of the thread.

  • Suspend() suspends the thread, if the thread
    is already suspended, nothing happens.
  • Resume() resumes a thread that has been
    suspended.
  • Interrupt() interrupts a thread that is in the
    wait, sleep or join stage.
  • Join() blocks a calling thread until the
    thread terminates.
  • Sleep(int x) suspends the thread for
    specified amount of time (in milliseconds).
  • Abort() Begins the process of terminating the
    thread. Once the thread terminates,
    it cannot be restarted by calling the
    function Start() again.

22
Thread.Start()
  • The UI was responsive
  • Was no longer a blocking call could continue
    working while search ran
  • Allows multiple worker threads to execute
    simultaneously
  • Bad coding practice for threads no guarantee
    that you will capture all errors

23
Thread.Start()
  • Suitable (not the best solution)

Worker Thread
UI Thread
Start
Start
Execute
Update UI
Done
Done
24
Delegate.BeginInvoke()
  • Ideal scenario is

Worker 1
SearchGoogle()
Return
FetchData
Worker 2
Merge
SearchYahoo()
Done
Return
25
Delegate.BeginInvoke()
  • Best Solution Using Delegates to execute a
    method asynchronously.
  • What is a delegate?
  • A delegate defines both the signature and the
    location of a specified method.
  • The .NET Framework provides a series of delegates
    for commonly used syntaxes.
  • Delegates can be used to call methods
    asynchronously, for example using
    Delegate.BeginInvoke().This uses a worker thread
    from the managed Threadpool.

26
Delegate.BeginInvoke()
  • The UI was completely responsive
  • UI updates are no longer tightly bound (event
    based)

27
Delegate.BeginInvoke()
  • The perfect solution

Worker 1
SearchGoogle()
Return
FetchData
Worker 2
Merge
SearchYahoo()
Done
Return
28
An Easier Way
  • BackgroundWorker Component
  • Uses Delegates under the covers
  • Sites to Research
  • http//msdn2.microsoft.com/en-us/library/system.co
    mponentmodel.backgroundworker(vs.80).aspx
  • http//www.codeplex.com/ExerciseBackgroundW
  • http//www.codeguru.com/columns/vb/article.php/c10
    755/

29
  • The One Golden Rule

30
The One Golden Rule
  • When Multiple threads access a shared resource,
    only 1 thread can update it at a time.
  • Otherwise we get
  • Deadlocks
  • Race Conditions
  • Correct use of Synchronisation prevents these
    situations

31
Synchronization Shared Resources
  • Synchronisation involves controlling access to a
    shared resource so only one thread can access the
    resource at a time.
  • Resources include
  • Variableseg. Arrays
  • User Interface Controls eg. Progress Bar

32
Synchronizing Variables
  • There are several methods of Synchronizing access
    to Variables
  • Lock() code Only one thread executes the
    code inside the braces at a
    time.
  • Monitors - lock a section of code so only one
    thread can access it at a time.
  • Interlocked Class basic atomic operations on a
    single integer variable
  • ManualResetEvent and AutoResetEvent - allow one
    thread to notify others of an event occurring
  • WaitHandles thread waits on a certain event to
    occur before continuing
  • And more

33
Lock
  • public static Array myArray
  • public void UpdateArray()
  • lock (myArray)
  • // Do something here to the shared
    resource
  • myArray.Add(10)

34
Using a Monitor
  • public static string c
  • public void some_method()
  • int a100
  • int b0
  • Monitor.Enter(c)
  • //say we do something here.
  • c(a/b).ToString()
  • Monitor.Exit(c)

35
Synchronizing User Interface
  • Synchronization problem solved in VS 2005 by only
    allowing the main UI thread to access UI
    controls.
  • Worker threads must tell the UI thread to make a
    change on their behalf via a delegate.

36
  • A Real World Example
  • SSW Link Auditor

37
Link Auditor
38
Link Auditor
  • Threading was used to achieve
  • A responsive UI throughout
  • Optimize the scanning process
  • Techniques that we used
  • Delegates and Callbacks
  • ManagedThreadPool class

39
Session 5 Threading
  • Summary
  • What is Threading?
  • Application.DoEvents ()
  • Thread.Start ()
  • Synchronization
  • Delegate.BeginInvoke()
  • Generate custom events
  • Delegates

40
Thank You!
Write a Comment
User Comments (0)
About PowerShow.com