Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Singleton Pattern

Singleton Pattern

Scheduled Pinned Locked Moved ASP.NET
helpdesignsysadminbeta-testingregex
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pranav Thakur
    wrote on last edited by
    #1

    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.

    S L G 3 Replies Last reply
    0
    • P Pranav Thakur

      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.

      S Offline
      S Offline
      Sylvester george
      wrote on last edited by
      #2

      are you loading the pdf file where the button exists?

      Regards, Sylvester G If we don't succeed, we run the risk of failure

      1 Reply Last reply
      0
      • P Pranav Thakur

        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.

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • P Pranav Thakur

          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.

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          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.

          P 1 Reply Last reply
          0
          • G Guffa

            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.

            P Offline
            P Offline
            Pranav Thakur
            wrote on last edited by
            #5

            Ok, my purpose was to understand singleton pattern with some live example.Can anyone please explain this singleton pattern with suitable example.

            C 1 Reply Last reply
            0
            • P Pranav Thakur

              Ok, my purpose was to understand singleton pattern with some live example.Can anyone please explain this singleton pattern with suitable example.

              C Offline
              C Offline
              CodingYoshi
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups