Threading - MonitorMicro (Wait and Pulse)

MonitorMicro is designed to complement the existing Monitor class. For use however, the lock object must be of type MicroLock. Example as below:

  

    public class BasicLockExample
    {
        private object _locker = new MicroLock();

        public void Run()
        {
            while (true)
            {
                lock (_locker)
                {
                    if (MonitorMicro.Wait(_locker, 1000))
                    {
                        Debug.Print("Signalled OK");
                    }
                    else
                    {
                        Debug.Print("Timout waiting");
                    }


                }
            }
        }

        public void DoWork()
        {
            lock (this._locker)
            {
                MonitorMicro.Pulse(_locker);
            }
        }
    }

 

String Formatting

Formatting is supported for all objects that implement the IFormattable interface. In NetMf primitive types do not implement this interface like they do in the full framework, They do however have the overloaded ToString(string format) method available on them. StringUtility.Format will explicitly use the overloaded ToString(string format) for each of the following types Int16,Int32,Int64... All other types will have the generic object.ToString() method called on them, ignoring any format string provided.

Sample Usage Sample output [variable]
string s = StringUtility.Format("{0:F}",7); [s] "7.00"
string s = StringUtility.Format("The {0} sat on the {1}", "cat", "mat");

[s] "The cat sat on the mat"

DateTime dt = new DateTime(2010,10,01);
string s = StringUtility.Format("Date is: {0:dd/M/yyyy}", dt);

[s] "01/10/2010"

String Replace

Replace will replace all instances of the specified search string with the replacement string provided.

Sample Usage Sample output [variable]
string original = "ABC";
string done = original.Replace("B", "D");
[done] "ADC"
string original = "A B C";
string done = original.Replace("B", "BB");
[done] "A BB C"

TryParseX

Implementations of the commonly used TryParse[Int/Short/Long...] methods.

Sample Usage Sample output [variable]
int i = 0;
string s = "10";
if(Parse.TryParseInt(s, out i))
{
     Debug.Print("Success: " + i);
}
else
{
      Debug.Pring("Failed to Parse");
}

Debug Output:

Success: 10



Last edited Nov 1, 2010 at 1:01 PM by mcdermottr, version 12

Comments

No comments yet.