public partial class Form1 : System.Windows.Form
{
private System.Windows.TextBox mySpecialTB;
private void button1_Click(object sender, System.EventArgs e)
{
this.mySpecialTB.Text = "Hello World!";
}
}
VB
Public Class Form1
Private mySpecialTB As TextBox
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.mySpecialTB.Text = "Hello World!"
End Sub
End Class
public partial class Form1 : System.Windows.Form
{
private MySpecialClass msc;
private void button1_Click(object sender, System.EventArgs e)
{
this.msc = new MySpecialClass();
System.Windows.MessageBox.Show(msc.MySpecialTextBox.Text);
}
}
public class MySpecialClass
{
private System.Windows.TextBox mySpecialTB;
public System.Windows.TextBox MySpecialTextBox { get { return this.mySpecialTB; } }
}
VB
Public Class Form1
Private msc As MySpecialClass
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.msc = New MySpecialClass()
MessageBox.Show(Me.msc.MySpecialTextBox.Text)
End Sub
End Class
Public Class MySpecialClass
Private mySpecialTB As TextBox
Public ReadOnly Property MySpecialTextBox() As TextBox
Get
Return Me.mySpecialTB
End Get
End Property
End Class
private void button1_Click(object sender, System.EventArgs e)
{
string test = "Hello World!";
int converted = Convert.ToInt32(test);
}
VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim test As String = "Hello World!"
Dim converted As Integer = Convert.ToInt32(test)
End Sub
string test = "Hello World!";
int converted;
if (int.TryParse(test, out converted))
{
// Conversion succeeded!
}
VB
Dim test As String = "Hello World!"
Dim converted As Integer
If Integer.TryParse(test, converted) Then
' Conversion succeeded!
End If
class Program
{
private static int hitCount;
static void Main(string[] args)
{
hitCount = 0;
MyRecursiveExample(null);
}
static void MyRecursiveExample(string value)
{
hitCount++;
if (value == null)
{
MyRecursiveExample(value);
}
else
{
System.Console.WriteLine(value);
}
}
}
VB
Module Module1
Private hitCount As Long
Sub Main()
hitCount = 0
MyRecursiveExample(Nothing)
End Sub
Sub MyRecursiveExample(ByVal value As String)
hitCount += 1
If value Is Nothing Then
MyRecursiveExample(value)
Else
Console.WriteLine(value)
End If
End Sub
End Module
This example is an oversimplification--most likely, you will have more complex logic than this causing the error, but you should get an idea of what to look for. In the above example, you can see that "value", as defined by my logic, can never not be null. As such, the function will recurse indefinately. Well, not indefinately: until you get a StackOverflowException! I've included "hitCount" to show you how "long" it takes to get to this point. (The count you see in the images is not fixed--it will be different based on your logic.)
class MyExampleClass
{
private string _somePrivateMember;
public string MyProperty
{
get { return this.MyProperty; }
set { this.MyProperty = value; }
}
}
VB
Class MyExampleClass
Private _somePrivateMember As String
Public Property MyProperty() As String
Get
Return Me.MyProperty
End Get
Set(ByVal value As String)
Me.MyProperty = value
End Set
End Property
End Class
Did you catch it? The problem with the above is that within the property, the property itself is being referred to in the get and set operations. This results in an endless loop, and susequently an overlfow of the stack:
public partial class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
private void startButton_Click(object sender, System.EventArgs e)
{
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(RunCustomLogic), "Done");
}
private void RunCustomLogic(object o)
{
for (int i = 0; i < 100; i++)
{
System.Threading.Thread.Sleep(10);
}
this.resultsBox.Text = o.ToString();
}
}
VB
Public Class Form1
Private Sub startButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startButton.Click
System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf RunCustomLogic), "Done")
End Sub
Sub RunCustomLogic(ByVal o As Object)
For i As Integer = 0 To 99
System.Threading.Thread.Sleep(10)
Next
Me.resultsBox.Text = o.ToString()
End Sub
End Class
In the above code, RunCustomLogic represents the logic I have forked off to another thread via the ThreadPool. I have used a "for" loop to simulate a lengthy operation. You will notice after the "for" loop terminates, I am attempting to print the message passed as a parameter to RunCustomLogic. This is what causes InvalidOperationException to rear its ugly head, as seen in the following images:
public partial class Form1 : System.Windows.Forms.Form
{
private delegate void MyUpdaterDelegate(string value);
public Form1()
{
InitializeComponent();
}
private void startButton_Click(object sender, System.EventArgs e)
{
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(RunCustomLogic), "Done");
}
private void RunCustomLogic(object o)
{
for (int i = 0; i < 100; i++)
{
System.Threading.Thread.Sleep(10);
}
UpdateResultsBox(o.ToString());
}
private void UpdateResultsBox(string value)
{
if (this.resultsBox.InvokeRequired)
{
MyUpdaterDelegate del = UpdateResultsBox;
this.resultsBox.Invoke(del, value);
}
else
{
this.resultsBox.Text = value;
}
}
}
VB
Public Class Form1
Private Delegate Sub MyUpdaterDelegate(ByVal value As String)
Private Sub startButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startButton.Click
System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf RunCustomLogic), "Done")
End Sub
Sub RunCustomLogic(ByVal o As Object)
For i As Integer = 0 To 99
System.Threading.Thread.Sleep(10)
Next
UpdateResultBox(o.ToString())
End Sub
Sub UpdateResultBox(ByVal value As String)
If Me.resultsBox.InvokeRequired Then
Dim del As MyUpdaterDelegate = AddressOf UpdateResultBox
Me.resultsBox.Invoke(del, value)
Else
Me.resultsBox.Text = value
End If
End Sub
End Class
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)