none
Get Unique Ids from service and use it as batch RRS feed

  • Question

  • Hi,

    I have  a requirement to get the unique ids

    I do get it from a service as 1000 at a time. Example: GetIds(1000) - I get it from 1-1000. For next call GetIds(1000) I get it as 1001 to 2000

    I want to user these 1000 Ids as a batch in my c# code once I reach to 999 I want to call GetIds to get my next bacth.

    Create batch for 1000 everytime, use all ids and call the service after every 1000.

    How can I do this as batch operation?

    Saturday, February 1, 2020 8:19 PM

Answers

  • Hello,

    As per your last question I answered you can do this. Here is a pattern to work from. In DoSomethingsWithIdentifier you would with the current int value. Note I take the List and use it as an array. Of course rather than returning a list from the singleton class you could return an array instead.

    private int[] _uniqueIdentifiers;
    private void Form1_Shown(object sender, EventArgs e)
    {
        _uniqueIdentifiers = ServiceIncrementer.Instance.GetIds(1000).ToArray();
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
    
        while ( _uniqueIdentifiers.Length >0)
        {
            var currentId = _uniqueIdentifiers[0];
            _uniqueIdentifiers = _uniqueIdentifiers.Skip(1).ToArray();
    
            DoSomethingWithIdentifier(currentId);
    
            if (_uniqueIdentifiers.Length == 0)
            {
                MessageBox.Show(currentId.ToString());
                _uniqueIdentifiers = ServiceIncrementer.Instance.GetIds(1000).ToArray();
            }
        }
    }
    
    private void DoSomethingWithIdentifier(int id)
    {
        // use the id for whatever
        Console.WriteLine(id);
    }
    }

    In this version an array of int is returned rather than a list.

    private int[] _uniqueIdentifiers;
    private void Form1_Shown(object sender, EventArgs e)
    {
        _uniqueIdentifiers = ServiceIncrementer.Instance.GetIds(1000);
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
    
        while (_uniqueIdentifiers.Length > 0)
        {
            var currentId = _uniqueIdentifiers[0];
            _uniqueIdentifiers = _uniqueIdentifiers.Skip(1).ToArray();
    
            DoSomethingWithIdentifier(currentId);
    
            if (_uniqueIdentifiers.Length == 0)
            {
                MessageBox.Show(currentId.ToString());
                _uniqueIdentifiers = ServiceIncrementer.Instance.GetIds(1000).ToArray();
            }
        }
    }
    
    private void DoSomethingWithIdentifier(int id)
    {
        // use the id for whatever
        Console.WriteLine(id);
    }

    .

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace ContinueWith1
    {
        public sealed class ServiceIncrementer
        {
            private static readonly Lazy<ServiceIncrementer> Lazy =
                new Lazy<ServiceIncrementer>(() =>
                    new ServiceIncrementer());
    
            public static ServiceIncrementer Instance => Lazy.Value;
            /// <summary>
            /// Max before reset
            /// </summary>
            private readonly int _maxValue = 20000;
            private List<int> _baseList = new List<int>();
            private void CreateList()
            {
                _baseList = new List<int>();
    
                for (var index = 0; index < _maxValue; index++)
                {
                    _baseList.Add(index);
                }
            }
            public int[] GetIds(int size)
            {
                if (!_baseList.Any())
                {
                    CreateList();
                }
    
                var values = _baseList.Take(size).ToArray();
                _baseList = _baseList.Skip(size).ToList();
                return values;
            }
    
            /// <summary>
            /// Instantiate List
            /// </summary>
            private ServiceIncrementer()
            {
                CreateList();
            }
            /// <summary>
            /// Manual reset
            /// </summary>
            public void Reset()
            {
                CreateList();
            }
        }
    }
    If neither is suited to your task then use the above and modify the code to do so.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Krishna90 Wednesday, February 5, 2020 3:51 AM
    Tuesday, February 4, 2020 2:24 PM
    Moderator

