LA.NET [EN]

Jun 23

In the last post, we’ve seen how to marshal back the results obtained on a secondary thread so that controls are updated on the GUI thread. Today we’re going to start looking at synchronization contexts. Synchronization contexts are abstractions for marshalling between threads. In other words, they abstract those scenarios where you cannot call a method from the current thread and need to make sure that the method is executed on a specific thread (as we’ve seen, GUIs fall in this kind of scenario).

As we’ve seen in the previous example, GUI controls can only be updated from the GUI thread and in that example, we’ve relied on the ISynchronizeInvoke method for running the code on the correct thread. In practice, whenever we’ve got a reference to a control, we can use its Invoke or BeginInvoke method to marshal work back to the current thread. The problem is getting a reference to the control. When we build the form, this is simple because we know the controls that are placed on the form and it is easy to get a reference to any of them.

However, suppose we’re building a general component that needs to be reused on several GUI apps. Now things become harder and we’d need to write boilerplate code for getting a control so that we can marshal work back into the GUI thread. This is where synchronization contexts step in and save the day. Before delving into the specifics of the synchronization contexts used on GUIs, lets take a step back an study the general API introduced by the base SynchronizationContext class:

public class SynchronizationContext {
  public virtual SynchronizationContext CreateCopy();
  public bool IsWaitNotificationRequired();
  public virtual void OperationCompleted();
  public virtual void OperationStarted();
  public virtual void Post(SendOrPostCallback d, 
object state); public virtual void Send(SendOrPostCallback d,
object state); public virtual int Wait(IntPtr[] waitHandles,
bool waitAll,
int millisecondsTimeout); public static SynchronizationContext Current { get; } public static void SetSynchronizationContext(
SynchronizationContext syncContext); }



As you can see, you can get (Current property) or set (by calling the static SetSynchronizationContext) a thread’s synchronization context. Synchronization contexts are always obtained through the “current” ExecutionContext (which you can get through the ExecutionContext property of the thread class). Notice that if you want to interact with synchronization contests, you must be prepared for getting a null reference.



Even though a synchronization context is part of the ExecutionContext, it’s important to keep in mind that queuing an item on the thread pool will never propagate the “current” SynchronizationContext (not even when you use the QueueUserWorkItem method – event though the execution context is propagated, the synchronization context is one of the items that will not be propagated to the new thread).



Post and Send are probably the most important methods exposed by the class. As you can see, both expect a SendOrPostDelegate and an option object parameter for passing some state to that delegate. The difference is that Post performs the operation in asynchronous fashion while Send performs a synchronous callback. The default implementation of these methods is rather simple: Post uses the thread pool while send simply invokes the delegate.



The OperationStarted and OperationCompleted methods let you receive notifications when an operation starts and ends. The default SynchronizationContext doesn’t do anything in these methods, but as we’ll see, they can be used for (for example) tracking running operations.



Finally, we’ve got a Wait method. The CLR uses this method for waiting if you the IsWaitNotificationRequired property is set to true. What this means is that whenever you wait on a thread that has a synchronization context that has this property set to true, your wait calls end up being redirected to this Wait method. Custom synchronization contexts override this method to perform some specific actions whenever a thread needs to wait on a specific action.



And that’s it. After this brief introduction, we’re ready to take a look at the synchronization contexts used by windows forms. But we’ll leave that to the next post. Keep tuned!

