CSharpTest.Net
OnError Event
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Threading Namespace > WorkQueue<T> Class : OnError Event

Glossary Item Box

Raised when a task fails to handle an error

Syntax

Visual Basic (Declaration) 
Public Event OnError As ErrorEventHandler
C# 
public event ErrorEventHandler OnError

Example

Library/Library.Test/TestTransactedCompoundFile.cs

C#Copy Code
using (TempFile temp = new TempFile())
using (ManualResetEvent stop = new ManualResetEvent(false))
using (TempFile copy = new TempFile())
using (TransactedCompoundFile test = new TransactedCompoundFile(
    new TransactedCompoundFile.Options(temp.TempPath) {BlockSize = 512, CreateNew = true}))
using (WorkQueue workers = new WorkQueue(5))
{
    bool failed = false;
    workers.OnError += (o, e) => failed = true;
    for(int i=0; i < 5; i++)
        workers.Enqueue(() => ExersizeFile(stop, test));

    do
    {
        System.Threading.Thread.Sleep(1000);
        test.Commit();
        File.Copy(temp.TempPath, copy.TempPath, true);
        Assert.AreEqual(0, copy.Length%512);
        int hcount = (int)(copy.Length/512);

        using (TransactedCompoundFile verify = new TransactedCompoundFile(
            new TransactedCompoundFile.Options(copy.TempPath) {BlockSize = 512, CreateNew = false}))
        {
            OrdinalList free = new OrdinalList();
            free.Ceiling = hcount;
            for(int i=0; i < hcount; i++)
            {
                uint h = verify.Create();
                free.Add((int)h);
                if (h >= hcount)
                    break;
            }

            int verifiedCount = 0;
            OrdinalList used = free.Invert(hcount);
            foreach (uint h in used)
            {
                // skip reserved offsets.
                if (h % (512 / 4) == 0 || (h + 1) % (512 / 4) == 0)
                    continue;

                IOStream.ReadAllBytes(verify.Read(h));
                verifiedCount++;
            }
            System.Diagnostics.Trace.WriteLine("Verified handle count: " + verifiedCount);
        }

    } while (!failed && System.Diagnostics.Debugger.IsAttached);

    stop.Set();
    workers.Complete(false, 1000);
    Assert.IsFalse(failed);
}
VB.NETCopy Code
Using temp As New TempFile()
    Using [stop] As New ManualResetEvent(False)
        Using copy As New TempFile()
            Using test As New TransactedCompoundFile(New TransactedCompoundFile.Options(temp.TempPath) With { _
                Key .BlockSize = 512, _
                Key .CreateNew = True _
            })
                Using workers As New WorkQueue(5)
                    Dim failed As Boolean = False
                    workers.OnError += Function(o, e) failed = True
                    Dim i As Integer = 0
                    While i < 5
                        workers.Enqueue(Function() ExersizeFile([stop], test))
                        System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
                    End While

                    Do
                        System.Threading.Thread.Sleep(1000)
                        test.Commit()
                        File.Copy(temp.TempPath, copy.TempPath, True)
                        Assert.AreEqual(0, copy.Length Mod 512)
                        Dim hcount As Integer = DirectCast((copy.Length / 512), Integer)

                        Using verify As New TransactedCompoundFile(New TransactedCompoundFile.Options(copy.TempPath) With { _
                            Key .BlockSize = 512, _
                            Key .CreateNew = False _
                        })
                            Dim free As New OrdinalList()
                            free.Ceiling = hcount
                            Dim i As Integer = 0
                            While i < hcount
                                Dim h As UInteger = verify.Create()
                                free.Add(DirectCast(h, Integer))
                                If h >= hcount Then
                                    Exit While
                                End If
                                System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
                            End While

                            Dim verifiedCount As Integer = 0
                            Dim used As OrdinalList = free.Invert(hcount)
                            For Each h As UInteger In used
                                ' skip reserved offsets.
                                If h Mod (512 / 4) = 0 OrElse (h + 1) Mod (512 / 4) = 0 Then
                                    Continue For
                                End If

                                IOStream.ReadAllBytes(verify.Read(h))
                                System.Math.Max(System.Threading.Interlocked.Increment(verifiedCount),verifiedCount - 1)
                            Next
                            System.Diagnostics.Trace.WriteLine("Verified handle count: " + verifiedCount)

                        End Using
                    Loop While Not failed AndAlso System.Diagnostics.Debugger.IsAttached

                    [stop].[Set]()
                    workers.Complete(False, 1000)
                    Assert.IsFalse(failed)
                End Using
            End Using
        End Using
    End Using
End Using

Requirements

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

See Also

Reference

WorkQueue<T> Class
WorkQueue<T> Members

Used By

ThreadedBTreeTest.TestErrorsOnInsertAndDelete()
ThreadedBTreeTest.TestConcurrentCreateReadUpdateDelete8000()
Boolean SecureTransfer.Client.Upload(String,Int64,Stream)

Source Code

Library/Threading/WorkQueue.cs

Generated with Document! X 2011 by Innovasys