Need to add control from one thread into the control collection of a form in another thread...

  • Thread starter Thread starter Jameson
  • Start date Start date
Jameson said:
...is this possible?

You'd better put the question in the body not in the subject line.



Answer: Yes. Call the form's invoke/begininvoke method. In the procedure
called by invoke/begininvoke, add the control.


Armin
 
Jameson,

I am always curious from people what they are doing, this newsgroup is to
learn for everybody you know.

A control is a kind of UI.

How do your users enter data Assynchonously in those controls?

Cor
 
Lets say my form1 needs to perform some operation and I designed a custom
control that will show the user some information about the operation. Well
For the sake of seamlessness I would rather have that control added to the
control list of form1 and have it run on its own thread so that the user can
see the updates happening, and the operations going on in form1 will not be
effected.

My form1 is docked to the top of the users screen, so I do not have to worry
about the user being able to move it or interact with it in any way while
the operation is underway.
 
Small example maybe?
I looked up some articles but I'm not seeing much that sounds like what I am
trying to do.
 
Jameson said:
Small example maybe?
I looked up some articles but I'm not seeing much that sounds like
what I am trying to do.


f.Invoke(New MethodInvoker(AddressOf f.YourMethod))

while f is the Form reference, and YourMethod is the method adding the
control. However, I'd raise a neutral event instead that is handled by the
Form and excutes the line above (replace 'f' by 'Me'). One shouldn't care
about the UI in a worker thread (IMO).


Armin
 
MainForm.Invoke(New MethodInvoker(AddressOf MainForm.Controls.Add))

Yeah that doesn't work and I know it's not what you told me to do, lol I'm
sorry. Its as if I am wanting to do this.


I have form1, it is going to do some calculations for a bit, I cannot escape
that fact as form1 is my main application form. What I want to do is create
a new thread that will add a control to form1 (that covers the entire form)
and display a bussy animation. Problem is VB wont let me add a control to a
form on a different thread. I'm certain what you gave me will work I guess
I just do not understand what I am supposed to do.

This is my code, Mainform is essentially form1 as described above.

'simple code to display bussy notification
Public mrg_p As wait
Private t As Threading.Thread



Public Sub show_bussy()
t = New Threading.Thread(AddressOf bussy)
t.Start()
End Sub
Private Sub bussy()
If mrg_p IsNot Nothing Then mrg_p.Dispose()
mrg_p = New wait
'MainForm.Controls.Add(mrg_p)
' f.Invoke(New MethodInvoker(AddressOf f.YourMethod))
MainForm.Invoke(New MethodInvoker(AddressOf MainForm.Controls.Add))

MainMarque.Visible = False
With mrg_p
.Top = 0
.Left = 0
.Dock = DockStyle.Fill
End With
MakeWindowAlwaysTop(mrg_p.Handle.ToInt32)
mrg_p.Show()
mrg_p.Visible = True
End Sub

Public Sub hide_bussy()
t.Abort()
End Sub
 
Well I tried this...Which worked but it defeats the purpose because it
places the control under mainform's thread instead of the new one I made and
the animation doesn't run, also the thread abort command never fires.


Private Sub bussy()
If mrg_p IsNot Nothing Then mrg_p.Dispose()
mrg_p = New wait
'MainForm.Controls.Add(mrg_p)
' f.Invoke(New MethodInvoker(AddressOf f.YourMethod))
MainForm.Invoke(New MethodInvoker(AddressOf add_bussy))
MainMarque.Visible = False
With mrg_p
.Top = 0
.Left = 0
.Dock = DockStyle.Fill
End With
MakeWindowAlwaysTop(mrg_p.Handle.ToInt32)
mrg_p.Show()
mrg_p.Visible = True
End Sub
Private Sub add_bussy()
MainForm.Controls.Add(mrg_p)
End Sub
 
Back
Top