#pragma once
#include "Form1.h"
namespace NamespaceThread
{
using namespace System;
ref class ThreadClass
{
public:
static void MyThread();
};
void ThreadClass::MyThread()
{
for (int i = 0; i <= 100; i++)
{
//Delegate needs to be called to update progressBar1->Value
}
}
}
in form1.h i have a delegate like below
delegate void ProgressStep(int i)and below the method that should update the progressBar
public:
void ProgressStep(int i)
{
progressBar1->Value = i;
}
Could someone help me out on how to pass the value i to form1.h so i can update the progress bar

Passing a value from one class to another class
xeondev
thank you for the help einaros i finally had time to test it out a little bit and i still am having a problem with it. In thread.h i have a class (ThreadClass). i have a simple loop there that will increment the value of i till it reaches 100. I also have a delegate in the class. Below is what it looks like:
#pragma
oncenamespace
NamespaceThread{
using namespace System; ref class ThreadClass{
public: static void MyThread(); delegate void ProgressStep(int i);};
void ThreadClass::MyThread(){
ProgressStep^ step;
for (int i = 0; i <= 100; i++){
step->Invoke(i); //<-- used to update the delegate
}
}
}
In the form1 constructor i have the following event to check the delegate in the ThreadClass:threadClass->ProgressStep +=
gcnew System::EventHandler(this, Form1::ProgressUpdated);ProgressUpdated Method is what updates the progressBar1:
void ProgressUpdated(int value){
progressBar1->Value = value;
}
I'm getting errors with the event when i try to compile
Error 1 error C2273: 'function-style cast' : illegal as right side of '->' operator c:\documents and settings\carteada\desktop\projects\threadingsample_gui\threadingsample_gui\Form1.h 29
Error 2 error C3867: 'ThreadingSample_GUI::Form1::ProgressUpdated': function call missing argument list; use '&ThreadingSample_GUI::Form1::ProgressUpdated' to create a pointer to member c:\documents and settings\carteada\desktop\projects\threadingsample_gui\threadingsample_gui\Form1.h 29
Error 3 error C3350: 'System::EventHandler' : a delegate constructor expects 2 argument(s) c:\documents and settings\carteada\desktop\projects\threadingsample_gui\threadingsample_gui\Form1.h 29
Could you help me out
Thanks
stlaural
The most fit pattern to apply in this case would be Observer (http://en.wikipedia.org/wiki/Observer_pattern).
This pattern would direct the relation from Form1 to ThreadClass, as ThreadClass would contain an event which Form1 could subscribe to. Whenever ThreadClass fired the event, this would mean that the progress was updated, and listeners should process the new value -- such as update a ProgressBar. The foremost benefit of using this pattern is the fact that business logic (your ThreadClass) more loosely coupled to the presentation layer (your GUI). The ThreadClass needs not even know that Form1 exists, so if one day you should choose to replace Form1 with a console, or perhaps make the application a service, you would not need to alter ThreadClass.
To implement this pattern, you could use the EventHandler class (http://msdn2.microsoft.com/en-us/library/db0etb8x.aspx), and set it up similar to how button click events are setup in forms. The subscription process would in this case be something along the lines of (called from Form1 on an instance of ThreadClass):
A custom EventArgs class would pass the updated progress bar value back to ProgressUpdated. An example of how to implement this is provided in the MSDN article noted above.
Ronald L
There's a couple of changes needed. First of all, you can create your own notification delegate. This delegate will be the signature of the event fired from ThreadClass to Form1. The second thing your thread class needs is an event with the delegate type. Whenever the progress is updated, this event will be fired.
Your form will create an instance of your worker class, subscribe to the event, create a thread to run in the worker class, then start the thread.
You will notice that the UpdateProgress function does some extra processing, in addition to updating the progress bar's value. The extra invoke is, relatively simply put, needed to asure that both the gui and worker thread isn't trying to access the progress bar's internal data at once. If this was allowed, your application could (and would) crash. The framework therefor prohibits direct calls from external threads onto the gui thread's resources. See http://msdn2.microsoft.com/en-us/library/3s8xdz5c.aspx for more information on this.