All replies

  • It depends how long you need to hold on to the values, for the life time the app is open or if the values are needed between running the app.

    You could populate an int array then check it's length, if the length of the array is not 0 get a values then get rid of the first value.

    So get a value then get rid of the first value as follows were batchArray contains 1000 identifiers.

    batchArray = batchArray.Skip(1).ToArray(); 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, February 1, 2020 9:21 PM
    Moderator
  • Thank you. I Want to hold it for the life time of the app.
    Saturday, February 1, 2020 10:37 PM
  • Sorry for the late reply.

    Setup a form level array

    private int[] _uniqueIdentifiers;

    Then in a button click event use the following and replace Service.GetIds with the method you have.

    if (_uniqueIdentifiers == null || _uniqueIdentifiers.Length == 0)
    {
        _uniqueIdentifiers = Service.GetIds(1000);
    }
    var currentId = _uniqueIdentifiers[0];
    _uniqueIdentifiers = _uniqueIdentifiers.Skip(1).ToArray();


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Krishna90 Tuesday, February 4, 2020 10:57 AM
    • Unmarked as answer by Krishna90 Tuesday, February 4, 2020 1:04 PM
    Sunday, February 2, 2020 1:02 AM
    Moderator
  • Thank you for your reply.

    Can we do this using arthemetic operations using 
    current = the current id 
    size =1000

    from GetIds()- I get from 0(if current id 0) to 1000

    increment current id until it reaches 999 and get the next set

    if you have idea about this let me know
    Tuesday, February 4, 2020 12:29 PM
  • Try the following out.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace YourNamespaceGoesHere
    {
        public sealed class ServiceIncrementer
        {
            private static readonly Lazy<ServiceIncrementer> Lazy =
                new Lazy<ServiceIncrementer>(() =>
                    new ServiceIncrementer());
    
            public static ServiceIncrementer Instance => Lazy.Value;
            /// <summary>
            /// Max before reset
            /// </summary>
            private readonly int _maxValue = 20000;
            private List<int> _baseList = new List<int>();
            private void CreateList()
            {
                _baseList = new List<int>();
    
                for (var index = 0; index < _maxValue; index++)
                {
                    _baseList.Add(index);
                }
            }
            public List<int> GetIds(int size)
            {
                if (!_baseList.Any())
                {
                    CreateList();
                }
    
                var values = _baseList.Take(size);
                _baseList = _baseList.Skip(size).ToList();
                return values.ToList();
            }
    
            /// <summary>
            /// Instantiate List
            /// </summary>
            private ServiceIncrementer()
            {
                CreateList();
            }
            /// <summary>
            /// Manual reset
            /// </summary>
            public void Reset()
            {
                CreateList();
            }
        }
    }

    Usage

    List<int> result = ServiceIncrementer.Instance.GetIds(1000);



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, February 4, 2020 1:05 PM
    Moderator
  • Thank you for your quick reply.. It’s worked fine with the lists, but didn’t want to use lists instead replace it by variables and use few calculations to get this working Like current value reaches max value get the next set like this
    Tuesday, February 4, 2020 1:16 PM
  • Hello,

    As per your last question I answered you can do this. Here is a pattern to work from. In DoSomethingsWithIdentifier you would with the current int value. Note I take the List and use it as an array. Of course rather than returning a list from the singleton class you could return an array instead.

    private int[] _uniqueIdentifiers;
    private void Form1_Shown(object sender, EventArgs e)
    {
        _uniqueIdentifiers = ServiceIncrementer.Instance.GetIds(1000).ToArray();
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
    
        while ( _uniqueIdentifiers.Length >0)
        {
            var currentId = _uniqueIdentifiers[0];
            _uniqueIdentifiers = _uniqueIdentifiers.Skip(1).ToArray();
    
            DoSomethingWithIdentifier(currentId);
    
            if (_uniqueIdentifiers.Length == 0)
            {
                MessageBox.Show(currentId.ToString());
                _uniqueIdentifiers = ServiceIncrementer.Instance.GetIds(1000).ToArray();
            }
        }
    }
    
    private void DoSomethingWithIdentifier(int id)
    {
        // use the id for whatever
        Console.WriteLine(id);
    }
    }

    In this version an array of int is returned rather than a list.

    private int[] _uniqueIdentifiers;
    private void Form1_Shown(object sender, EventArgs e)
    {
        _uniqueIdentifiers = ServiceIncrementer.Instance.GetIds(1000);
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
    
        while (_uniqueIdentifiers.Length > 0)
        {
            var currentId = _uniqueIdentifiers[0];
            _uniqueIdentifiers = _uniqueIdentifiers.Skip(1).ToArray();
    
            DoSomethingWithIdentifier(currentId);
    
            if (_uniqueIdentifiers.Length == 0)
            {
                MessageBox.Show(currentId.ToString());
                _uniqueIdentifiers = ServiceIncrementer.Instance.GetIds(1000).ToArray();
            }
        }
    }
    
    private void DoSomethingWithIdentifier(int id)
    {
        // use the id for whatever
        Console.WriteLine(id);
    }

    .

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace ContinueWith1
    {
        public sealed class ServiceIncrementer
        {
            private static readonly Lazy<ServiceIncrementer> Lazy =
                new Lazy<ServiceIncrementer>(() =>
                    new ServiceIncrementer());
    
            public static ServiceIncrementer Instance => Lazy.Value;
            /// <summary>
            /// Max before reset
            /// </summary>
            private readonly int _maxValue = 20000;
            private List<int> _baseList = new List<int>();
            private void CreateList()
            {
                _baseList = new List<int>();
    
                for (var index = 0; index < _maxValue; index++)
                {
                    _baseList.Add(index);
                }
            }
            public int[] GetIds(int size)
            {
                if (!_baseList.Any())
                {
                    CreateList();
                }
    
                var values = _baseList.Take(size).ToArray();
                _baseList = _baseList.Skip(size).ToList();
                return values;
            }
    
            /// <summary>
            /// Instantiate List
            /// </summary>
            private ServiceIncrementer()
            {
                CreateList();
            }
            /// <summary>
            /// Manual reset
            /// </summary>
            public void Reset()
            {
                CreateList();
            }
        }
    }
    If neither is suited to your task then use the above and modify the code to do so.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Krishna90 Wednesday, February 5, 2020 3:51 AM
    Tuesday, February 4, 2020 2:24 PM
    Moderator
  • Based on a queue implement your own class. Get the first batch of UID's when it is initialized. In the your custom UID getter method trigger a threshold test. When a certain limit is reached get the next batch in the background.

    Depending on the use-case, you need to consider using a concurrent queue in you implementation and probably using async/await.

    Tuesday, February 4, 2020 2:30 PM