I want to implement some kind of MVC pattern in C# for loading some data stored in files. I want my loader class to signal other classes when the loading has started, when a piece of the work has been done (read a line from the file for example, to update the progressbar in a form or other ways to show elaboration progress), when the data has been completely created reading the file so any other classes can start to use it. I want this loader to be used inside another thread or the same thread of the application. So i've defined some events inside the FileLoader class
public delegate void LoadStartedDelegate(uint steps);
public event LoadStartedDelegate LoadStarted;
public delegate void StepFinishedDelegate();
public event StepFinishedDelegate StepFinished;
public delegate void LoadFinishedDelegate();
public event LoadFinishedDelegate LoadFinished;
then i call the 3 events during the Load function. I've created a form and put a progress bar in it. Then i created 3 functions and set the progressbar
private void button1_Click(object sender, EventArgs e)
{
loader = new ObjLoader("box1.obj");
loader.LoadStarted += new ObjLoader.LoadStartedDelegate(loader_LoadStarted);
loader.StepFinished += new ObjLoader.StepFinishedDelegate(loader_StepFinished);
loader.LoadFinished += new ObjLoader.LoadFinishedDelegate(loader_LoadFinished);
loader.Load();
}
void loader_LoadFinished()
{
// use the loader data here
}
void loader_StepFinished()
{
progressBar1.Value++;
progressBar1.Refresh();
}
void loader_LoadStarted(uint steps)
{
progressBar1.Minimum = 0;
progressBar1.Maximum = Convert.ToInt32(steps);
progressBar1.Value = 0;
}
the problem is that the progressbar received the value update but doesn't refresh itself at that moment. Certainly there's a different thread that renders the progressbar but what is the difference between using this logic or doing a for cycle inside button1_Click and update the value of the progress from there? Or having a backgroundworker signals the form of the update? If there's a render thread you can call progressbar.Value++ anywhere but the bar will update slowly with its time without waiting for you. Is there a way to make the current thread (my situation) wait for the refresh. I've added a Sleep call in my loader but it's not precise and not professional.
Thanks