Disable form dis-focusing when executing code
Hi all,
When my program is executing code it's disfocusing the form so you can't do anything on the form. Is there a possibility to fix that? I already have heard of the Backgroundworker, but I don't know the way how to change things to the form when it runs its background-process, because then it will give errors.
Any hint about the backgroundworker or another solution?
EDIT: Sorry I forgot to say, it's C#, not Visual Basic.
Re: Disable form dis-focusing when executing code
It could be how you are doing what you are doing? sometimes rewriting slow sections of code can fix these issues. would need to see the code though
Re: Disable form dis-focusing when executing code
It's a code that will always take a long time, because it's checking files you select in a folder, so it can't be sped up.
Edit: and the thing I want is showing the status on controls in the form.
Re: Disable form dis-focusing when executing code
Again without seeing the code i wont be able to help. Iwithout knowing how you are checking the files i wouldn't know, I presume that your file checking is event driven and not all done in a loop then for example
Re: Disable form dis-focusing when executing code
I'll give an example of what the program does:
Code:
string[] filePaths;
string allText;
FolderBrowserDialog SelectFolder = new FolderBrowserDialog();
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Value = 0;
if (SelectFolder.ShowDialog() == DialogResult.OK)
{
Refresh();
backgroundWorker1.RunWorkerAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
filePaths = Directory.GetFiles(SelectFolder.SelectedPath);
progressBar1.Maximum = (int)filePaths.LongLength;
foreach (string file in filePaths)
{
progressBar1.Value += 1;
allText = File.ReadAllText(file);
if (allText.Contains("checked text"))
{
//do things here
}
}
}
So it's normal it will always take some time.
Re: Disable form dis-focusing when executing code
Do the work in a background task. You spin off a thread to do the work and that leaves you UI thread to be responsive.
Re: Disable form dis-focusing when executing code
Quote:
Originally Posted by
Cooi
EDIT: Sorry I forgot to say, it's C#, not Visual Basic.
Thread moved from 'VB.Net' forum to 'C#' forum
Re: Disable form dis-focusing when executing code
you're already off loading it to a thread... so that's done.. what you need to do now is use the .ReportProgress method of your backgroundworker to report it's progress to the main thread. And then be sure you are handling the _ProgressChanged event for BackgroundWorker1 .... I think that's right... it's in that event where you'll want to update your progress bar... you can't do it from the background thread itself.
Ah... I see it's a bit more complicated than that. OK, short and sweet... you can't access elements on your form from the thread instance... so, what you'll need to to is pass SelectFolder.SelectedPath as a parameter when you call the RunWorkerAsync method:
backgroundWorker1.RunWorkerAsync(SelectFolder.SelectedPath);
Then first thing in your DoWork event, extract the arguement.... you'll have to look up the examples on the BGW, it shows how to do that.... then get your list of files and start your loop.
Since you can't pass back the number of files, I'd simply set your progbar to go from 0 to 100.... then calc the pct done in the loop, and pass that value back (*100 as a whole number) as part of the .ReportProgress call, and use that number to update your progress bar (as part of the Progresschanged event).
I think I got that right.
-tg