9 comments so far

  1. PresidentY
    1:54 pm - 8-5-2009

    8 free mp3 ringtones downloads
    20 what mp3 players are compatible with itunes
    23 how to put music on mp3 players using limewire
    550 free mp3 players
    7 download mp3 music free
    7 top mp3 players
    6 radios with mp3 cd players
    3 free reggae music mp3 downloads
    4 free classical mp3 downloads
    20 sansa mp3 players
    275 sony network walkman mp3
    5 free mp3 downloads online
    336 circus mp3
    363 free mp3 songs
    481 shrek mp3
    4 mp3 free downloads music – greenday
    251 where do i plug my mp3 player into
    10 free trace adkins mp3 downloads
    28 free mp3 mobile downloads
    22 sandisk mp3 players
    2 free mp3 music video downloads
    18 mp3 players for sale
    5 mp3 players with wifi
    11 free red end of silence mp3 downloads
    7 anastacia free mp3 downloads
    6 yankee doodle mp3 music free download
    5 mp3 players compared
    240 enya mp3
    5 free afroman mp3 downloads
    4 40gb mp3 players

    Big Country – East Of Eden (Extended)

    mp3
    17 free music downloads mp3
    9 difference between ipods and mp3 players
    8 free mp3 music download site
    9 history of mp3 players
    245 one more drink mp3
    24 types of mp3 players
    11 free tubthumping mp3 downloads
    5 pre loaded mp3 players
    4 free crabb family mp3 downloads

    Grupo Batuque – Ole Ola Futebol Bonito!

    11 legal free mp3 downloads
    4 free soaking music mp3 downloads
    6 free cheerleading music mixes mp3 downloads
    521 how does an mp3 player work
    2 free genesis music mp3 downloads
    2 download music videos mp3 free
    5 free mp3 downloads megadeth 99 ways to die
    7 free music video downloads for mp3 player
    8 free mp3 ringtones downloads
    378 free aries mp3 downloads

    Himekami – Kaze No Uta

    2 free download mp3 carnatic music
    158 compare mp3 players and ipods
    737 kanye west mp3
    84 very cheap mp3 players
    3 download free music mp3
    1144 who invented the mp3 player
    123 sony mp3 players
    2 booty music free mp3 download
    5 mp3 players how do they work
    3 download free mp3 music software

    Gregorian Chants – Songs Of Abba

    237 mp3 converter
    521 how does an mp3 player work
    265 mp3 search
    487 paramore mp3
    4 free soaking music mp3 downloads
    70 absolutely free mp3 downloads
    66 free mp3 music downloads uk
    2 catholic music free mp3 downloads
    7 coby mp3 players
    314 best mp3 players

    Radio Massacre International – 3

    1377 beyonce mp3
    463 akon mp3
    44 download free mp3 music
    426 kanye west heartless mp3
    68 how much do mp3 players cost
    521 how does an mp3 player work
    6 best sport mp3 players
    4 30 gb mp3 players
    6 mp3 players and ipods should not be allowed in school
    3 free music mp3 download

    Himekami – Japanese Titles

    337 mp3 player history
    401 single ladies mp3
    96 mp3 free downloads music
    22 download free mp3 music site
    27 free christian music downloads mp3
    43 5 gb mp3 players
    237 foo fighters mp3
    17 can you use itunes with other mp3 players
    41 how to use mp3 players
    550 free mp3 players

    Natasha Atlas – Just Like A Dream

    305 how to transfer songs to a mp3 player
    2 free download mp3 carnatic music
    3 need for speed 4 mp3 free music download
    27 what year were mp3 players invented
    399 compare mp3 players
    6 how to download free mp3 music legally
    333 how do i add music to my mp3 player
    123 sony mp3 players
    5 free mp3 downloads megadeth 99 ways to die
    11 mp3 players uk

    Reuben Wilson – Love Bug

    8 free mp3 downloads delirious
    2 free mp3 black metal music 100% free downloads
    4 free classical mp3 downloads
    14 free downloads mp3
    324 how to transfer songs to my mp3 player
    44 download free mp3 music
    27 what year were mp3 players invented
    20 what mp3 players are compatible with itunes
    11 free mp3 music download websites
    2 free new wave music mp3 download

    Swingle Singers – Someone”s Rocking My Dreamboat

    568 how do mp3 players work
    2 free mp3 music video downloads
    10 free hindi mp3 downloads
    258 mp3 to aac converter
    846 nickelback mp3
    10 zen mp3 players
    448 mp3 ringtones
    4 free soaking music mp3 downloads
    9 free mp3 downloads legal
    399 compare mp3 players

    Schandmaul – frei

    333 how do i add music to my mp3 player
    22 samsung mp3 players
    5 john cena free mp3 downloads
    7 free hed pe lets ride mp3 downloads
    66 free mp3 music downloads uk
    305 how to transfer songs to a mp3 player
    8 tsunami bomb mp3 downloads free
    6 eternal snow free mp3 downloads
    257 lil wayne mp3
    23 how to put music on mp3 players using limewire

    Natasha Atlas – One Brief Moment

    4 free tq mp3 music downloads
    13 john 5 mp3 free downloads
    17 free music downloads mp3
    276 sony walkman mp3 player
    291 paramore decode mp3
    11 free gospel mp3 downloads
    2 free legal music to download to mp3
    6 radios with mp3 cd players
    7 mp3 workout free downloads
    22 samsung mp3 players

    Menace Ruine – The Die Is Cast

    279 heartless kanye west mp3
    22 download free mp3 music site
    907 decode mp3
    123 sony mp3 players
    41 how to use mp3 players
    493 explain what is mp3
    36 how do you download music to mp3 players in uk
    13 free music for mp3 players
    11 download free mp3 player music
    12 free mp3 files downloads

    David Bowie – John, I”m Only Dancing

    374 movie soundtrack mp3s
    7 download mp3 music free
    314 best mp3 players
    8 tsunami bomb mp3 downloads free
    4 free soaking music mp3 downloads
    6 free mp3 downloads
    27 what year were mp3 players invented
    333 how do i add music to my mp3 player
    395 free mp3 music downloads
    426 kanye west heartless mp3

    Roberto Carlos – Jovens Tardes de Domingo

    7 christmas music free mp3 downloads
    8 free mp3 downloads of skillet
    550 free mp3 players
    20 what mp3 players are compatible with itunes
    6 san disk mp3 players
    2 booty music free mp3 download
    3 full versions afroman music for mp3 to download for free
    2 catholic music free mp3 downloads
    7 brian head welch free mp3 downloads
    4 christian download free mp3 music

    Ordo Equitum Solis – How

    27 free christian music downloads mp3
    10 free techno downloads mp3
    17 creative mp3 players
    6 san disk mp3 players
    816 what does mp3 stand for
    70 free mp3 music player downloads
    9 free mp3 downloads legal
    446 free mp3 music
    6 free parry gripp mp3 downloads
    240 enya mp3

    Le Tigre – Hot Topic

    12 sylvania mp3 players
    5 mp3 players and accersios
    426 kanye west heartless mp3
    4 mp3 free downloads music – greenday
    12 inexpensive mp3 players
    2 free music download sample mp3 easy listening
    302 bossa nova mp3
    10 free arabic mp3 downloads
    free mp3 downloads
    4 free mp3 music to download

    Kou Shibasaki – actuality

    7 free digimon themesong downloads for mp3
    6 free parry gripp mp3 downloads
    770 free mp3 ringtones
    mp3 players
    3 download mp3 music for free
    20 what mp3 players are compatible with itunes
    6 free mp3 downloads for nursery rhymes
    989 mp3 search engines
    689 sony walkman mp3 software
    6 radios with mp3 cd players

    Katatonia – Had To (Leave)

    30 free mp3 video downloads
    10 free arabic mp3 downloads
    12 sylvania mp3 players
    378 free aries mp3 downloads
    6 free cheerleading music mixes mp3 downloads
    96 mp3 free downloads music
    4 totally free afroman mp3 music downloads
    4 free mp3 music to download
    23 how to put music on mp3 players using limewire
    2134 mp3 players

    Depeche Mode – Just Can”t Get Enough

    11 christian music free downloads mp3
    6 yankee doodle mp3 music free download
    13 john 5 mp3 free downloads
    695 heartless mp3
    399 compare mp3 players
    144 free legal mp3 downloads
    9 free indian mp3 downloads
    8 mp3 players reviews
    410 free mp3 music download uk
    4 download free legal mp3 music

    David Bowie – Track 10

    70 free mp3 music player downloads
    258 mp3 to aac converter
    827 britney spears mp3
    941 if i were a boy mp3
    4 free download music mp3
    7 christmas music free mp3 downloads
    468 taylor swift mp3
    36 how do you download music to mp3 players in uk
    379 fifth element soundtrack mp3s
    5 mp3 players how do they work

    Anthony Hamilton – Love And War – Hamilton, Anthony & Macy Gray

    12 inexpensive mp3 players
    4 free christian music downloads mp3 yahoo
    4 music mp3 download free
    689 sony walkman mp3 software
    3 free mp3 downloads music
    37 free music mp3 downloads
    11 christian music free downloads mp3
    9 free mp3 music download programs
    3 download free music mp3
    9 free mp3 angel song downloads

    Jimmy Somerville – Ain”t No Mountain High Enough

    7 free online mp3 downloads
    379 fifth element soundtrack mp3s
    18 mp3 players for sale
    5 should”ve said no free music download for mp3
    1137 what is an mp3 player
    58 free music downloads for mp3 players
    4 where to find free mp3 music downloads
    11 christian music free downloads mp3
    mp3
    5 mp3 players and accersios

    Magic Slim – Living In My Neighborhood

    480 free mp3 download
    5 mp3 players + mac compatability
    257 lil wayne mp3
    5 mp3 players and accersios
    6 best sport mp3 players
    3 download free music mp3
    7 free music video downloads for mp3 player
    4 totally free afroman mp3 music downloads
    4 free video game mp3 music download
    830 how do i put songs on my mp3 player

    Burl Ives – Poor Little Jimmie

    Cafe Del Mar – The Messenger (A New Funky Generation)
    Thunderstone – Will To Power
    HIM – Burried Alive By Love
    Sodom – Ausgebombt
    Dvar – Iillah

  2. enaniaLeact
    4:59 pm - 8-22-2009

    Hiya Peps,

    I spent 3 days looking on the web, finally I found your site I thikn, I wiol be on here for som timew!

    I am looking for yojr assistance

    “I am looking for a Asia properties website that is like http://www.primelocation.comhttp://www.primelocation.com?

    However, from a quick search, I have found a very useful Asian Property website with the name

    http://www.kothi.com – India property prices which shows http://www.kothi.com – Delhi Property . Just thought I”d let you Guys know, because knowledge is for sharing.

    Best Regards

    Pala Alfred

  3. tinahelpusa
    4:32 pm - 11-8-2009

    Hi everybody,

    My name is Tina, I am 41 yrs old, living in Austin, TX.

    I”d love to make good close friends here.

    Thanks,
    Tina.

  4. usatodayhelp
    11:49 am - 11-18-2009

    hi there, my name is Allan.

    found this website and read some great discussion and feedback so decided to join

    i am happy to help others and offer advice where possible :)

  5. evaline9
    11:14 pm - 11-19-2009

    Hi everybody,

    My name is Eva, I am 41 yrs old, living in Scottsdale, AZ.

    I”d love to make good close friends here.

    Thanks,
    Eva.

  6. tomnclick
    11:36 am - 2-26-2010

    I”m new to this , but I guess I shall be spending quite a bit of time here as there seems to be so much going on! I”m hoping to make some new friends

    so how is everyone doing?? :)

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>