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. Couldn't click any botton using BackgroundWorker

Couldn't click any botton using BackgroundWorker

Scheduled Pinned Locked Moved C#
helptutorialvisual-studiolearning
8 Posts 2 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
    pnpfriend
    wrote on last edited by
    #1

    Hi, I'm new to Threading.. After learning how to work with threading, I found out that I could use BackgroundWorker since I'm using VS 2005. I also check on the example and try to do the same like following. However I couldn't click on Cancel Button after I click StartButton. I couldn't interrupt what my program is currenting doing. I dont' know what I am doing it wrong. I couldnt' find the answer. Please help me. Thank you. My form has StartButton, Cancel Button, Edit Text To Display the message 1) Add BackgroundWorker to my Form, 2) Set WorkerReportsProgress = False 3) WorkderSupportsCancella = True; 4) Add DoWork and RunWokerCompleted events 5) Add event to StartButton, and Cancel button. 6) Add functions: Private void BrowseFileFolder(),that will browse the folder given and then get all file information. private void BrowseFF(BackgroundWorker bk, DoWorkEventArgs e), 7) Add another function: public delegate void BrowseFFDelegate(); because i got error calling BrowseFileFolder() directly from DoWork(). 8) OnStartButton() -> myBkWorker.RunWorkderAsync(); 9) OnMyBkWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorkder worker = sender as BackgroundWorkder; BrowseFF(worker,e); } 10) private void BroswerFF(BackgroundWorker bk, DoWorkEventArgs e) { BKWorker = worker; workerEvent = e; Invoke(new BrowseFFDelegate(BrowseFileFolder)); } 11) private void BrowseFileFolder() { if(BKWorker.CancellationPending) { workerEvent.Cancel = true; return; } else { DirectoryInfo dirfolder = new DirectoryInfo(pathToBrowse); .... .... //Getting file and folder information .... } } Thank you.

    L 1 Reply Last reply
    0
    • P pnpfriend

      Hi, I'm new to Threading.. After learning how to work with threading, I found out that I could use BackgroundWorker since I'm using VS 2005. I also check on the example and try to do the same like following. However I couldn't click on Cancel Button after I click StartButton. I couldn't interrupt what my program is currenting doing. I dont' know what I am doing it wrong. I couldnt' find the answer. Please help me. Thank you. My form has StartButton, Cancel Button, Edit Text To Display the message 1) Add BackgroundWorker to my Form, 2) Set WorkerReportsProgress = False 3) WorkderSupportsCancella = True; 4) Add DoWork and RunWokerCompleted events 5) Add event to StartButton, and Cancel button. 6) Add functions: Private void BrowseFileFolder(),that will browse the folder given and then get all file information. private void BrowseFF(BackgroundWorker bk, DoWorkEventArgs e), 7) Add another function: public delegate void BrowseFFDelegate(); because i got error calling BrowseFileFolder() directly from DoWork(). 8) OnStartButton() -> myBkWorker.RunWorkderAsync(); 9) OnMyBkWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorkder worker = sender as BackgroundWorkder; BrowseFF(worker,e); } 10) private void BroswerFF(BackgroundWorker bk, DoWorkEventArgs e) { BKWorker = worker; workerEvent = e; Invoke(new BrowseFFDelegate(BrowseFileFolder)); } 11) private void BrowseFileFolder() { if(BKWorker.CancellationPending) { workerEvent.Cancel = true; return; } else { DirectoryInfo dirfolder = new DirectoryInfo(pathToBrowse); .... .... //Getting file and folder information .... } } Thank you.

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

      pnpfriend wrote:

      I'm new to Threading..

      Yeah you really didn't need to explain that. You need to "Invoke" UI code from secondary threads. http://www.google.com/search?hl=en&q=MSDN+C%23+thread+UI+invoke&btnG=Search[^]

      P 1 Reply Last reply
      0
      • L led mike

        pnpfriend wrote:

        I'm new to Threading..

        Yeah you really didn't need to explain that. You need to "Invoke" UI code from secondary threads. http://www.google.com/search?hl=en&q=MSDN+C%23+thread+UI+invoke&btnG=Search[^]

        P Offline
        P Offline
        pnpfriend
        wrote on last edited by
        #3

        Thank you Mike, I have Invoke Methods.. I do not have any problem updating any of my form controls only that after I called the invoke method.. ( when the user click on Start Button) I can no longer click on any of form buttons like Cancel Button. I would like to enable user interrupting whatever the program is busy working after the Start Button got clicked. Thanks.

        L 1 Reply Last reply
        0
        • P pnpfriend

          Thank you Mike, I have Invoke Methods.. I do not have any problem updating any of my form controls only that after I called the invoke method.. ( when the user click on Start Button) I can no longer click on any of form buttons like Cancel Button. I would like to enable user interrupting whatever the program is busy working after the Start Button got clicked. Thanks.

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

          pnpfriend wrote:

          I can no longer click on any of form buttons like Cancel Button.

          Then you have something "blocking" or code executing in the main thread that you perhaps "think" is executing in a background thread.

          P 1 Reply Last reply
          0
          • L led mike

            pnpfriend wrote:

            I can no longer click on any of form buttons like Cancel Button.

            Then you have something "blocking" or code executing in the main thread that you perhaps "think" is executing in a background thread.

            P Offline
            P Offline
            pnpfriend
            wrote on last edited by
            #5

            I know.. there might be something is totally wrong on what I am doing. but i just couldnt' see it. Yes.. I believe my function is running the BackgroundWorker thread.. but looks like i am not. therefore I posted my code at first place. Please refer to my first post. I worte down exactly what I did.. I'm trying to get the file and folder information using recursive. since there are lots of files to browse I would like to allow the user interrupts by clicking Cancel Button. However, I can no longer click any button once the program gets into that function. I used Invoke method too. So what am I missing? what am I doing it wrong?? Please guide me, point me out my error. Thanks.

            L 1 Reply Last reply
            0
            • P pnpfriend

              I know.. there might be something is totally wrong on what I am doing. but i just couldnt' see it. Yes.. I believe my function is running the BackgroundWorker thread.. but looks like i am not. therefore I posted my code at first place. Please refer to my first post. I worte down exactly what I did.. I'm trying to get the file and folder information using recursive. since there are lots of files to browse I would like to allow the user interrupts by clicking Cancel Button. However, I can no longer click any button once the program gets into that function. I used Invoke method too. So what am I missing? what am I doing it wrong?? Please guide me, point me out my error. Thanks.

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

              pnpfriend wrote:

              So what am I missing? what am I doing it wrong??

              Sorry to say but your code is a mess and very difficult to tell what you are doing. Maybe... just maybe this line Invoke(new BrowseFFDelegate(BrowseFileFolder)); Is sending you back to the UI thread where you finally do all the directory scanning work and therefore you are not even using the worker thread to do the work.

              P 1 Reply Last reply
              0
              • L led mike

                pnpfriend wrote:

                So what am I missing? what am I doing it wrong??

                Sorry to say but your code is a mess and very difficult to tell what you are doing. Maybe... just maybe this line Invoke(new BrowseFFDelegate(BrowseFileFolder)); Is sending you back to the UI thread where you finally do all the directory scanning work and therefore you are not even using the worker thread to do the work.

                P Offline
                P Offline
                pnpfriend
                wrote on last edited by
                #7

                Is it returning to the UI Thread? Invoke(new BrowseIFFDelegate(BrowseFileFolder)); It is working now for me after I added Application.DoEvent(); I am not so sure if it is in BK worker or not anymore but I can now click on cancel button. Thanks for your help mike.

                L 1 Reply Last reply
                0
                • P pnpfriend

                  Is it returning to the UI Thread? Invoke(new BrowseIFFDelegate(BrowseFileFolder)); It is working now for me after I added Application.DoEvent(); I am not so sure if it is in BK worker or not anymore but I can now click on cancel button. Thanks for your help mike.

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

                  pnpfriend wrote:

                  I am not so sure if it is in BK worker or not anymore

                  That's not a good thing. If you are going to put threads in a production application you better know what they and you are doing.

                  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