none
Thread safe lists - concept RRS feed

  • Question

  • Hello,

    I have a class stacker, this one has 2 lists in it.

    The user interface fills in the lists, empties them from the process and reduces them by the entries.
    It may happen that the operator fills these, the process reduces this list by one entry.
    How do I get the process-proof realized?

    Thanks for your tips and examples in advance.

    With best regards Markus

    More details for understanding.

    Step 1 fills TopList  
    TopList
       3, 200
       2, 300
       1, 500
       
    Is current list empty I move the top list to current list, the last input, is then the first output.
       
    CurrentList   (working list)
       3, 200
       2, 300
       1, 500   
       
       CurrentList   (working list descrease with 50)
       3, 150
       2, 300
       1, 500   
       
       CurrentList   (working list descrease with 50)
       3, 100
       2, 300
       1, 500   
       
       CurrentList   (working list descrease with 50)
       3, 50
       2, 300
       1, 500   
       
       CurrentList   (working list descrease with 50) if <=0 delete the item.
       2, 300
       1, 500   
       
       Is the current list is empty, the top list has entries move top to current list again.
       
       
       Maybe BlockingCollection<T> ?

    public class Stacker
    {
    	public List<PropertyMaterial> ListTop;
    	public List<PropertyMaterial> ListCurrent;
    
    	public Destacker()
    	{
    		ListTop = new List<PropertyMaterial>();
    		ListCurrent = new List<PropertyMaterial>();
    	}
    
    	public class PropertyMaterial
    	{
    		public string TypeNo { get; set; }
    		public int Quantity { get; set; }
    		
    //....
    //....
    //MainWorkflow.cs
    if (CurrentStacker.ListCurrent.Count > 0)
    {
    	CurrentDestacker.ListCurrent[0].Quantity = CurrentDestacker.ListCurrent[0].Quantity - 50;
    	if (CurrentDestacker.ListCurrent[0].Quantity <= 0)
    		CurrentDestacker.ListCurrent.RemoveAt(0);
    }
    
    
    
    BlockingCollection<T> 	
    ConcurrentDictionary<TKey,TValue> 	
    ConcurrentQueue<T> 	Thread First In, First Out (FIFO)
    ConcurrentStack<T> 	Thread Last In, First Out (LIFO)
    ConcurrentBag<T>
    Picturesample
    What is right here?
    Do I need a thread there?


    Thursday, January 3, 2019 6:39 PM

All replies

  • Hi Markus Freitag,

    According to your description, It seems that you want to create user interface to simulate stack.  if so, I think you don't need a thread, just achieve it via c# logic.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 4, 2019 1:48 AM
    Moderator
  • Hello,

    >I think you don't need a thread, just achieve it via c# logic.

    @Zhanglong, I have created 2 images that represent it more clearly. Maybe you or someone here can create an example.

    2

    view

    I think I need a list that allows two accesses and handles them correctly.

    1 --> Fill by operator

    2 --> Remove it by process

    What happens if it happens at the same time?

    Greetings Markus


    Saturday, January 5, 2019 11:35 AM
  • Hi Markus Freitag,

    Do you want show them in UI framework, such as winforms, WPF? and all data source come from collection?

    Best regards,

    Zhanglong 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 7, 2019 5:56 AM
    Moderator
  • Hello Zhanglong ,

    with WPF the user input occurs, the working thread empties the list.

    Thanks for an example in advance.

    Greetings Markus

    Monday, January 7, 2019 12:35 PM
  • Hi Markus Freitag,

    >>with WPF the user input occurs, the working thread empties the list.

    I would suggest that you could try it yourself, if you encounter any issue during developing, welcome to post here, we will do our best to resolve it.

    For WPF issue, you could also post on WPF forum for suitable support.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wpf

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 8, 2019 3:15 AM
    Moderator
  • Hello Zhanglong,
    >I would suggest that you could try it yourself, if you encounter any issue
    >during developing, welcome to post here, we will do our best to resolve it.
    My problem is
    1 --> Fill by operator
    2 --> Remove it by process
    What happens if it happens at the same time?

    1 must wait until it is safely deleted.
    2 can clear/remove if the operator did not add anything.
    I have to lock the processes together.
    It is difficult to simulate.
    I think it is 99% good, 1% mistakes, the end consumer is dissatisfied.

    My questions are. How can I ensure that which software concept is suitable?
    BlockingCollection<T>  
    It is sufficient if I use a blocking list without lock function. Visual Studio does it all in the background?

    Or do I need both?
    ConcurrentQueue<T>  Thread First In, First Out (FIFO)
    ConcurrentStack<T>  Thread Last In, First Out (LIFO)
    The bottom list is LiFo
    Will be filled from the top list.
    The top list is like a buffer list.

    ----------------------

    Hello experts, do you have a solution for me?

    ---------------------

    Greetings Markus

    Saturday, January 12, 2019 11:25 AM
  • Hi Markus Freitag,

    Sorry, I am not sure what's your requirement, do you want to create an application to simulate stack, or the usage of BlockingCollection<T>, or others? could you please describe it in detailed.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 16, 2019 6:56 AM
    Moderator