Results 1 to 8 of 8

Thread: Disable form dis-focusing when executing code

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    55

    Post 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.

  2. #2
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    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
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    55

    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.

  4. #4
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    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
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    55

    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.

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    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.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Disable form dis-focusing when executing code

    Quote Originally Posted by Cooi View Post
    EDIT: Sorry I forgot to say, it's C#, not Visual Basic.
    Thread moved from 'VB.Net' forum to 'C#' forum

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width