multi-threading wpf inside of autocad fenton webb autodesk developer evangelist

16
Multi-Threading WPF Inside of AutoCAD Fenton Webb Autodesk Developer Evangelist

Upload: hubert-dawson

Post on 05-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Multi-Threading WPF Inside of AutoCAD Fenton Webb Autodesk Developer Evangelist

Multi-Threading WPF Inside of AutoCAD

Fenton WebbAutodesk Developer Evangelist

Page 2: Multi-Threading WPF Inside of AutoCAD Fenton Webb Autodesk Developer Evangelist

About the Presenter

Fenton WebbDeveloper Technical ServicesAmericasAutodesk, Inchttp://twitter.com/fentonwebb

Fenton has been a member of the Autodesk DevTech team since 2000. Originally a member of our EMEA team, he has recently relocated to California to work for DevTech Americas.  Fenton is an expert in all the AutoCAD APIs, AutoCAD OEM, RealDWG and Plant3d. He particularly enjoys travelling to evangelise the APIs he support at our annual Developer Days conferences. 

 

Before joining Autodesk, Fenton worked for an ADN partner developing ObjectARX applications in the Civil and Structural Engineering domain.

Page 3: Multi-Threading WPF Inside of AutoCAD Fenton Webb Autodesk Developer Evangelist

Autodesk Developer Network

Access to almost all Autodesk software and SDK’s Including early access to Beta software

Members-only website with 1000s of technical articles Unlimited technical support Product direction through conferences Marketing benefits

Exposure on autodesk.com Promotional opportunities

1 to 3 free API training classes Based on user level

www.autodesk.com/joinadn

Page 4: Multi-Threading WPF Inside of AutoCAD Fenton Webb Autodesk Developer Evangelist

Agenda

What is WPF?What is Multi-Threading?Multi-Threading and WPFWhat about Multi-Threading WPF inside of AutoCAD?

Page 5: Multi-Threading WPF Inside of AutoCAD Fenton Webb Autodesk Developer Evangelist

What is WPF?

Windows Presentation Foundation Next Generation GUI Developer Platform from Microsoft

New update to WinForms Packaged with .NET Version 3.0 Driven by Direct3D drivers

Uses XAML eXtensible Application Markup Language XML based Declarative Language

Requires very little programming

Technical Information

Page 6: Multi-Threading WPF Inside of AutoCAD Fenton Webb Autodesk Developer Evangelist

What is WPF?

Windows Presentation Foundationby Microsoft

Let’s see it for real...

Page 7: Multi-Threading WPF Inside of AutoCAD Fenton Webb Autodesk Developer Evangelist

What is Multi-Threading?All about Threads...

The utilization of multiple concurrent execution threads in the same process

MyApplication.exe

Resources

Main Execution Thread

Thread 1

Thread 2

Thread 3

Notify

Threads…• Are used to do “work”.

• Run separately from UI/Main Thread.

• Share the same application resources as the UI/Main Thread.

• Usually need to notify the Main Thread of “work” that is complete

• To update UI, etc.

• Only use what you need. Reuse.

Notify

Notify

Spawn Spawn

Spawn

Page 8: Multi-Threading WPF Inside of AutoCAD Fenton Webb Autodesk Developer Evangelist

Multi-Threading and WPFRules of…

WPF works on the UI/Main Thread Based on Windows Messaging, like MFC/Win32 Worker thread UI updates must be done on the UI/Main Thread

You cannot update WPF UI from a worker thread You need to switch to the UI/Main thread.

Page 9: Multi-Threading WPF Inside of AutoCAD Fenton Webb Autodesk Developer Evangelist

Multi-Threading and WPFTools of the Job

WPF derives all controls from DispatcherObject Allows easy invocation of code on the Main Thread.

MyControl.Dispatcher.BeingInvoke() – asynchronous Allows you to set an update priority

MyControl.Dispatcher.Invoke() – synchronous

For UI lots of updates per second Recommend using the DispatcherPriority.Background priority setting Too many updates can cause the UI to become slow and/or jerky

Use a timer to know when you authorize an update

Page 10: Multi-Threading WPF Inside of AutoCAD Fenton Webb Autodesk Developer Evangelist

Multi-Threading and WPFTools of the job - Timers

Timer classes .NET provides

System.Windows.Forms.Timer Runs on UI main thread only. System.Timers.Timer  Runs on UI main thread, or worker thread

SynchronizingObject is not compatible with WPF System.Threading.Timer Worker thread only.

All can be used fairly easily inside of WPF

Page 11: Multi-Threading WPF Inside of AutoCAD Fenton Webb Autodesk Developer Evangelist

Multi-Threading and WPFTools of the job - Threading

Threading classes .NET provides

System.Threading.Thread class Raw .NET threading class I don’t recommend using this class in WPF

BackGroundWorker class Neatly packages up multi threading to work with WPF Perfect for use with WPF Thread switching automatically taken care of

Switching from the worker thread to the main thread.

Page 12: Multi-Threading WPF Inside of AutoCAD Fenton Webb Autodesk Developer Evangelist

Multi-Threading and WPFSimple Example

Demo “Simple Windows Application”

DemonstratingWindows Application

Using threads

Providing Feedback while doing “work”

Page 13: Multi-Threading WPF Inside of AutoCAD Fenton Webb Autodesk Developer Evangelist

What about Multi-Threading inside AutoCAD

AutoCAD is an Single Threaded Application You cannot use worker threads to access any part of AutoCAD. It will crash.

You may do none-AutoCAD related “work”

Is it possible?

Page 14: Multi-Threading WPF Inside of AutoCAD Fenton Webb Autodesk Developer Evangelist

Multi-Threading and WPFAutoCAD Ribbon Example

Demo “AutoCAD Performance Monitor”

DemonstratingCUI Custom Control

Using Timers

Using WPF Dispatcher

Using BackGroundWorker

Providing Feedback while doing “work”

Page 15: Multi-Threading WPF Inside of AutoCAD Fenton Webb Autodesk Developer Evangelist

Multi-Threading WPF Inside of AutoCAD

Fenton WebbSenior Developer Evangelist

Thank you…

Page 16: Multi-Threading WPF Inside of AutoCAD Fenton Webb Autodesk Developer Evangelist