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