+ Reply to Thread
Results 1 to 3 of 3

Thread: C# Show Event Trouble

  1. #1
    espfutbol98's Avatar
    espfutbol98 is offline x10 Sophmore espfutbol98 is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Zagreb... želim
    Posts
    200

    C# Show Event Trouble

    Hello, I have a Forms program in Visual C# and there is an image, four text labels, and one progress bar. Three of the labels are supposed to be visible always while the remaining text label and progress bar are supposed to disappear after a for loop from 0 reaches 100 with progressBar1.Value++ and System.Threading.Thread.Sleep(50).

    At first I used the Load event handler but the progress bar would complete its task before the entire window opened in the first place. Next I tried using the Show handler but the current problem is the labels and image boxes load but no values are added. The images below will better explain but what I'm wondering is do I have to use multi-threaded methods and if so, how would I do that in this particular case?

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    
    namespace subjectSystem
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.Shown += new System.EventHandler(this.Form1_Show_1);
            }
            private void Form1_Show_1(object sender, EventArgs e)
            {
                label4.Visible = true;
                for (int i = 0; i < 100; i++)
                {
                    progressBar1.Value++;
                    System.Threading.Thread.Sleep(50);
                }
                System.Threading.Thread.Sleep(500);
                label4.Visible = false;
                progressBar1.Visible = false;
            }
        }
    }
    Should appear as:
    C# Show Event Trouble-1.png

    However in the begining:
    C# Show Event Trouble-2.png

    And finally after for loop ends:
    C# Show Event Trouble-3.png

  2. #2
    Livewire's Avatar
    Livewire is offline Abuse Compliance Officer Livewire is a glorious beacon of lightLivewire is a glorious beacon of light
    Join Date
    Jun 2005
    Location
    Behind a keyboard.
    Posts
    8,998

    Re: C# Show Event Trouble

    Multi-threading's the "proper" way to do it, but to be honest it can also cause problems later on just by being two separate threads. Conflicts are easy to run into.

    What you might want to try (I haven't personally tried it recently so I'm not 100% sure how to do it or if it'll work) is to look up the Form.Redraw/Refresh method(s) (one or both should exist). I believe you'd need both; redraw should redraw it while refresh should cause it to actually update for the viewer.

    Another alternative would be to use a timer and its tick event to increase the progress bar by 1.


    The root cause of your problem is the event itself - it won't redraw/refresh until the event completes.


    TOS breakers will be suspended regardless of race, creed, national origin, hair color, or favorite food. Thanks for your understanding!

  3. #3
    espfutbol98's Avatar
    espfutbol98 is offline x10 Sophmore espfutbol98 is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Zagreb... želim
    Posts
    200

    Re: C# Show Event Trouble

    Okay, heres an update while I'm still working on it: I found the Refresh() method in the API however it doesn't look like it will work to well. Next I looked up how to use a timer (I only started Visual C# a few days ago by the way) and this time it gives me a sepcific warning that the for loop didn't and that error is "Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on." which means I'll probably have to make a multi-threaded function for the progress bar.

+ Reply to Thread

Similar Threads

  1. Event Managment
    By jjuder in forum Free Hosting
    Replies: 1
    Last Post: 12-10-2009, 08:10 AM
  2. Regarding a suspension event
    By apbg2801 in forum Free Hosting
    Replies: 0
    Last Post: 11-03-2009, 02:05 AM
  3. New GP Event Headers
    By ghotel in forum Graphics & Webdesign
    Replies: 8
    Last Post: 01-24-2009, 07:02 PM
  4. Event ID 4201
    By Alpinestar in forum Computers & Technology
    Replies: 2
    Last Post: 11-17-2008, 05:43 AM
  5. Event calendar
    By rlodge in forum Scripts & 3rd Party Apps
    Replies: 0
    Last Post: 09-09-2008, 09:24 AM

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
x10hosting free hosting for the masses
dedicated servers