Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Good day everyone.
There is a class that performs some work:
C#
public  class MyNew
        {
               private bool Initialize()
               {
                 this.InitializeProgressDialog();
                 return true;
               }
               private void InitializeProgressDialog()
               {
                 this.ProgressDialog = new ProgressBar();
                 this.ProgressDialog.setProgressBarLimit(this.Count);
                 this.ProgressDialog.Show();
               }
               private void CreateBMP()
               {
                 this.ProgressDialog.updateStatus("Creating something", true);
               }
               private void CreateSmall()
               {
                 this.ProgressDialog.updateStatus("Creating something new", false);
               }
               private void CreateBigger()
               {
                  int[] array;
                  array = new int[]
                  {
                     1,
                     2,
                     3,
                     4,
                     5
                  };
                   string[] array2 = new string[array.Length];
                   for (int i = 0; i < array.Length; i++)
               {
                  this.ProgressDialog.updateStatus(string.Concat(new object[]
                {
                    "Creating something else: ",
                    i + 1,
                    " of ",
                    array.Length
                }), true);

That is, in ProgressDialog (created visually distribute the component on the form) by passing the label and the parameters of the progressbar.
C#
public partial class ProgressBar : Form
    {
        MyNew Breack;
        public void setProgressBarLimit(int SomeCount)
        {
            this.progressBarStatus.Maximum = SomeCount + 2;
        }
        public void updateStatus(string statusText, bool updateBar)
        {
            if (updateBar)
            {
                if (this.progressBarStatus.Value < this.progressBarStatus.Maximum)
                {
                    this.progressBarStatus.Value++;
                }
                this.progressBarStatus.Update();
            }
            if (statusText != "")
            {
                this.labelSatus.Text = statusText;
            }
            this.labelSatus.Update();
        }
        public ProgressBar()
        {
            InitializeComponent();
        }


How can I unfreeze a form ProgressDialog?
The fact that my class, I also call from another class. That is, this scheme
Class 1-> MyNew-> Progressbar
If I call the class MyNew a new thread, then the form ProgressDialog is not be displayed.
How to stuff a progressbar directly backgroundWorker also do not understand, because to update the form I use is not something running in the DoWork function with parameters and updateStatus and there is already decides how to update the progress bar on the form.
May not go that way?
Posted
Updated 9-Jan-12 1:11am
v2

1 solution

I don't really understand the question. (I'm guessing English isn't your first language? I find it hard to read what you've typed.) But the problem here appears to be that you are mixing the calculation code and display code. The code that performs the calculation (which appears to be MyNew, I hope that is just a name you've anonymised for posting here!) shouldn't care about how information about the calculation is displayed, apart from providing notifying properties (i.e. implement INotifyPropertyChanged and fire the PropertyChanged event on the relevant properties when they change) or events to tell whatever view is applied to it to update itself.

You can then, separately, in your UI code, when you press the Go button (or however this calculation is spawned) create a progress bar and either bind it to the property of MyNew you created, or bind an event handler to the event you provided which updates the progress bar (depending on which method you chose).
 
Share this answer
 
Comments
skilllab 9-Jan-12 7:49am    
yes, sorry for my English.
Let me try to explain:
I have a 2 class (CLASS1, CLASS2) + form (FORM1).
In CLASS1 I call CLASS2, that create FORM1 and periodically updates its.
There are suspicions that I'm doing something wrong.
Maybe I should do this:
In CLASS1 create FORM1 and call CLASS1 separatly.
In this case, how to update the progress bar and label on the FORM1 from CLASS2?
BobJanova 9-Jan-12 8:56am    
Yes, you should do that. Have CLASS2 expose a Progress property and implement INotifyPropertyChanged:

class CLASS2 : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
protected void Notify(string property){
PropertyChangedEventHandler h = PropertyChanged;
if(h != null) h(this, new PropertyChangedEventArgs(property));
}

private float progress = 0f;
public float Progress { get { return progress; } set { progress = value; Notify("Progress"); } }

... // your calculation code

void SomeMethod(){
Progress = 0.6f; // This will cause an update
}
}

Then, you want to bind your progress bar to the CLASS2 you create. One way would be to pass it to the form constructor, e.g.

class Form1 : Form {
// ...

public Form1(Class2 source){
InitializeComponent();
this.progressBar1.DataBindings.Add("Value", source, "Progress");
}
}

You can use a similar approach for binding Enabled to have some controls inactive when a calculation is ongoing.
skilllab 10-Jan-12 6:31am    
I tried to rewrite your code under my program and I did not succeed.
Can you help me with the project http://zalil.ru/32463320
I'm still new to programming
The problem is the same, unfreeze form 2
BobJanova 10-Jan-12 15:04pm    
If you're new to programming you probably don't want to be trying to write a multithreaded application just yet.
skilllab 10-Jan-12 15:09pm    
Why?
I gave my example is very simple code. Understand your advice - I will learn this technique.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900