Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > ProgressBar Thread
 

ProgressBar Thread

Am I missing something here on the TaskVision sample. My understanding about threads is that a new Thread should not modify any control that was not created on that thread. TheShowProgressBar method is instantiatingthe ProgressBar on the UI Thread, then creating a newbackground Thread which is updating the ProgressBar. My understanding is that the background thread should use the Invoke() Method on the ProgressBar to update it's value....
Techno_Dex  Monday, April 10, 2006 4:07 PM
I think you right, this is not good design. It may work for some time, but there is no guarantee that this will not make problems. MSDN about ProgressBar do not promise thread safety.
Sergey Galich  Monday, April 10, 2006 8:15 PM

Hi!

I don't have that source right now, can you post here part of code that confuse you?

Sergey Galich  Monday, April 10, 2006 4:19 PM

private void ShowProgressBar(string message)

{

// if the progressbar doesn't exist then create it

if (m_ProgressBar == null)

{

m_ProgressBar = new ProgressBar();

sbrBottom.Controls.Add(m_ProgressBar);

m_ProgressBar.Width = 100;

m_ProgressBar.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;

m_ProgressBar.Height = 16;

m_ProgressBar.Location = new System.Drawing.Point((sbrPanelLeft.Width - m_ProgressBar.Width) - 10, ((sbrBottom.Height - m_ProgressBar.Height) / 2) + 1);

}

sbrPanelLeft.Text = message;

m_ProgressBar.Visible = true;

m_ProgressBar.Value = 0;

//NOTE: CRH - THIS IS BAD DESIGN AS THE THREAD IS UPDATING THE PROGRESSBAR ON A THREAD THAT IS NOT THE CREATING THREAD (I.E. THE UI THREAD)

// start a new thread to increment the progressbar

Thread progressThread = new Thread(new ThreadStart(this.BackGroundIncrementProgressBar));

progressThread.IsBackground = true;

progressThread.Name = "Progress Bar";

progressThread.Start();

}

private void BackGroundIncrementProgressBar()

{

// note: this is run on a worker thread

while (m_ProgressBar.Value != 100)

{

if (m_ProgressBar.Value < 80)

m_ProgressBar.Value += 8;

Thread.Sleep(100);

}

}

Techno_Dex  Monday, April 10, 2006 7:59 PM
I think you right, this is not good design. It may work for some time, but there is no guarantee that this will not make problems. MSDN about ProgressBar do not promise thread safety.
Sergey Galich  Monday, April 10, 2006 8:15 PM
hi
very good.
i have a question!
what is this method : this.progressBar1.Invoke(Delegate Method);

سلام
خیلی خوب بود
من یک سئوال داشتم
کار متد زیرچیست ؟
this.progressBar1.Invoke(Delegate Method);

علی مخلصی  Monday, July 07, 2008 10:24 AM

You can use google to search for other answers

Custom Search

More Threads

• who konws when dose sqlconn in ivdata.cs init?
• Need Help
• How to Set Tooltip to Combobox Item at Binding Time in vb.net
• Can't load the Deer.dll to Terrarium game
• copy file from one folder another
• IssueVision Issue
• Terrarium Templates Visual Studio 2005
• I don't think there is a problem with TaskVision ...
• Can I write animal in managed C++?
• When's Terrarium going to work?