Thread saftey on aspx codebehind page

Well, it was such a long day that I will just seek input from experts.

Let's say I do this:

public class ZipFiles : Page { Hashtable hs; 

for starters, bearing in mind that I have a member variable in this class.

Then I have lines that I call in OnLoad as:

  ManagedFile mf = new ManagedFile(site); mf.ID = docID; mf.Load(); hs.Add(Path.GetFileName(mf.URL), mf); 

OK, and finally I have a WriteDelegate method from DotNetZip that does this:

  private void WriteEntry(String filename, Stream output) { ManagedFile value = (ManagedFile)hs[filename]; using ( MemoryStream ms = new MemoryStream(value.GetBinary())) { ms.WriteTo(output); } } 

You can see that in OnLoad I put all the mf objects in a hash table named FileName as my key (there will never be two identical file names), and then in WriteDelegate I output them to get Byte [] behind the MF object.

Is this a call to disaster? Will the hash tables of different users on our site mix together and the connections of the universe diverge?

Thanks for the help in advance! It's 5:09 p.m., and I ended the day. Wife makes Biscay chicken fingers tonight for dinner. They are great, ask for a reciepe if you want. Plus she got our 2 boys (6 and 4) at home who had just returned to school for the first time since LAST Monday. It will be 2 tired boys and 1 tired mom when I go through 5:40 today!

+4
source share
2 answers

Each visit page load will recreate the hash table. This is not a recipe for disaster in terms of thread safety, but it is a recipe for disaster in terms of performance. You probably want to create a thread safe singleton to manage this hash table.

+1
source

You should not run into HashTable problems as your HashTable variable HashTable not static. Each page request generates a new instance of your ZipFiles class with its own data.

+4
source

Source: https://habr.com/ru/post/1338780/


All Articles