CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    [RESOLVED] Create a delay in a window form

    I have made an application that opens a file reads the content then splits the content into categories depending on their properties. Some of the files are over 1mb in size and with my code they can take some time so I tried adding a progress bar to show progress over time so the user knows its working and not frozen.

    Since I am still learning and wanted hands on learning I made two instances of the application. One that uses DLLs and one that has all the information contained within the exe itself. The one w/o DLLs I got to work flawlessly with a progress bar. The issue I am having is the one that calls up the DLL. Since I was unable to think of any ideas to make this work with a progress bar I decided to make it a marquee bar. I need a way to make a pause for the marquee to be able to initiate and for the application to not look frozen as it does now.

    Here is the code for the one I am trying to get to work
    Code:
    private void btnExtract_Click(object sender, EventArgs e)
            {
                // Disable button so its not pressed on accident again
                btnExtract.Enabled = false;
    
                // Clear text box and Display message of extracting items
                lstDisplay.Items.Clear();
                lstDisplay.Items.Add("Extracting items now...");
    
                // Set speed of marquee to show the program is working
                progBar.MarqueeAnimationSpeed = 100;
    
                // Create extraction class
                string filePath = txtDir.Text;
                Extracion path = new Extracion();
                path.Extract(filePath);
    
                // Display operation completed message
                lstDisplay.Items.Add("");
                lstDisplay.Items.Add("Extraction Complete");
    
                // Remove extension from path
                string dirPath = Path.GetFileNameWithoutExtension(txtDir.Text);
    
                // Get list of directories and add to combo box categories
                DirectoryInfo catDir = new DirectoryInfo(dirPath);
    
                DirectoryInfo[] catFiles = catDir.GetDirectories();
                foreach (DirectoryInfo catName in catFiles)
                {
                    cmbCat.Items.Add(catName.Name);
                }
    
                cmbCat.Enabled = true;
    
                // Set speed of marquee to 0 to show program has finished
                progBar.MarqueeAnimationSpeed = 0;
    My code may not be the best to do this but I'm sure there is a way to get it to work I just haven't found out how. I have read about using Thread.Sleep(x) but that seems to freeze the whole application and after it comes back it does the same thing as without it. It keeps on going through the file and no update on the marquee or the list box is done until after it's done splitting up the file.

    I have also tried making a timer but from what I noticed the timer got executed twice the way I made it. Maybe if I had two timers the first one executing my functions and the second one doing nothing but disabling both timers that would be a solution but I haven't tried that yet. I am hoping someone here can help me out with this. It's not an essential function for my program but would like something to show users that its working. I could always use the one without the DLL but the DLL one just looks tidier.

    Thanks in advance to anyone looking at my post.

  2. #2
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    Re: Create a delay in a window form

    I was unable to find an Edit button so I am sorry for double post. I decided to test the program leaving marquee running at 100 as default and no modifications done to it. It seems when I start extracting the data it makes the application appear to be frozen. I guess it's understandable since I didn't exactly use the best method of data editing since I extract and save the files into individual files depending on the item extracted from the parent file. The application keeps doing its job though so its no biggie I will just make sure users know it may take a while to complete its task but that it is working.

  3. #3
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Create a delay in a window form

    For long-running tasks executed from a GUI it might be helpful to spawn another thread to do the work using BackgroundWorker. See the following tutorial: http://www.dotnetperls.com/backgroundworker
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Create a delay in a window form

    Ubiquitous, with all due respect you have a lack of understanding with how a windows application works. Trying to solve the problem with sleep or timer isn't going to help your situation.

    The bottom line is that the main thread of your application needs to pump Windows messages. In .Net, this happens underneath the covers (but happens nevertheless).

    When you put a long running operation inside your main ui thread, you prevent the app from pumping messages and so it appears to freeze.

    It freezes because the app can't respond to messages such as wm_paint, and wm_move. If you can't process these messages in a timely manner, your app will appear to freeze.

    So the answer to the problem is to put your work inside a separate thread as BioPhysicEng suggested.

    Btw, putting code inside an assembly (dll) should have no impact on your problem - where the code resides (i.e. which assembly) doesn't make any difference because when the code is JIT compiled, it's in memory anyway.

    The problems you are seeing is because you are trying to perform a long running operation inside the main UI thread.

  5. #5
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    Re: Create a delay in a window form

    As I mentioned in the post I am learning how stuff works so I wanted a hands on approach.

    Thank you BioPhysEngr for your helpful post and link.

    Thank you Arjay for the explanation and clarification on why it happens and how it works. The code in the DLL changed a little compared to the file without the DLL since I am still a newbie to coding. From what you explained to me I am guessing the one without the DLL works because I force it to update the progress bar from within the intensive operation.

    Well I want to say thanks again since I never even thought about Background worker. This has definitely helped me expand my knowledge

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

Featured