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. General Programming
  3. C#
  4. Is this safe

Is this safe

Scheduled Pinned Locked Moved C#
helpquestion
10 Posts 4 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.
  • M Offline
    M Offline
    Mide55
    wrote on last edited by
    #1

    I need to communicate with fiscal printer via third party software (Don't ask me way). I'm sending command file in one folder and receive answer file in another folder. My print method need to return number of fiscal invoice which is generated by printer and can be found in answer file. So my method look like this

    public int PrintInvoice()
    {
    //Do some code of sending command file

    //Wait until answer file received
    while(!\_answerFileReceived){}
    
    //Process answer file and return Invoice number
    

    }

    Also I have FileSystemWatcher which monitor answer directory (file creation) with code

    fswWatcher_FileCreated(object sender, FileSystemEventArgs e)
    {
    _answerFileReceived = true;
    }

    My question is "Is this safe" and can I have exceptions, or do you have any suggestion of another approach to above problem. Thank you.

    Y 1 Reply Last reply
    0
    • M Mide55

      I need to communicate with fiscal printer via third party software (Don't ask me way). I'm sending command file in one folder and receive answer file in another folder. My print method need to return number of fiscal invoice which is generated by printer and can be found in answer file. So my method look like this

      public int PrintInvoice()
      {
      //Do some code of sending command file

      //Wait until answer file received
      while(!\_answerFileReceived){}
      
      //Process answer file and return Invoice number
      

      }

      Also I have FileSystemWatcher which monitor answer directory (file creation) with code

      fswWatcher_FileCreated(object sender, FileSystemEventArgs e)
      {
      _answerFileReceived = true;
      }

      My question is "Is this safe" and can I have exceptions, or do you have any suggestion of another approach to above problem. Thank you.

      Y Offline
      Y Offline
      Yuri Vital
      wrote on last edited by
      #2

      Why not using event instead of infinite loop ? :doh: ie:

      fswWatcher_FileCreated(object sender, FileSystemEventArgs e)
      {
      MyInvoiceClass = newe MyInvoiceClass();
      int invoiceID = MyInvoiceClass. PrintInvoice();

       // Your stuff here
      

      }

      M 1 Reply Last reply
      0
      • Y Yuri Vital

        Why not using event instead of infinite loop ? :doh: ie:

        fswWatcher_FileCreated(object sender, FileSystemEventArgs e)
        {
        MyInvoiceClass = newe MyInvoiceClass();
        int invoiceID = MyInvoiceClass. PrintInvoice();

         // Your stuff here
        

        }

        M Offline
        M Offline
        Mide55
        wrote on last edited by
        #3

        First I have to send file so PrintInvoice can not be in FileCreated event.

        L 1 Reply Last reply
        0
        • M Mide55

          First I have to send file so PrintInvoice can not be in FileCreated event.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Not the first part (before the wait-loop), but the second part of it can.

          M 1 Reply Last reply
          0
          • L Lost User

            Not the first part (before the wait-loop), but the second part of it can.

            M Offline
            M Offline
            Mide55
            wrote on last edited by
            #5

            I agree with you, but my Print method is inside class and must return Invoice number. Class name is FiscalPrinter and it has method PrintInvoice. So I call

            FiscalPrinter printer = new FiscalPrinter()

            //Assing some properties

            //Print invoice and get invoice number

            int InvoiceNumber = printer.PrintInvoice();

            Thats why I'm using infinite loop (it will be extended to wait some amount time so it won't be infinite), and I'm not sure is this approach safe. Thank you for your answers.

            L 1 Reply Last reply
            0
            • M Mide55

              I agree with you, but my Print method is inside class and must return Invoice number. Class name is FiscalPrinter and it has method PrintInvoice. So I call

              FiscalPrinter printer = new FiscalPrinter()

              //Assing some properties

              //Print invoice and get invoice number

              int InvoiceNumber = printer.PrintInvoice();

              Thats why I'm using infinite loop (it will be extended to wait some amount time so it won't be infinite), and I'm not sure is this approach safe. Thank you for your answers.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Well there is a problem with that, while you are looping/waiting the event loop can not process the event raised by the file system watcher. Or is it on a different thread? In that case, you should definitely use a real synchronization primitive instead of busy-waiting.

              L 1 Reply Last reply
              0
              • L Lost User

                Well there is a problem with that, while you are looping/waiting the event loop can not process the event raised by the file system watcher. Or is it on a different thread? In that case, you should definitely use a real synchronization primitive instead of busy-waiting.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Asynchronous events tend to get handled on TrheadPool threads, see here[^]. I didn't test specifically for FileSystemWatcher, however I expect them to behave similarly. So yes busy loops are bad and inter-thread synchronization is recommended. :)

                Luc Pattyn [My Articles] Nil Volentibus Arduum iSad

                L 1 Reply Last reply
                0
                • L Luc Pattyn

                  Asynchronous events tend to get handled on TrheadPool threads, see here[^]. I didn't test specifically for FileSystemWatcher, however I expect them to behave similarly. So yes busy loops are bad and inter-thread synchronization is recommended. :)

                  Luc Pattyn [My Articles] Nil Volentibus Arduum iSad

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Well I like to be sure so I looked it up; the example on msdn:filesystemwatcher[^] shows that FSW events happen on a different threat

                  L 1 Reply Last reply
                  0
                  • L Lost User

                    Well I like to be sure so I looked it up; the example on msdn:filesystemwatcher[^] shows that FSW events happen on a different threat

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    Well, it doesn't show it, what it does is proof or disproof depending on whatever the behavior is when you actually run that code. Will it hang waiting for exit? or will it ever report something? They don't mention the ThreadPool. they even manage to "explain" without using the word "thread" (except for the silly sentence "Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe." which they have on most every page). :~

                    Luc Pattyn [My Articles] Nil Volentibus Arduum iSad

                    L 1 Reply Last reply
                    0
                    • L Luc Pattyn

                      Well, it doesn't show it, what it does is proof or disproof depending on whatever the behavior is when you actually run that code. Will it hang waiting for exit? or will it ever report something? They don't mention the ThreadPool. they even manage to "explain" without using the word "thread" (except for the silly sentence "Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe." which they have on most every page). :~

                      Luc Pattyn [My Articles] Nil Volentibus Arduum iSad

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      I assumed it would work, perhaps I'm overestimating MS :)

                      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