public ActionResult LoadCache() { System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly(); DirectoryInfo d = Directory.GetParent(Directory.GetParent(a.CodeBase.ToString().Substring(8)).ToString()); string[] fileEntries = Directory.GetFiles(d.ToString() + @"/Content", "*.jpg"); var files = from f in fileEntries where f.EndsWith(".jpg") || f.EndsWith(".png") select f; ViewBag.Images = new ArrayList(); for (int j = 0; j < 10; j++) { foreach (var fileName in files) { AsyncWorker l = new AsyncWorker(); String contents = (String)HttpRuntime.Cache.Get(fileName); // Check if file contents exist in cache if (contents == null) { contents = l.LoadImageAsync((string)fileName); // Insert contents into cache, specifying a 1 minute timeout policy HttpRuntime.Cache.Insert(fileName, contents, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 1, 0)); } // Place image contents in an array of images in viewbag ViewBag.Images.Add("data:image/jpeg;base64," + contents); } } return View("Media"); }
/// <summary> /// Loads images from database using asynchronous request processing. /// </summary> public void GetImagesAsync() { AsyncManager.Timeout = 10000; ViewBag.Images = new ArrayList(); ViewBag.Tables = new ArrayList(); DatabaseWorker.connectDB(); DatabaseWorker dw = new DatabaseWorker(); for (int j = 0; j < 10; j++) { int count = dw.intQueryDB("select count(*) from Images"); for (int i = 1; i <= count; i++) { AsyncManager.OutstandingOperations.Increment(); AsyncWorker l = new AsyncWorker(); // Register the event handler to be notified when the async operation finishes l.LoadDataCompleted += (sender, e) => { // Place image contents in an array of images in viewbag ViewBag.Images.Add("data:image/jpeg;base64," + @e.ImageContents); AsyncManager.OutstandingOperations.Decrement(); }; // Execute async method (automatically on separate thread) l.LoadDBImageAsync("select image from Images where imageID = " + i + " and exists (select * from ArticleAttachments where ImageID = " + i + ")"); } } DatabaseWorker.disconnectDB(); }
/// <summary> /// Loads all JPG and PNG images and sends them to the view synchronously. /// </summary> /// <returns>View of images</returns> public ActionResult LoadMediaSynchronous() { System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly(); DirectoryInfo d = Directory.GetParent(Directory.GetParent(a.CodeBase.ToString().Substring(8)).ToString()); string[] fileEntries = Directory.GetFiles(d.ToString() + @"/Content", "*.jpg"); var files = from f in fileEntries where f.EndsWith(".jpg") || f.EndsWith(".png") select f; ViewBag.Images = new ArrayList(); for (int j = 0; j < 10; j++) { foreach (var fileName in files) { AsyncWorker l = new AsyncWorker(); String contents = l.LoadImageAsync((string)fileName); // Place image contents in an array of images in viewbag ViewBag.Images.Add("data:image/jpeg;base64," + contents); } } return View("Media"); }
/// <summary> /// Loads all images using the AsyncWorker class - will not return until the number of outstanding operations /// is zero. /// </summary> public void MediaAsync() { System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly(); DirectoryInfo d = Directory.GetParent(Directory.GetParent(a.CodeBase.ToString().Substring(8)).ToString()); string[] fileEntries = Directory.GetFiles(d.ToString() + @"/Content", "*.jpg"); var files = from f in fileEntries where f.EndsWith(".jpg") || f.EndsWith(".png") select f; AsyncManager.Timeout = 10000; ViewBag.Images = new ArrayList(); for (int j = 0; j < 10; j++) { foreach (var fileName in files) { AsyncManager.OutstandingOperations.Increment(); AsyncWorker l = new AsyncWorker(); l.LoadDataCompleted += (sender, e) => { // Place image contents in an array of images in viewbag ViewBag.Images.Add("data:image/jpeg;base64," + @e.ImageContents); AsyncManager.OutstandingOperations.Decrement(); }; l.LoadImageAsync((string)fileName); } } }
/// <summary> /// Loads images from database synchronously. /// </summary> /// <returns>View of textual tables</returns> public ActionResult GetTextSynchronous() { DatabaseWorker dw = new DatabaseWorker(); DatabaseWorker.connectDB(); ViewBag.Tables = new ArrayList(); ViewBag.Images = new ArrayList(); String[] tableNames = { "Technology", "Music", "Nature", "Health", "Sports", "Business" }; for (int j = 0; j < tableNames.Length; j++) { AsyncWorker aw = new AsyncWorker(); DataTable table = aw.LoadDBTextAsync("select DocumentTitle as Title, Username, FirstName, LastName " + "from Articles, Groups, SiteUsers where Articles.GroupID=Groups.GroupID and SiteUsers.UserID=Articles.UserID " + "and GroupName = '" + tableNames[j] + "' order by FirstName, LastName ASC"); table.TableName = tableNames[j] + " Articles:"; ViewBag.Tables.Add(table); } DatabaseWorker.disconnectDB(); ViewBag.Title = "Synchronous Test Loading Text From Database"; return View("RegularQueries"); }
public ActionResult GetTextCache() { DatabaseWorker dw = new DatabaseWorker(); DatabaseWorker.connectDB(); ViewBag.Tables = new ArrayList(); ViewBag.Images = new ArrayList(); String[] tableNames = { "Technology", "Music", "Nature", "Health", "Sports", "Business" }; for (int j = 0; j < tableNames.Length; j++) { AsyncWorker aw = new AsyncWorker(); DataTable table; object t = HttpRuntime.Cache.Get(tableNames[j]); if (t == null) { table = aw.LoadDBTextAsync("select DocumentTitle as Title, Username, FirstName, LastName from Articles, Groups, SiteUsers " + "where Articles.GroupID=Groups.GroupID and SiteUsers.UserID=Articles.UserID and GroupName = '"+tableNames[j]+"'" + " order by FirstName, LastName ASC"); table.TableName = tableNames[j] + " Articles:"; HttpRuntime.Cache.Insert(tableNames[j], table, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 1, 0)); } else { table = (DataTable)t; } ViewBag.Tables.Add(table); } DatabaseWorker.disconnectDB(); ViewBag.Title = "Caching Test Loading Text From Database"; return View("RegularQueries"); }
/// <summary> /// Loads images from database using asynchronous method calling. /// </summary> public void GetTextAsync() { AsyncManager.Timeout = 10000; ViewBag.Images = new ArrayList(); DatabaseWorker.connectDB(); DatabaseWorker dw = new DatabaseWorker(); ViewBag.Tables = new ArrayList(); String[] tableNames = { "Technology", "Music", "Nature", "Health", "Sports", "Business" }; for (int j = 0; j < tableNames.Length; j++) { AsyncManager.OutstandingOperations.Increment(); AsyncWorker l = new AsyncWorker(); l.LoadDataCompleted += (sender, e) => { e.Data.TableName = tableNames[j] +" Articles:"; ViewBag.Tables.Add(e.Data); AsyncManager.OutstandingOperations.Decrement(); }; l.LoadDBTextAsync("select DocumentTitle as Title, Username, FirstName, LastName from Articles, Groups, SiteUsers " + "where Articles.GroupID=Groups.GroupID and SiteUsers.UserID=Articles.UserID and GroupName = '"+tableNames[j]+"'" + " order by FirstName, LastName ASC"); } DatabaseWorker.disconnectDB(); }