Superior ways to a better UI - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Superior ways to a better UI

Description:

The One Golden Rule. Delegate.BeginInvoke() Generate custom events. Delegates. Callbacks ... SSW Link Auditor. Link Auditor. Threading was used to achieve. A ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 33
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 Adam Cogan
2
Admin Stuff
  • Attendance
  • You initial sheet
  • Hands On Lab
  • You get me to initial sheet
  • Homework
  • Certificate
  • At end of 10 sessions
  • If I say if you have completed successfully ?

3
Session 3 Threading
  • Agenda
  • What is Threading?
  • Application.DoEvents ()
  • Thread.Start ()
  • The One Golden Rule
  • Delegate.BeginInvoke()
  • Generate custom events
  • Delegates
  • Callbacks
  • Locking / Monitor.Enter
  • Threadpool

4
Session 3 Threading
5
What is Threading?
Running
Sleeping
Sleeping
Running
Running
  • Threading is an illusion.
  • E.g. searching file system while waiting for web
    site to load
  • Threading is not the perfect solution for
    everything, and we should only use it where
    necessary.
  • Restrict your self to a UI thread, and a few
    worker threads.

6
What is Threading?
  • Threading allows several tasks to run
    simultaneously
  • Two major uses
  • Responsive UI
  • e.g. work with results as they come back
  • Optimizing backend processes
  • e.g. multiple search engines at once

7
What is Threading?
  • Responsive UI

Worker Thread
UI Thread
Start
Start
Execute
Update UI
Done
Done
8
What is Threading?
  • BAD Optimizing backend processes (Scenario)
  • e.g. no threading have to wait for results
    for first site before calling second

SearchGoogle()
FetchData()
Fetch
SearchGoogle()
Return
SearchYahoo()
SearchYahoo()
Fetch
Merge
Return
9
What is Threading?
  • GOOD Optimizing backend processes (Solution)

Worker 1
SearchGoogle()
Return
FetchData
Worker 2
Merge
SearchYahoo()
Done
Return
10
How do I use threading?
  • 3 Ways
  • Application.DoEvents()
  • Thread.Start()
  • Delegate.BeginInvoke(),ISynchronizeInvoke,IAsync
    Result

11
Application.DoEvents()
12
Application.DoEvents()
  • The UI was partially more responsive
  • Still a blocking call
  • Still only allows only one worker process to
    execute at a time
  • UI updates are tightly bound (worker code is
    directly tied to progress bar cant
    componentise search logic)

13
Application.DoEvents()
  • Works (but not a great solution)

DoWork()
UI
Start
Start
Execute
Update UI
Done
Done
14
Application.DoEvents()
  • Not appropriate
  • Ideal scenario is

Worker 1
SearchGoogle()
Return
FetchData
Worker 2
Merge
SearchYahoo()
Done
Return
15
Thread.Start()
Use a ThreadStart delegate to specify the program
code executed by a thread.
16
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
  • There is no real way of knowing when the thread
    has finished cant return a status code saying
    Im finished!
  • No way of telling when an exception is thrown
  • Bad to check the status of the thread

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

Worker Thread
UI Thread
Start
Start
Execute
Update UI
Done
Done
18
Thread.Start()
  • Not appropriate
  • Ideal scenario is

Worker 1
SearchGoogle()
Return
FetchData
Worker 2
Merge
SearchYahoo()
Done
Return
19
  • The One Golden Rule

20
The One Golden Rule
  • The One Golden Rule
  • Windows forms controls must only be accessed from
    the UI thread

21
Delegate.BeginInvoke()
  • 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.

22
Delegate.BeginInvoke()
  • The UI was completely responsive
  • UI updates are no longer tightly bound (event
    based)
  • We know exactly when the call has finished (with
    a callback object)

23
Delegate.BeginInvoke()
  • Perfect solution

Worker Thread
UI Thread
Start
Start
Execute
Update UI
Done
Done
24
Delegate.BeginInvoke()
  • Perfect solution

Worker 1
SearchGoogle()
Return
FetchData
Worker 2
Merge
SearchYahoo()
Done
Return
25
Locking / Monitor.Enter
  • If you are going to read/write from/to an object
    when using multiple threads, lock the object
    first using lock (obj)
  • E.g. reading contact details into a form

26
  • ManagedThreadPool

27
ManagedThreadPool
  • What is a Thread Pool?

28
  • Code Sample
  • Implementing ManagedThreadPool

29
  • A Real World Example
  • SSW Link Auditor

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

32
Session 3 Threading
  • Summary
  • What is Threading?
  • Application.DoEvents ()
  • Thread.Start ()
  • The One Golden Rule
  • Delegate.BeginInvoke()
  • Generate custom events
  • Delegates
  • Callbacks
  • Locking / Monitor.Enter
  • Threadpool

33
2 things
AdamCogan_at_ssw.com.au
34
Thank You!
ASP.NET is Cool
Write a Comment
User Comments (0)
About PowerShow.com