C# - Static Methods Updating A Dictionary<T,U> Is It Safe To Lock() On The Dictionary Itself
Dec 23, 2010
I have a class that maintains a static dictionary of cached lookup results from my domain controller - users' given names and e-mails.My code looks something like:
private static Dictionary<string, string> emailCache = new Dictionary<string, string>();
protected string GetUserEmail(string accountName)
{
if (emailCache.ContainsKey(accountName))
{
return(emailCache[accountName]);
}
lock(/* something */)
{
if (emailCache.ContainsKey(accountName))
[code]...
I have a static class that holds a dictionary of objects. I have multiple threads that access this dictionary and create objects on it that are specific to that thread. How and when can I remove entries from the dictionary when a thread is "done"?
Basically this is a static class for an ASP.NET application that all requests can have "isolated objects" (that are related to the current thread) created in the dictionary. But, I don't really want to be tied to ASP.NET.
I need an advice on piece of functionality that I am ought to implement. The scenario is that we haven an HttpHandler which servers to intercept file uploads. In the handler, I need to persist a large dictionary of strings inside the memory. The dictionary might be as large as 100 entries. I am wondering whether it is safe to store that in a static variable, so that it is not initialized every time instance of the handler is created (there will be a lot of instance for sure). In general, what is the approach in such scenarios. Is it a generally better idea to use static fields, to persist data that will not be changed?
I have a private static field in my Controller class in an MVC web application.
I have a static method in that controller that assigns some value to that static field, I want to apply lock on that static field until some other instance method in the controller uses the value stored in the static field and then releases it.
DETAILS:
I have a controller named BaseController having a static ClientId field as follows and two methods as follows:-
public static string ClientId = ""; static void OnClientConnected(string clientId, ref Dictionary<string, object> list) { list.Add("a", "b"); // I want the ClientId to be locked here, so that it can not be accessed by other requests coming to the server and wait for ClientId to be released:- BaseController.clientId = clientId; } public ActionResult Handler() { if (something) { // use the static ClientId here } // Release the ClientId here, so it can now be used by other web requests coming to the server. return View(); }
How do you sort a dictionary object in C# 2.0 for asp.net or is their an alternative to Dictionay for sorting this is to sort a countries list alphabetically
I end up with a row for each object in the list, and any cell in a given row is bound to a property of the corresponding object.
However, suppose one of the properties of my object is a dictionary, and each is expected to contain a specific key. Is there any way to bind one of my DataGridColumns to that dictionary key?
I'm about to begin an ASP.NET MVC project and I'm not sure how to approach an aspect of the design. Basically, there is a user site and an admin site. In the admin sites, administartors design a form and send an e-mail link out to a handful of people. When the users click on the link, they are sent to the form.
Essentially what I'm wondering is what are the best practices when the model resembles looks more like a dictionary than a table?
can anyone explain what this method does exactly. Here what I can tell.ine 1 a dictionary object is createdline 2. it iterate through the Datarowline 3 . it adds first column name and value of the first row to other dictionary object fsline 4 & 5 here where I got lost, do not know what the code is ment to do , please explain also, can you go over the first two iteration word by word so i can follow up with you
protected Dictionary<string, object> fields(DataRow dr) { 1 Dictionary<string, object> fs = new Dictionary<string, object>(); 2 for (int i = 0; i < dr.ItemArray.Length; i++) [code]...
how to do routing. My problem basically relates to "logged in" and "logged out routes" and having both as "/".
i.e. I have home/index for logged out user which appears as "/" but this has got me confused as to how I can have home/home for logged in user and still have "/".
I keep getting 127.0.0.1/home/home
I could modify like 127.0.0.1/home - but I want it like "/". My confusion relates to the fact that the "/" [127.0.0.1/] is bound in the routes collection to home/index.
how I can modify the routes dictionary (which will be binded ONCE at the start) so that the "/" can be shared for logged and non-logged users ?
I've got a little problem with displaying my Dictionary in ListBox.
public static Dictionary<String, List<String>> MyDict = new Dictionary<string, List<String>>(); ... if(MyDict[value1] == null){ List<String> Temp_List = new List<String>(); Temp_List.Items.Add(sth); MyDict[value1] = Temp_List; } else if(MyDict[value1].Count < 4){ List<String> Temp_List = new List<String>(); Temp_List = MyDict[value1]; Temp_List.Add(sth); MyDict[value1] = Temp_List; }
and I'd like to display in Page_Load (after logging) a ListBox which will have List from MyDict for this login. Eg. Login is : sally And list contains of: she, no, then And after sally logging I'd like to see result like this:
This binds the properties in myObject to the gridview, but I'd like to get the key as well. If it's not possible, is there an alternative, e.g. use a repeater?
this code i wrote var sortedDict = _GrandTotalUserDictionary.OrderBy(key => key.Value.Priority); the dictionary looks like "key1", new OrderItem(){title:'a', priority:1} "key2", new OrderItem(){title:'b', priority:3} "key3", new OrderItem(){title:'c', priority:12}
How do you create a dictionary of objects in the session? More specifically, I have a list of objects: MyList stores MyObject as the result of a linq query with the date as a parameter.
Now I'd like to store MyList in the session object in a dictionary with the date as the key. When the page needs a MyList for a specific date, first search the dictionary and if it's blank for that date, get the data from the GetObjects query and store the result in the dictionary in the session.
I have to put data into a dictinary, thus formed: Dictionary (Of Integer, List (Of RicercaNews))
but when I go to put the list in the dictionary, I always return the same record, as if it does not increase the index of the list.
This is the code:
ResultTable Dim As New DataTable () Dictgestionalews Dim As New Dictionary (Of Integer, List (Of RicercaNews)) Listacat Dim As New Dictionary (Of Integer, String) Dim listaricerca As List (Of RicercaNews)