Click here to Skip to main content
15,887,820 members
Home / Discussions / C#
   

C#

 
Questionusing sData in C# to post sales Pin
Jassim Rahma7-Jan-15 20:33
Jassim Rahma7-Jan-15 20:33 
AnswerRe: using sData in C# to post sales Pin
Mycroft Holmes7-Jan-15 21:02
professionalMycroft Holmes7-Jan-15 21:02 
AnswerRe: using sData in C# to post sales Pin
Richard MacCutchan7-Jan-15 22:51
mveRichard MacCutchan7-Jan-15 22:51 
QuestionABOUT C# PROJECT Pin
bhulku Swati7-Jan-15 16:33
bhulku Swati7-Jan-15 16:33 
AnswerRe: ABOUT C# PROJECT Pin
BillWoodruff7-Jan-15 16:55
professionalBillWoodruff7-Jan-15 16:55 
AnswerRe: ABOUT C# PROJECT Pin
Pete O'Hanlon7-Jan-15 20:29
mvePete O'Hanlon7-Jan-15 20:29 
AnswerRe: ABOUT C# PROJECT Pin
Swinkaran8-Jan-15 10:12
professionalSwinkaran8-Jan-15 10:12 
QuestionWhy is happening this error: Object synchronization method was called from an unsynchronized block of code. ? Pin
FANMixco7-Jan-15 8:38
FANMixco7-Jan-15 8:38 
I'm developing a Smart Array (it's a request I cannot use a List of int that I know it's easier because I made both codes). I have done this class and below is the example of how I use it. The error is often in this line (153 from class):

// Ensure that the lock is released.
Monitor.Exit(array);


If I use a List nothing wrong happens just when I translate to an array. Thanks for your help.

