CSharpTest.Net
Clone Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.IO Namespace > SharedMemoryStream Class : Clone Method

Glossary Item Box

Returns a 'clone' of this stream so that the two instances act independantly upon a single set of data

Syntax

Visual Basic (Declaration) 
Public Function Clone() As SharedMemoryStream
C# 
public SharedMemoryStream Clone()

Example

Library/Library.Test/TestSegmentedStream.cs

C#Copy Code
using (ManualResetEvent mreStart = new ManualResetEvent(false))
using (SharedMemoryStream shared = new SharedMemoryStream(5))
using (SharedMemoryStream copy = shared.Clone())
{
    Thread[] allwriters = new Thread[Math.Max(1, Environment.ProcessorCount - 1)];
    for (int tix = 0; tix < allwriters.Length; tix++)
    {
        allwriters[tix] = new Thread(
            delegate()
                {
                    using (SharedMemoryStream stream = shared.Clone())
                    {
                        Random r = new Random();
                        mreStart.WaitOne();
                        for (int i = 0; i < 1000; i++)
                        {
                            PrimitiveSerializer.Int32.WriteTo(i, stream);
                            Thread.SpinWait(r.Next(i));
                        }
                    }
                }
            );
        allwriters[tix].IsBackground = true;
        allwriters[tix].Start();
    }

    mreStart.Set();
    foreach(Thread t in allwriters)
        Assert.IsTrue(t.Join(100));

    for (int i = 0; i < 1000; i++)
    {
        int value = PrimitiveSerializer.Int32.ReadFrom(copy);
        Assert.AreEqual(i, value);
    }
}
VB.NETCopy Code
Using mreStart As New ManualResetEvent(False)
    Using [shared] As New SharedMemoryStream(5)
        Using copy As SharedMemoryStream = [shared].Clone()
            Dim allwriters As Thread() = New Thread(Math.Max(1, Environment.ProcessorCount - 1)) {}
            Dim tix As Integer = 0
            While tix < allwriters.Length
                allwriters(tix) = New Thread(Function() Do
                    Using stream As SharedMemoryStream = [shared].Clone()
                        Dim r As New Random()
                        mreStart.WaitOne()
                        Dim i As Integer = 0
                        While i < 1000
                            PrimitiveSerializer.Int32.WriteTo(i, stream)
                            Thread.SpinWait(r.[Next](i))
                            System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
                        End While
                    End Using
                End Function)
                allwriters(tix).IsBackground = True
                allwriters(tix).Start()
                System.Math.Max(System.Threading.Interlocked.Increment(tix),tix - 1)
            End While

            mreStart.[Set]()
            For Each t As Thread In allwriters
                Assert.IsTrue(t.Join(100))
            Next

            Dim i As Integer = 0
            While i < 1000
                Dim value As Integer = PrimitiveSerializer.Int32.ReadFrom(copy)
                Assert.AreEqual(i, value)
                System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
            End While
        End Using
    End Using
End Using

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys