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. BackgroundWorker and command line exe: what DoWork deals with?

BackgroundWorker and command line exe: what DoWork deals with?

Scheduled Pinned Locked Moved C#
tutorialquestion
18 Posts 6 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.
  • L LordZoster

    Hallo i run a .bat with Process.Start that performs some operations (partitions a disk and deploys an OS image to it) that doesn't return a progress information useful to determine the progression. I would like to use it with a BackgroundWorker, but in every example i see that the DoWork method works upon some data returned by the process in order to determine the progression. In my case it's not clear to me how to hook the process to the BGW component. Could you give me some hints? Thanks

    modified on Saturday, November 8, 2008 3:10 AM

    S Offline
    S Offline
    sph3rex
    wrote on last edited by
    #6

    Well this is just an idea ... you could use the Process.StandardOutput to get data from the cmd window(in which the bat runs) ... then you would have to output some "marks" from the .bat file in order to "signal" an update to the state of how far the things have gone. Check Process.StandardOutput on MSDN documentation.

    1 Reply Last reply
    0
    • L LordZoster

      thank you, both of your suggestion are useful and can apply, but i have an option too: the batch file launches in sequence DISKPART (along with several SELECT DISK, SELECT PARTITION, FORMAT, etc commands) and IMAGEX: both of them returns some messages, imagex in particular updates a percentual progression indicator. Could i redirect these messages to the frontend?

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #7

      Usually not. They're usually dispaly using non-stream methods that your code would not be able to intercept or would be very difficult to process with any meaning. A better option would be to get rid of the batch file and execute each command from your background worked. When you go from one step (command line) in the batch file to the next, you can update the progress bar. Keep in mind you will NOT be able to increment progress inside each of those steps.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      1 Reply Last reply
      0
      • L LordZoster

        Hallo i run a .bat with Process.Start that performs some operations (partitions a disk and deploys an OS image to it) that doesn't return a progress information useful to determine the progression. I would like to use it with a BackgroundWorker, but in every example i see that the DoWork method works upon some data returned by the process in order to determine the progression. In my case it's not clear to me how to hook the process to the BGW component. Could you give me some hints? Thanks

        modified on Saturday, November 8, 2008 3:10 AM

        M Offline
        M Offline
        Muammar
        wrote on last edited by
        #8

        This's how I would do it.. - If the process is well known to me, I would monitor something that it does all the time "like in the installation, the number of files it creates and the name of the final file created" and count those files while it's running and update my progress bar accordingly - If this's not an option, then this's why we have a dump loading animation :laugh:


        All generalizations are wrong, including this one! (\ /) (O.o) (><)

        L 2 Replies Last reply
        0
        • M Muammar

          This's how I would do it.. - If the process is well known to me, I would monitor something that it does all the time "like in the installation, the number of files it creates and the name of the final file created" and count those files while it's running and update my progress bar accordingly - If this's not an option, then this's why we have a dump loading animation :laugh:


          All generalizations are wrong, including this one! (\ /) (O.o) (><)

          L Offline
          L Offline
          LordZoster
          wrote on last edited by
          #9

          i'm trying both these approaches ;) 1- i'm trying to use the wimgapi.dll to control the image restore 2- in the meanwhile i'll use a simply process.start and process.end for each of the .bat' step

          1 Reply Last reply
          0
          • M Muammar

            This's how I would do it.. - If the process is well known to me, I would monitor something that it does all the time "like in the installation, the number of files it creates and the name of the final file created" and count those files while it's running and update my progress bar accordingly - If this's not an option, then this's why we have a dump loading animation :laugh:


            All generalizations are wrong, including this one! (\ /) (O.o) (><)

            L Offline
            L Offline
            LordZoster
            wrote on last edited by
            #10

            i'm trying to monitor the total space written on disk, but that value is written only when process has completed and not while it's running: during the process execution, the GUI seems to be frozen. how could I get it up?

            modified on Sunday, November 16, 2008 4:17 PM

            M 1 Reply Last reply
            0
            • L LordZoster

              i'm trying to monitor the total space written on disk, but that value is written only when process has completed and not while it's running: during the process execution, the GUI seems to be frozen. how could I get it up?

              modified on Sunday, November 16, 2008 4:17 PM

              M Offline
              M Offline
              Muammar
              wrote on last edited by
              #11

              Sorry mate, haven't tried that actually, but I could assume doing it by separating file stream writings and get the total free space left after closing the stream after writing each file of the process.


              All generalizations are wrong, including this one! (\ /) (O.o) (><)

              L 1 Reply Last reply
              0
              • M Muammar

                Sorry mate, haven't tried that actually, but I could assume doing it by separating file stream writings and get the total free space left after closing the stream after writing each file of the process.


                All generalizations are wrong, including this one! (\ /) (O.o) (><)

                L Offline
                L Offline
                LordZoster
                wrote on last edited by
                #12

                Thanks Muammar, the write process is managed by a pure command line process, hence i cannot distinguish the streams.. The only option I have is to keep monitored the disk space being written, but the process.start is on the same thread than the GUI, hence the GUI is locked during the process. It gets refreshed only once the process ends. I'm trying a way to launch the command line process on a separate thread, keeping the GUI free to refresh.

                M 1 Reply Last reply
                0
                • L LordZoster

                  Thanks Muammar, the write process is managed by a pure command line process, hence i cannot distinguish the streams.. The only option I have is to keep monitored the disk space being written, but the process.start is on the same thread than the GUI, hence the GUI is locked during the process. It gets refreshed only once the process ends. I'm trying a way to launch the command line process on a separate thread, keeping the GUI free to refresh.

                  M Offline
                  M Offline
                  Muammar
                  wrote on last edited by
                  #13

                  Actually, it seems like you have to give up your current code and start off with a new clean and simple one, which simply loops in a background worker do_work method and in each loop you: 1. open a file stream 2. copy the file through it //instead of using a dull command-line copy (sorry!) 3. close the stream //so you have your hd unlocked to decide the new free space 4. get the new free space and do the math 5. submit that value to the status member of the background process 6. load the value from step 5 to your progress bar control Voala!


                  All generalizations are wrong, including this one! (\ /) (O.o) (><)

                  L 1 Reply Last reply
                  0
                  • M Muammar

                    Actually, it seems like you have to give up your current code and start off with a new clean and simple one, which simply loops in a background worker do_work method and in each loop you: 1. open a file stream 2. copy the file through it //instead of using a dull command-line copy (sorry!) 3. close the stream //so you have your hd unlocked to decide the new free space 4. get the new free space and do the math 5. submit that value to the status member of the background process 6. load the value from step 5 to your progress bar control Voala!


                    All generalizations are wrong, including this one! (\ /) (O.o) (><)

                    L Offline
                    L Offline
                    LordZoster
                    wrote on last edited by
                    #14

                    i cannot loop any file writing: the command line process is imagex, that explode and apply a .wim image to the specified disk, it runs in a single instance, from start to end, and it takes approximately 20 minutes. Hence, the process cannot be subdivided.

                    M 1 Reply Last reply
                    0
                    • L LordZoster

                      i cannot loop any file writing: the command line process is imagex, that explode and apply a .wim image to the specified disk, it runs in a single instance, from start to end, and it takes approximately 20 minutes. Hence, the process cannot be subdivided.

                      M Offline
                      M Offline
                      Muammar
                      wrote on last edited by
                      #15

                      Say, this's different than I thought, hmmm, I'm not sure... did you try getting the file size instead?? I think it would lead into the same thing after all. If I were you, I'd simply use a dull loading like the one I'm using here


                      All generalizations are wrong, including this one! (\ /) (O.o) (><)

                      L 1 Reply Last reply
                      0
                      • M Muammar

                        Say, this's different than I thought, hmmm, I'm not sure... did you try getting the file size instead?? I think it would lead into the same thing after all. If I were you, I'd simply use a dull loading like the one I'm using here


                        All generalizations are wrong, including this one! (\ /) (O.o) (><)

                        L Offline
                        L Offline
                        LordZoster
                        wrote on last edited by
                        #16

                        thanks mate, i solved it instancing a separate thread.

                        M 1 Reply Last reply
                        0
                        • L LordZoster

                          thanks mate, i solved it instancing a separate thread.

                          M Offline
                          M Offline
                          Muammar
                          wrote on last edited by
                          #17

                          LordZoster wrote:

                          separate thread.

                          It's embarrassing I missed that!!

                          LordZoster wrote:

                          thanks mate

                          No, thanks to you LordZoster! This is usually the most beautiful kind of posts here in the cp: when you fight to find an answer but eventually find it yourself and be generous enough to share the answer with everyone.. Thanks again mate, and welcome to the code project:) we're proud to have such people in here:)


                          All generalizations are wrong, including this one! (\ /) (O.o) (><)

                          L 1 Reply Last reply
                          0
                          • M Muammar

                            LordZoster wrote:

                            separate thread.

                            It's embarrassing I missed that!!

                            LordZoster wrote:

                            thanks mate

                            No, thanks to you LordZoster! This is usually the most beautiful kind of posts here in the cp: when you fight to find an answer but eventually find it yourself and be generous enough to share the answer with everyone.. Thanks again mate, and welcome to the code project:) we're proud to have such people in here:)


                            All generalizations are wrong, including this one! (\ /) (O.o) (><)

                            L Offline
                            L Offline
                            LordZoster
                            wrote on last edited by
                            #18

                            glad to be here, i'm learning alot from cp!

                            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