SmartArray3.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SmartArray
{
    class SmartArray3
    {
        private int[] array;
        private int size = 0;
        private int count = 0;

        public SmartArray3()
        {
            Resize(1);
        }

        public SmartArray3(int size)
        {
            this.size = size;
            array = new int[this.size];
        }

        public bool Resize(int size)
        {
            try
            {
                if (array == null)
                    array = new int[size];
                else
                    Array.Resize(ref array, size);

                this.size++;
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void add(int value)
        {
            try
            {
                if (array == null)
                {
                    this.size = 1;
                    Resize(this.size);

                    array[0] = value;
                    this.count++;
                }
                else
                {
                    if (this.count == (this.size - 1))
                    {
                        this.size *= 2;
                        this.Resize(this.size);
                    }

                    if ((this.count - 1) < 0)
                        array[0] = value;
                    else
                        array[this.count - 1] = value;
                    this.count++;
                }
            }

            catch (Exception ex)
            {
                Console.Write(ex.ToString());
                throw new System.IndexOutOfRangeException("Index out of Range.");
            }

        }

        // Lock the array and add an element. 
        public void Add(int value)
        {
            // Request the lock, and block until it is obtained.
            Monitor.Enter(array);
            try
            {
                if (array == null)
                {
                    this.size = 1;
                    Resize(this.size);

                    array[0] = value;
                    this.count++;
                }
                else
                {
                    if (this.count == (this.size - 1))
                    {
                        this.size *= 2;
                        this.Resize(this.size);
                    }

                    if ((this.count - 1) < 0)
                        array[0] = value;
                    else
                        array[this.count - 1] = value;
                    this.count++;
                }
            }
            finally
            {
                // Ensure that the lock is released.
                Monitor.Exit(array);
            }
        }

        // Try to add an element to the List: Add the element to the List  
        // only if the lock is immediately available. 
        public bool TryAdd(int value)
        {
            // Request the lock. 
            if (Monitor.TryEnter(array))
            {
                try
                {
                    if (array == null)
                    {
                        this.size = 1;
                        Resize(this.size);

                        array[0] = value;
                        this.count++;
                    }
                    else
                    {
                        if (this.count == (this.size - 1))
                        {
                            this.size *= 2;
                            this.Resize(this.size);
                        }

                        if ((this.count - 1) < 0)
                            array[0] = value;
                        else
                            array[this.count - 1] = value;
                        this.count++;
                    }
                }
                finally
                {
                    // Ensure that the lock is released.
                    Monitor.Exit(array);
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        public int Get(int index)
        {
            try
            {
                return array[index];
            }
            catch (IndexOutOfRangeException ex)
            {
                throw new System.IndexOutOfRangeException("Index out of range");
            }
        }
    }
}


Code for called the Class:

C#
private static int threadsRunning = 0;
        private SmartArray3 sa = new SmartArray3();
        private List times;

        private static string[] titles ={
                                "Add                            ", "Add failed                     ", "TryAdd succeeded               ", "TryAdd failed                  "};
        private static int[][] results = new int[3][];

        //Event to Create Threads
        private void newTest()
        {
            for (int i = 0; i < 3; i++)
            {
                Thread t = new Thread(ThreadProc);
                t.Start(i);
                Interlocked.Increment(ref threadsRunning);
            }
        }

        private void ThreadProc(object state)
        {
            times = new List();
            DateTime finish = DateTime.Now.AddSeconds(10);
            Random rand = new Random();
            int[] result = { 0, 0, 0, 0};
            int threadNum = (int)state;

            while (DateTime.Now < finish)
            {
                Stopwatch sw = Stopwatch.StartNew();
                int what = rand.Next(250);
                int how = rand.Next(25);

                if (how < 16)
                {
                    try
                    {
                        sa.Add(what);
                        result[(int)ThreadResultIndex.AddCt] += 1;
                        times.Add(sw.Elapsed.TotalMilliseconds);
                    }
                    catch
                    {
                        result[(int)ThreadResultIndex.AddFailCt] += 1;
                    }
                }
                else
                {
                    if (sa.TryAdd(what))
                    {
                        result[(int)ThreadResultIndex.TryAddSucceedCt] += 1;
                    }
                    else
                    {
                        result[(int)ThreadResultIndex.TryAddFailCt] += 1;
                    }
                }
                sw.Stop();
            }
            results[threadNum] = result;

            if (0 == Interlocked.Decrement(ref threadsRunning))
            {
                StringBuilder sb = new StringBuilder(
                   "                               Thread 1 Thread 2 Thread 3 Total\n");

                for (int row = 0; row < 4; row++)
                {
                    int total = 0;
                    sb.Append(titles[row]);

                    for (int col = 0; col < 3; col++)
                    {
                        sb.Append(String.Format("{0,4} ", results[col][row]));
                        total += results[col][row];
                    }

                    sb.AppendLine(String.Format("{0,4} ", total));
                }

                Console.WriteLine(sb.ToString());
            }
        }

        private enum ThreadResultIndex
        {
            AddCt,
            AddFailCt,
            TryAddSucceedCt,
            TryAddFailCt
        }

Federico Navarrete

AnswerRe: Why is happening this error: Object synchronization method was called from an unsynchronized block of code. ? PinPopular
Richard Deeming7-Jan-15 8:57
mveRichard Deeming7-Jan-15 8:57 
GeneralRe: Why is happening this error: Object synchronization method was called from an unsynchronized block of code. ? Pin
OriginalGriff7-Jan-15 8:59
mveOriginalGriff7-Jan-15 8:59 
QuestionI'm looking for instructions to convert a demo project into a class Pin
turbosupramk37-Jan-15 5:00
turbosupramk37-Jan-15 5:00 
AnswerRe: I'm looking for instructions to convert a demo project into a class Pin
OriginalGriff7-Jan-15 5:23
mveOriginalGriff7-Jan-15 5:23 
GeneralRe: I'm looking for instructions to convert a demo project into a class Pin
turbosupramk37-Jan-15 8:00
turbosupramk37-Jan-15 8:00 
GeneralRe: I'm looking for instructions to convert a demo project into a class Pin
OriginalGriff7-Jan-15 8:27
mveOriginalGriff7-Jan-15 8:27 
GeneralRe: I'm looking for instructions to convert a demo project into a class Pin
turbosupramk37-Jan-15 8:49
turbosupramk37-Jan-15 8:49 
GeneralRe: I'm looking for instructions to convert a demo project into a class Pin
OriginalGriff7-Jan-15 8:58
mveOriginalGriff7-Jan-15 8:58 
GeneralRe: I'm looking for instructions to convert a demo project into a class Pin
turbosupramk37-Jan-15 9:12
turbosupramk37-Jan-15 9:12 
GeneralRe: I'm looking for instructions to convert a demo project into a class Pin
OriginalGriff7-Jan-15 10:09
mveOriginalGriff7-Jan-15 10:09 
AnswerRe: I'm looking for instructions to convert a demo project into a class Pin
V.7-Jan-15 10:35
professionalV.7-Jan-15 10:35 
GeneralRe: I'm looking for instructions to convert a demo project into a class Pin
Mycroft Holmes7-Jan-15 13:42
professionalMycroft Holmes7-Jan-15 13:42 
GeneralRe: I'm looking for instructions to convert a demo project into a class Pin
turbosupramk38-Jan-15 4:58
turbosupramk38-Jan-15 4:58 
GeneralRe: I'm looking for instructions to convert a demo project into a class Pin
V.8-Jan-15 10:04
professionalV.8-Jan-15 10:04 
QuestionBinding one to many (bindingsource / datasource ) Pin
rudo327-Jan-15 4:52
rudo327-Jan-15 4:52 
Questionenums Pin
Gilbert Consellado6-Jan-15 21:21
professionalGilbert Consellado6-Jan-15 21:21 
AnswerRe: enums Pin
V.6-Jan-15 21:46
professionalV.6-Jan-15 21:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.