David

  • About Me
  • GoGetter for Windows Phone 7
  • My Etsy Shop
  • Random
  • Archive
  • RSS
  • Ask me anything

Mutex Snippet for Windows Phone 7

I created a little snippet for my app called “Surround with Mutex”. Basically it protects any call to the isolated storage with a Mutex.

A Mutex is: “A synchronization primitive that can also be used for interprocess synchronization.”

http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx

I had to use this due to my foreground and background agent possibly making calls at the same time to my files within isolated storage. If you do not protect your calls with a Mutex it is possible you could corrupt the data files within isolated storage. I had help with the Mutex with this bit of code example: http://www.albahari.com/threading/part2.aspx#_Mutex

I then took it for my needs and created a snippet for Visual Studio 2010. I named my snippet file Mutex.snippet. 

It is essentially just an XML document with the code below. Instead of having file named Mutex.xml it needs to be renamed to Mutex.snippet.

<?xml version=”1.0” encoding=”utf-8” ?>

<CodeSnippet Format=”1.0.0” xmlns=”http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet”>

  <Header>

    <Title>Surround with Mutex</Title>

    <Author>your name here</Author>

    <Shortcut>smutex</Shortcut>

    <Description>Surround with Mutex</Description>

    <SnippetTypes>

      <SnippetType>Expansion</SnippetType>

      <SnippetType>SurroundsWith</SnippetType>      

    </SnippetTypes>

  </Header>

  <Snippet>

    <Code Language=”CSharp”>

      <![CDATA[

                        // Naming a Mutex makes it available computer-wide. Use a name that’s

                        // unique to your company and application (e.g., include your URL).

                        using (var _mutex = new Mutex(false, “myappmutex-www.yourwebaddress.com”))

                        {

                            // Wait a few seconds if contended, in case another instance

                            // of the program is still in the process of shutting down.

                            if (!_mutex.WaitOne(TimeSpan.FromSeconds(3)))

                            {

                                return;

                            }

                            $selected$ $end$

                        }   

      ]]>

    </Code>

  </Snippet>

</CodeSnippet>

Once this file is created in your project you need to add it to your snippet library. You can do this by going to Tools->Code Snippet Manager… then clicking Import…

Then select the snippet you created “Mutex.snippet”. Once you have done this, your snippet is immediately available for use. I just select the code I would like to surround with the Mutex and press control+K, control+S then I am able to use the snippet I created. 

    • #mutex
    • #c
    • #.net
    • #csharp
    • #programming
    • #asp.net
    • #wp7
    • #wp7dev
    • #windows phone
    • #Windows Phone 7
    • #windowsphone
  • 22 hours ago
  • Comments
  • Permalink
  • Share
Star Trekking while coding. 
Pop-upView Separately

Star Trekking while coding. 

  • 23 hours ago
  • Comments
  • Permalink
  • Share
I was able to score these Clif Bars for a dollar each at Sprouts this weekend. Great bargin!
Pop-upView Separately

I was able to score these Clif Bars for a dollar each at Sprouts this weekend. Great bargin!

  • 1 day ago
  • Comments
  • Permalink
  • Share

Memory Profiling for Application Performance

Something that I need to use with my app.

  • 1 day ago
  • Comments
  • Permalink
  • Share

Marketplace now open in 5 new markets

Excellent!

  • 1 day ago
  • Comments
  • Permalink
  • Share

Mad Catz Cyborg M.M.O. 7 gaming mouse hands-on - Engadget

You Gamers need this mouse. This is so Transformer like. 

  • 1 day ago
  • Comments
  • Permalink
  • Share

Nokia Lumia 800 White hands-on -- Engadget

Another article showing the White Nokia Lumia 800. Again, beautiful phone.

  • 1 day ago
  • Comments
  • Permalink
  • Share

Nokia officially announces the Lumia 800 in white

This phone is beautiful! Are you planning on getting a Nokia Lumia 800 or 910? Or are you waiting for Windows 8 enabled phones?

  • 1 day ago
  • Comments
  • Permalink
  • Share
'\x3ciframe width=\x22500\x22 height=\x22281\x22 src=\x22http://www.youtube.com/embed/pl3spwzUZfQ?wmode=transparent\x26autohide=1\x26egm=0\x26hd=1\x26iv_load_policy=3\x26modestbranding=1\x26rel=0\x26showinfo=0\x26showsearch=0\x22 frameborder=\x220\x22 allowfullscreen\x3e\x3c/iframe\x3e'

FBI Conference Call - #FFF. (by TheDigitalfolklore)

  • 1 day ago
  • Comments
  • Permalink
  • Share
work work work
Pop-upView Separately

work work work

  • 3 days ago
  • Comments
  • Permalink
  • Share
← Newer • Older →
Page 1 of 17

Logo

Portrait/Logo

I created an awesome "Getting Things Done" app and I make awesome art.

GoGetter for Windows Phone 7: www.gogetterapp.com
Etsy Shop: davidrodriguez.etsy.com

Me, Elsewhere

  • @davidrodriguez on Twitter
  • Facebook Profile
  • davidarodriguez on Flickr
  • iimmerse on Last.fm
  • My Skype Info
  • Linkedin Profile

Twitter

loading tweets…

  • RSS
  • Random
  • Archive
  • Ask me anything
  • Mobile

© 2011 David A. Rodriguez. Effector Theme by Carlo Franco.

Powered by Tumblr