S
Sam
Hi,
I've written the following, inspired by a MSDN article.
First I want to know if what I do is correct. The reason I'm asking is
because if the value passed to CalcPi is great(eg.10000) then I can't
interact with the UI anymore (resize window, move window...)
Second, I don't know how to close Form2. If I do frm.Close in CalcPi
after the loop, I get the exception:
Cross-thread operation not valid: Control 'Form2' accessed from a
thread other than the thread it was created on.
Quick overview of the app:
I create a delegate to open a new form when I click on a button
The new form creates a delegate of CalcPi and update a progressbar
CalcPi does a loop from 0 to X and display "test" each time in a
textbox.
Can you help?
Thanks
CODE:
Public Class Form1
Dim frm As Form2
Dim sf As ShowFormDelegate
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
sf = New ShowFormDelegate(AddressOf ShowForm)
sf.BeginInvoke(Nothing, Nothing)
End Sub
Public Delegate Sub CalcPiDelegate(ByVal digits As Integer)
Public Sub CalcPi(ByVal digits As Integer)
Dim pi As StringBuilder = New StringBuilder)
ShowProgress(pi.ToString, digits, 0)
If (digits > 0) Then
pi.Append(".")
For i As Integer = 0 To digits
pi.Append("test")
ShowProgress(pi.ToString, digits, i)
Next
'RAISED EXCEPTION HERE
frm.Close()
End If
End Sub
Delegate Sub ShowProgressDelegate(ByVal pi As String, ByVal
totalDigits As Integer, ByVal digitSoFar As Integer)
Public Sub ShowProgress(ByVal pi As String, ByVal totalDigits As
Integer, ByVal digitSoFar As Integer)
If Not piText.InvokeRequired Then
piText.Text = pi
frm.ProgressBar2.Maximum = totalDigits
frm.ProgressBar2.Value = digitSoFar
Else
Dim sp As New ShowProgressDelegate(AddressOf ShowProgress)
Dim ar As New ArrayList
ar.Add(pi.ToString)
ar.Add(totalDigits)
ar.Add(digitSoFar)
BeginInvoke(sp, ar.ToArray)
End If
End Sub
Delegate Sub ShowFormDelegate()
Public Sub ShowForm()
If Not InvokeRequired Then
frm = New Form2(Me)
frm.ShowDialog()
Else
Dim sf As New ShowFormDelegate(AddressOf ShowForm)
BeginInvoke(sf)
End If
End Sub
End Class
---------------------------------------------------------------
Public Class Form2
Private Sub Form2_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim cp As New Form1.CalcPiDelegate(AddressOf Form1.CalcPi)
cp.BeginInvoke(1000, Nothing, Nothing)
End Sub
End Class
I've written the following, inspired by a MSDN article.
First I want to know if what I do is correct. The reason I'm asking is
because if the value passed to CalcPi is great(eg.10000) then I can't
interact with the UI anymore (resize window, move window...)
Second, I don't know how to close Form2. If I do frm.Close in CalcPi
after the loop, I get the exception:
Cross-thread operation not valid: Control 'Form2' accessed from a
thread other than the thread it was created on.
Quick overview of the app:
I create a delegate to open a new form when I click on a button
The new form creates a delegate of CalcPi and update a progressbar
CalcPi does a loop from 0 to X and display "test" each time in a
textbox.
Can you help?
Thanks
CODE:
Public Class Form1
Dim frm As Form2
Dim sf As ShowFormDelegate
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
sf = New ShowFormDelegate(AddressOf ShowForm)
sf.BeginInvoke(Nothing, Nothing)
End Sub
Public Delegate Sub CalcPiDelegate(ByVal digits As Integer)
Public Sub CalcPi(ByVal digits As Integer)
Dim pi As StringBuilder = New StringBuilder)
ShowProgress(pi.ToString, digits, 0)
If (digits > 0) Then
pi.Append(".")
For i As Integer = 0 To digits
pi.Append("test")
ShowProgress(pi.ToString, digits, i)
Next
'RAISED EXCEPTION HERE
frm.Close()
End If
End Sub
Delegate Sub ShowProgressDelegate(ByVal pi As String, ByVal
totalDigits As Integer, ByVal digitSoFar As Integer)
Public Sub ShowProgress(ByVal pi As String, ByVal totalDigits As
Integer, ByVal digitSoFar As Integer)
If Not piText.InvokeRequired Then
piText.Text = pi
frm.ProgressBar2.Maximum = totalDigits
frm.ProgressBar2.Value = digitSoFar
Else
Dim sp As New ShowProgressDelegate(AddressOf ShowProgress)
Dim ar As New ArrayList
ar.Add(pi.ToString)
ar.Add(totalDigits)
ar.Add(digitSoFar)
BeginInvoke(sp, ar.ToArray)
End If
End Sub
Delegate Sub ShowFormDelegate()
Public Sub ShowForm()
If Not InvokeRequired Then
frm = New Form2(Me)
frm.ShowDialog()
Else
Dim sf As New ShowFormDelegate(AddressOf ShowForm)
BeginInvoke(sf)
End If
End Sub
End Class
---------------------------------------------------------------
Public Class Form2
Private Sub Form2_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim cp As New Form1.CalcPiDelegate(AddressOf Form1.CalcPi)
cp.BeginInvoke(1000, Nothing, Nothing)
End Sub
End Class