Error: Deadlock risk detected…..
I got this error “Deadlock risk detected.Trying to read a page from the same thread that is already trying to read the same page” and want to share with you what was my problem.
I can seems that when you first access a page data, and if that page data object make a call to itself (direct or indirect) you will get this error.
I got it in my file selector property and I needed to rewrite the Value method.
The problem was that if you in the file selector added files from the page folder to the page the
System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetDirectory(“/pagefiles/currentPageID/”) this code will make a new call to get the page and make a deadlock.
The trick is to move the code from Value to the SaveData method. So instead of changing the code each time it’s read from the database, only to each time it’s saved.
So the new code for instead of the Value method in the PropertyFiles.cs class is
- public override object SaveData(PropertyDataCollection properties)
- {
- var str = this.LongString;
- if (str != null)
- {
- var doc = new HtmlDocument();
- doc.LoadHtml(str);
- var refLinks = "";
- foreach (var item in doc.DocumentNode.ChildNodes)
- {
- if (item.Name == "div" && item.Attributes.Contains("data-href") && item.Attributes["data-href"].Value != "")
- {
- var href = item.Attributes["data-href"].Value;
- var inner = "";
- refLinks += "<a href=\"" + href + "\" class=\"ignore\" style=\"display:none;\">" + href + "</a>";
- item.Attributes.Remove("style");
- if (href.EndsWith("/"))
- {
- item.Attributes.Add("style", "border:1px solid black;margin-bottom:5px;");
- inner = "<div>";
- inner += "<input type=checkbox onclick=\"$(this).parent().parent().removeAttr('data-href');$(this).parent().parent().css('display','none');var obj=$(this).closest('.ownerDiv').find('.actionField');obj.val('" + href + "');obj.click();\" />delete";
- inner += "</div>";
- var dir = System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetDirectory(href) as UnifiedDirectory;
- if (dir != null)
- {
- inner += "All files from folder:<br />" + dir.VirtualPath;
- foreach (UnifiedFile fil2 in dir.Files)
- {
- if (BaseMedia.IsMedia(fil2.VirtualPath))
- {
- refLinks += "<a href=\"" + fil2.VirtualPath + "\" class=\"ignore\" style=\"display:none;\">" + fil2.VirtualPath + "</a>";
- inner += "<div style=\"float:left;\">";
- inner += "<img style=\"width:80px;height:80px;\" src=\"/FileCache" + fil2.VirtualPath + "/width_80.height_80.mode_crop.jpg\" data-org=\"" + fil2.VirtualPath + "\" title=\"" + fil2.VirtualPath + "\" />";
- inner += "</div>";
- }
- else
- {
- refLinks += "<a href=\"" + fil2.VirtualPath + "\" class=\"ignore\" style=\"display:none;\">" + fil2.VirtualPath + "</a>";
- inner += "<div title=\"" + fil2.VirtualPath + "\">" + fil2.Name + "</div>";
- }
- }
- }
- }
- else
- {
- item.Attributes.Add("style", "border:1px solid black;float:left;margin-right:5px;margin-bottom:5px;");
- var filname = Path.GetFileName(href);
- var path = href.Substring(0, href.Length - filname.Length);
- inner = "<div>";
- inner += "<input type=checkbox onclick=\"$(this).parent().parent().removeAttr('data-href');$(this).parent().parent().css('display','none');var obj=$(this).closest('.ownerDiv').find('.actionField');obj.val('" + href + "');obj.click();\" />delete";
- inner += "<input type=checkbox onclick=\"$(this).parent().parent().attr('data-href','" + path + "');var obj=$(this).closest('.ownerDiv').find('.actionField');$(this).parent().html('All files from folder:<br />" + path + "');obj.val('" + href + "');obj.click();\" />all<br/>";
- item.Attributes.Remove("onclick");
- if (BaseMedia.IsMedia(filname))
- {
- inner += "<img style=\"width:80px;height:80px;\" src=\"/FileCache" + href + "/width_80.height_80.mode_crop.jpg\" data-org=\"" + href + "\" title=\"" + href + "\" />";
- }
- else
- {
- inner += "<div title=\"" + href + "\">" + filname + "</div>";
- }
- inner += "</div>";
- }
- item.InnerHtml = inner + "<div style=\"clear:both;\"></div>";
- }
- }
- return doc.DocumentNode.OuterHtml;
- }
- else
- {
- return this.Value;
- }
- }
Comments