C# Show Event Trouble

espfutbol98

New Member
Messages
200
Reaction score
2
Points
0
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:
1.png

However in the begining:
2.png

And finally after for loop ends:
3.png
 

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
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.
 

espfutbol98

New Member
Messages
200
Reaction score
2
Points
0
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.
 
Top