Singleton Pattern
-
Hi, I want to open only one PDF file using singleton pattern.I have written a code as follows on button click event:- FileInfo objFile; string strFileName; strFileName = "User Manual - Quality Feedback System.pdf"; objFile = new FileInfo(Server.MapPath(ConfigurationSettings.AppSettings["App_Name"]) + "\\" + strFileName); Response.Clear(); Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition", "attachment; filename=" + objFile.Name); Response.Flush(); Response.WriteFile(objFile.FullName); Response.End(); The problem with the above code is that it opens more than one PDF file if button is clicked more than once.How can I make sure that one PDF file gets opened using Singleton design pattern.I am clear with the concept of singleton pattern but I am not able to implement this concept in my problem.Please help.
-
Hi, I want to open only one PDF file using singleton pattern.I have written a code as follows on button click event:- FileInfo objFile; string strFileName; strFileName = "User Manual - Quality Feedback System.pdf"; objFile = new FileInfo(Server.MapPath(ConfigurationSettings.AppSettings["App_Name"]) + "\\" + strFileName); Response.Clear(); Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition", "attachment; filename=" + objFile.Name); Response.Flush(); Response.WriteFile(objFile.FullName); Response.End(); The problem with the above code is that it opens more than one PDF file if button is clicked more than once.How can I make sure that one PDF file gets opened using Singleton design pattern.I am clear with the concept of singleton pattern but I am not able to implement this concept in my problem.Please help.
are you loading the pdf file where the button exists?
Regards, Sylvester G If we don't succeed, we run the risk of failure
-
Hi, I want to open only one PDF file using singleton pattern.I have written a code as follows on button click event:- FileInfo objFile; string strFileName; strFileName = "User Manual - Quality Feedback System.pdf"; objFile = new FileInfo(Server.MapPath(ConfigurationSettings.AppSettings["App_Name"]) + "\\" + strFileName); Response.Clear(); Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition", "attachment; filename=" + objFile.Name); Response.Flush(); Response.WriteFile(objFile.FullName); Response.End(); The problem with the above code is that it opens more than one PDF file if button is clicked more than once.How can I make sure that one PDF file gets opened using Singleton design pattern.I am clear with the concept of singleton pattern but I am not able to implement this concept in my problem.Please help.
Pranav Thakur wrote:
The problem with the above code is that it opens more than one PDF file if button is clicked more than once.How can I make sure that one PDF file gets opened
Disable the button so that it can't be clicked again?
Pranav Thakur wrote:
I am clear with the concept of singleton pattern
That seems unlikely given your post.
led mike
-
Hi, I want to open only one PDF file using singleton pattern.I have written a code as follows on button click event:- FileInfo objFile; string strFileName; strFileName = "User Manual - Quality Feedback System.pdf"; objFile = new FileInfo(Server.MapPath(ConfigurationSettings.AppSettings["App_Name"]) + "\\" + strFileName); Response.Clear(); Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition", "attachment; filename=" + objFile.Name); Response.Flush(); Response.WriteFile(objFile.FullName); Response.End(); The problem with the above code is that it opens more than one PDF file if button is clicked more than once.How can I make sure that one PDF file gets opened using Singleton design pattern.I am clear with the concept of singleton pattern but I am not able to implement this concept in my problem.Please help.
If you would apply the singleton pattern to this, it would mean that only one user could open the PDF file. Then you would have to restart the web application before another user could look at it... I assume that what you really want to do, is to limit the usage per user, not per application. The problem is that the method that you use to open the file effectively prevents any attempt to check if the file is already open or not. The server code has no means at all of checking this, and you would have to open a window using client side code to have a chance to determine if there is already a window open or not. Even then, it depends on how the file is opened by the browser. If it's not opened in a browser window, but in a separate application (i.e. Adobe Reader), you can't determine if the file is still open or not. I'm not certain that it's ossible even if it's opened in a browser window. So, what you want to do falls somewhere between "maybe working sometimes" and "impossible". Either way it's definitely not possible to implement the way that you suggest.
Despite everything, the person most likely to be fooling you next is yourself.
-
If you would apply the singleton pattern to this, it would mean that only one user could open the PDF file. Then you would have to restart the web application before another user could look at it... I assume that what you really want to do, is to limit the usage per user, not per application. The problem is that the method that you use to open the file effectively prevents any attempt to check if the file is already open or not. The server code has no means at all of checking this, and you would have to open a window using client side code to have a chance to determine if there is already a window open or not. Even then, it depends on how the file is opened by the browser. If it's not opened in a browser window, but in a separate application (i.e. Adobe Reader), you can't determine if the file is still open or not. I'm not certain that it's ossible even if it's opened in a browser window. So, what you want to do falls somewhere between "maybe working sometimes" and "impossible". Either way it's definitely not possible to implement the way that you suggest.
Despite everything, the person most likely to be fooling you next is yourself.
Ok, my purpose was to understand singleton pattern with some live example.Can anyone please explain this singleton pattern with suitable example.
-
Ok, my purpose was to understand singleton pattern with some live example.Can anyone please explain this singleton pattern with suitable example.
Singleton Pattern is used when only one instance of a class is needed to provide services. In other words, only one instantiation. Here is an example: public class SingletonClass { private static SingletonClass _myInstance; //Private constructor so clients can not instantiate private SingletonClass { } // Clients call this when they need an instance of this class // Needs to be static as it can be called without instantiation or instance of this class public static SinletonClass getInstance() { if (_myInstance == null) { _myInstance = new SingletonClass(); } return _myInstance; } } The whole idea is the class checks whether an instance is available, if yes it returns it, else creates and returns it. Let me know if you have questions.