Click here to Skip to main content
15,886,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There is a somoe sort of multi-key dictionary that I can safely use with multiple Tasks?
I mean that I need a Dictionary with 2 keys threadsafe that I can use in multi task program.
Something like that:
C#
var dictionary = new MultiDictionary<int key1, int key2, string value>();

Task[] tasks = new Task[2];

            tasks[0] = Task.Run(() =>
            {
                for (int i = 0; i < 2; i++)
                {
                    dictionary.Add(i,i+1, "value: "+i); 
                }
            });

            tasks[1] = Task.Run(() =>
            {
                for (int i = 2; i < data.Length; i++)
                {
                    if (dictionary.ContainsKeys(i,i+1)
                        Console.WriteLine("Value is:"dictionary.GetValue(i,i+1));
                    else
                        Console.WriteLine(No value);
                }
            });

            
            Task.WaitAll(tasks);


Thank's
Posted
Updated 5-May-13 1:32am
v2
Comments
Sandeep Mewara 5-May-13 7:03am    
This is not a well framed question! We cannot work out what you are trying to do/ask from the post. Please elaborate and be specific.
Use the "Improve question" link to edit your question and provide better information.

If you have to supply both keys to get your value then you are using 1 key so you can "concat" (somehow combine 2 key in the case of ints you can create a long for example etc.) the two keys and use a normal Dictionary.
 
Share this answer
 
Comments
boris01 5-May-13 23:13pm    
Thank you Mehdi for the answer, but normal Dictionary is not thread/task safe as I need.
I found this solution http://www.codeproject.com/Articles/32894/C-Multi-key-Generic-Dictionary
but I'm not sure if it 100% thread/task safe.
Thank you!
Since your question is really about the implementation of the MultiDictionary as described in: C# Multi-key Generic Dictionary[^] you should ask in the questions section in that article.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900