Friday, April 9, 2010
Connection String examples in vb.net and C#.net
http://www.dofactory.com/Connect/Connect.aspx#_self11
Thursday, April 8, 2010
C# Cross-Thread Operations Cross-Thread Operations
C# Cross-Thread Operations
Cross-Thread Operations
A cross-thread operation in C# is a call that accesses components from a different thread. Starting with .NET Framework 2.0, it is no longer optional to make proper cross-thread operations, it is a requirement. So we are going to learn how to make cross-thread calls between controls.
For example, if you try to call a Form function from a seperate thread, you will get an error message similar to:
Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on.
Delegates
The answer is simply to use delegates to invoke methods that use cross-thread operations. Delegates are an elegant way to call methods that are from other threads.
Methods
For our example, we need two general methods: one to keep the loop running, and one to perform the single cross-thread operation. The whole point of even using seperate a Thread is have a thread specialized in updating part of the application without disturbing the rest.
The example, which you can download at the bottom of the page, will alternate the colors of a Panel to create a sort of blinker. By using a Thread, the Panel can be constantly updated without locking up the rest of the user interface.
InvokeRequired
The key for cross-thread calls is in the InvokeRequired property. When cross-threading will be required, the property will be true. You can then use delegates to invoke the method properly:
if (this.InvokeRequired)
{
BlinkDelegate del = new BlinkDelegate(Blink); //delegate
object[] parameters = { secondsInterval }; //parameters
this.Invoke(del, parameters); //call
}
else
{
//What you want done goes here
}
Notice that the C# function is simply calling itself if invoke is required.
Cross-Thread Operations
A cross-thread operation in C# is a call that accesses components from a different thread. Starting with .NET Framework 2.0, it is no longer optional to make proper cross-thread operations, it is a requirement. So we are going to learn how to make cross-thread calls between controls.
For example, if you try to call a Form function from a seperate thread, you will get an error message similar to:
Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on.
Delegates
The answer is simply to use delegates to invoke methods that use cross-thread operations. Delegates are an elegant way to call methods that are from other threads.
Methods
For our example, we need two general methods: one to keep the loop running, and one to perform the single cross-thread operation. The whole point of even using seperate a Thread is have a thread specialized in updating part of the application without disturbing the rest.
The example, which you can download at the bottom of the page, will alternate the colors of a Panel to create a sort of blinker. By using a Thread, the Panel can be constantly updated without locking up the rest of the user interface.
InvokeRequired
The key for cross-thread calls is in the InvokeRequired property. When cross-threading will be required, the property will be true. You can then use delegates to invoke the method properly:
if (this.InvokeRequired)
{
BlinkDelegate del = new BlinkDelegate(Blink); //delegate
object[] parameters = { secondsInterval }; //parameters
this.Invoke(del, parameters); //call
}
else
{
//What you want done goes here
}
Notice that the C# function is simply calling itself if invoke is required.
Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on
Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on PDF Print E-mail
Written by Shabdar
Tuesday, 25 November 2008 12:28
You will get following error when you try to update a windows form control from a separate thread.
"Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on."
This article guides you on how to overcome this problem.
Problem
To reproduce this error, insert a progress bar control (progressbar1) on your form and insert a button(btnStart).
private void btnStart_Click(object sender, EventArgs e)
{
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
System.Threading.Thread t1 = new System.Threading.Thread(startProgress);
t1.Start();
}
void startProgress()
{
for (int i = 0; i {
progressBar1.Value = i; //You will get error at this line
System.Threading.Thread.Sleep(100);
}
}
window.google_render_ad();
Solution
private void btnStart_Click(object sender, EventArgs e)
{
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
System.Threading.Thread t1 = new System.Threading.Thread(startProgress);
t1.Start();
}
void startProgress()
{
for (int i = 0; i {
SetControlPropertyValue(progressBar1, "value", i); //This is a thread safe method
System.Threading.Thread.Sleep(100);
}
}
delegate void SetControlValueCallback(Control oControl, string propName, object propValue);
private void SetControlPropertyValue(Control oControl, string propName, object propValue)
{
if (oControl.InvokeRequired)
{
SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue);
oControl.Invoke(d, new object[] { oControl, propName, propValue });
}
else
{
Type t = oControl.GetType();
PropertyInfo[] props = t.GetProperties();
foreach (PropertyInfo p in props)
{
if (p.Name.ToUpper() == propName.ToUpper())
{
p.SetValue(oControl, propValue, null);
}
}
}
}
You can apply same solution to any windows control. All you have to do is, copy SetControlValueCallback delegate and SetControlPropertyValue function from above code. For example if you want to set property of a label, use SetControlPropertyValue function.
SetControlPropertyValue(Label1, "Text", i.ToString());
Make sure you supply property value in correct type. In above example Text is a string property. This is why I am converting variable i to string.
Written by Shabdar
Tuesday, 25 November 2008 12:28
You will get following error when you try to update a windows form control from a separate thread.
"Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on."
This article guides you on how to overcome this problem.
Problem
To reproduce this error, insert a progress bar control (progressbar1) on your form and insert a button(btnStart).
private void btnStart_Click(object sender, EventArgs e)
{
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
System.Threading.Thread t1 = new System.Threading.Thread(startProgress);
t1.Start();
}
void startProgress()
{
for (int i = 0; i {
progressBar1.Value = i; //You will get error at this line
System.Threading.Thread.Sleep(100);
}
}
window.google_render_ad();
Solution
private void btnStart_Click(object sender, EventArgs e)
{
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
System.Threading.Thread t1 = new System.Threading.Thread(startProgress);
t1.Start();
}
void startProgress()
{
for (int i = 0; i {
SetControlPropertyValue(progressBar1, "value", i); //This is a thread safe method
System.Threading.Thread.Sleep(100);
}
}
delegate void SetControlValueCallback(Control oControl, string propName, object propValue);
private void SetControlPropertyValue(Control oControl, string propName, object propValue)
{
if (oControl.InvokeRequired)
{
SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue);
oControl.Invoke(d, new object[] { oControl, propName, propValue });
}
else
{
Type t = oControl.GetType();
PropertyInfo[] props = t.GetProperties();
foreach (PropertyInfo p in props)
{
if (p.Name.ToUpper() == propName.ToUpper())
{
p.SetValue(oControl, propValue, null);
}
}
}
}
You can apply same solution to any windows control. All you have to do is, copy SetControlValueCallback delegate and SetControlPropertyValue function from above code. For example if you want to set property of a label, use SetControlPropertyValue function.
SetControlPropertyValue(Label1, "Text", i.ToString());
Make sure you supply property value in correct type. In above example Text is a string property. This is why I am converting variable i to string.
Subscribe to:
Posts (Atom)
