D
dgleeson3
Hello All
Im having lots of fun with window handles and invoke.
The code started off in a single class. Main thread set up a worker
thread and the worker thread updated the progress bar on form1 (which
only has a progress bar and a button). All worked well - No problems.
The implementation then changed to 2 classes. This code is given
below.
First problem was with beginInvoke. An exception was being generated
saying that
"Invoke or BeginInvoke cannot be called on a control until the window
handle has been created."
So to ensure I had a handle to the progress bar I simply referenced
the handle in the constructor see
winhandle = ProgressBar1.Handle
This solved this problem. Though Im not sure whats going on.
Now Im on the next problem. When BeginInvoke is called as in
" result = Form1.ProgressBar1.BeginInvoke(New
InvokeDelegate(AddressOf Form1.UpdateProgressDisplay), 25)"
and when Im debugging the code I find myself stepping through the
constructor for Form1 again. Its like as if the form1 object has been
removed and a reference to it is causing it to be created again.
Iv tried " Public Shared ProgressBar1 As New ProgressBar" in an
effort to see if the form1 object is being removed. And ive passed the
form1 to the second class in its constructor. All to no avail.
Any Gurus able to point me in the right direction.
-----------------------------------------------------------------------------------------------------------
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Threading
Imports System.IO
Public Class Form1
'Dim Communications_thread As New Thread(AddressOf
DoRemoteCommunications)
Dim worker As System.Threading.Thread ' Thread
Dim worker_obj As New worker_class(Me) ' Object
from worker class
' Set up delegate for assync function call.
--------------------------------
'Public Delegate Sub Async_Update_Progress_caller(ByVal
Value_for_progress_bar As Integer)
Public Shared ProgressBar1 As New ProgressBar
'Delegate Sub InvokeDelegate()
Public winhandle As IntPtr
Public Sub New()
Me.InitializeComponent()
'ProgressBar1
'
Me.SuspendLayout()
ProgressBar1 = New System.Windows.Forms.ProgressBar
ProgressBar1.Name = "ProgressBar1"
ProgressBar1.Maximum = 100
ProgressBar1.Value = 10
ProgressBar1.Location = New System.Drawing.Point(69, 120)
ProgressBar1.Size = New System.Drawing.Size(145, 23)
ProgressBar1.TabIndex = 0
' To have handle for progress bar available. Othyerwise
beginInvoke fails because handle is not available.
' I think this may actually be creating the handle, which may
not exist otherwise.
winhandle = ProgressBar1.Handle
Me.Controls.Add(ProgressBar1)
Me.ResumeLayout(False)
End Sub
Public Sub UpdateProgressDisplay(ByVal Value_for_progress_bar As
Integer)
ProgressBar1.Value = Value_for_progress_bar
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
worker = New Thread(AddressOf
worker_obj.DoRemoteCommunications)
' Now actually start the comms thread.
worker.Start()
End Sub
End Class
Public Class worker_class
Delegate Sub InvokeDelegate(ByVal temp As Integer)
Public form_reference As Form
Public Sub New(ByVal form_ref)
form_reference = form_ref
End Sub
Public Sub DoRemoteCommunications()
Console.WriteLine("Comms worker thread started.....")
' Initiate the asynchronous call.
Dim result As IAsyncResult
result = Form1.ProgressBar1.BeginInvoke(New
InvokeDelegate(AddressOf Form1.UpdateProgressDisplay), 25)
Console.WriteLine("Progress value 25 passed ")
Thread.Sleep(3000)
result = Form1.ProgressBar1.BeginInvoke(New
InvokeDelegate(AddressOf Form1.UpdateProgressDisplay),
Form1.ProgressBar1.Maximum)
Console.WriteLine("Comms worker thread Finished.")
End Sub
End Class
-----------------------------------------------------------------------------------------------------
Thanks
Denis
____________________
http://www.CentronSolutions.com
Im having lots of fun with window handles and invoke.
The code started off in a single class. Main thread set up a worker
thread and the worker thread updated the progress bar on form1 (which
only has a progress bar and a button). All worked well - No problems.
The implementation then changed to 2 classes. This code is given
below.
First problem was with beginInvoke. An exception was being generated
saying that
"Invoke or BeginInvoke cannot be called on a control until the window
handle has been created."
So to ensure I had a handle to the progress bar I simply referenced
the handle in the constructor see
winhandle = ProgressBar1.Handle
This solved this problem. Though Im not sure whats going on.
Now Im on the next problem. When BeginInvoke is called as in
" result = Form1.ProgressBar1.BeginInvoke(New
InvokeDelegate(AddressOf Form1.UpdateProgressDisplay), 25)"
and when Im debugging the code I find myself stepping through the
constructor for Form1 again. Its like as if the form1 object has been
removed and a reference to it is causing it to be created again.
Iv tried " Public Shared ProgressBar1 As New ProgressBar" in an
effort to see if the form1 object is being removed. And ive passed the
form1 to the second class in its constructor. All to no avail.
Any Gurus able to point me in the right direction.
-----------------------------------------------------------------------------------------------------------
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Threading
Imports System.IO
Public Class Form1
'Dim Communications_thread As New Thread(AddressOf
DoRemoteCommunications)
Dim worker As System.Threading.Thread ' Thread
Dim worker_obj As New worker_class(Me) ' Object
from worker class
' Set up delegate for assync function call.
--------------------------------
'Public Delegate Sub Async_Update_Progress_caller(ByVal
Value_for_progress_bar As Integer)
Public Shared ProgressBar1 As New ProgressBar
'Delegate Sub InvokeDelegate()
Public winhandle As IntPtr
Public Sub New()
Me.InitializeComponent()
'ProgressBar1
'
Me.SuspendLayout()
ProgressBar1 = New System.Windows.Forms.ProgressBar
ProgressBar1.Name = "ProgressBar1"
ProgressBar1.Maximum = 100
ProgressBar1.Value = 10
ProgressBar1.Location = New System.Drawing.Point(69, 120)
ProgressBar1.Size = New System.Drawing.Size(145, 23)
ProgressBar1.TabIndex = 0
' To have handle for progress bar available. Othyerwise
beginInvoke fails because handle is not available.
' I think this may actually be creating the handle, which may
not exist otherwise.
winhandle = ProgressBar1.Handle
Me.Controls.Add(ProgressBar1)
Me.ResumeLayout(False)
End Sub
Public Sub UpdateProgressDisplay(ByVal Value_for_progress_bar As
Integer)
ProgressBar1.Value = Value_for_progress_bar
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
worker = New Thread(AddressOf
worker_obj.DoRemoteCommunications)
' Now actually start the comms thread.
worker.Start()
End Sub
End Class
Public Class worker_class
Delegate Sub InvokeDelegate(ByVal temp As Integer)
Public form_reference As Form
Public Sub New(ByVal form_ref)
form_reference = form_ref
End Sub
Public Sub DoRemoteCommunications()
Console.WriteLine("Comms worker thread started.....")
' Initiate the asynchronous call.
Dim result As IAsyncResult
result = Form1.ProgressBar1.BeginInvoke(New
InvokeDelegate(AddressOf Form1.UpdateProgressDisplay), 25)
Console.WriteLine("Progress value 25 passed ")
Thread.Sleep(3000)
result = Form1.ProgressBar1.BeginInvoke(New
InvokeDelegate(AddressOf Form1.UpdateProgressDisplay),
Form1.ProgressBar1.Maximum)
Console.WriteLine("Comms worker thread Finished.")
End Sub
End Class
-----------------------------------------------------------------------------------------------------
Thanks
Denis
____________________
http://www.CentronSolutions.com