Mikhail Sokolov
Mikhail Sokolov

Reputation: 556

Separate data between multiple instances

I have one class where I store my data:

class Model
{
   public int Progress{ get; set; }
}

Second class where I can modify this data and notify subscribers about changes:

class Copy
{
   //...
   public static event EventHandler Changed;
   Model model = new Model();
   ProgressForm progressForm = new ProgressForm();

   public void Start()
   {
      for(int i=0;i<100;i++)
      { 
         model.Progress++;
         if(Changed!=null)
           Changed(this,EventArgs.Empty);
      }
   }

   //...
}

and something like this:

class ProgressForm
{
    Model model;
    public ProgressForm()
    { 
       model = new Model();
       Copy.Changed+=new Changed(ShowProgress);
    }

    void ShowProgress()
    {
       progressBar1.value = model.Progress;
    }
}

How can I change data for each model separately and show this data in ProgressForm when I run two or more instances of Copy?

Main()
{
   Copy copy = new Copy();
   copy.Start();
   Copy copy2 = new Copy();
   copy2.Start();
}

Upvotes: 3

Views: 124

Answers (1)

Emad Melad
Emad Melad

Reputation: 41

public class Model
{ 
    public int progress;
    public event EventHandler ProgressChanged;

    public int Progress
    {
        get { return progress; }
        set 
        { 
            progress = value;
            if (ProgressChanged != null)
            {
                ProgressChanged(this, null);
            }
        }
    }
}

public class Copy
{
    public List<Model> models = new List<Model>();
    public event EventHandler CopyProgrss; // FormModel binded to this event. 
    public void AddModel(Model m)
    {
        this.models.Add(m);
        m.ProgressChanged += new EventHandler(m_ProgressChanged);
    }

    void m_ProgressChanged(object sender, EventArgs e)
    {
        Model currentModel = sender as Model;
        int modelProgress = currentModel.Progress;
        if (CopyProgrss != null)
            CopyProgrss(modelProgress,null); // here you can caluclate your over progress. 
    }
}

Upvotes: 1

Related Questions