how to create Thread?

A

Adda

I have an mdi form and I am trying to call a procedure -
"myTest" in the parent from the childform. I am
experimenting using a thread, but the thread is not
releasing the childform. I place a breakpoint in "myTest"
on the parent form and I have a
msgbox("Done here!") on the childform. The program stops
at the breakpoint in "myTest" but does not continue to the
msgbox until after I have finished with "myTest". Here is
what I have in the childform:
----------------------------------------------
Imports System
Imports System.Threading
....
Public Class childfrm

Private Sub btn_Click(...)...
Dim p As New frmParent
Dim t As New Thread(AddressOf p.myTest())
t.Start()
MsgBox("done here!")
End Sub
End Class
------------------------------------------------
"myTest" runs OK, but not reaching msgbox in childform
until "myTest" is done first.

I also get this message in the output window after running
everything:

The thread '<No Name>' (0xb48) has exited with code 0
(0x0).

Am I using the Thread object correctly? Could someone
tell me how to use Threading correctly here?

TIA
Adda
 
K

Ken Tucker [MVP]

Hi,

Why are you creating a new frmparent. Why dont you try

Dim p as frmParent = Me.Parent

Ken
 
A

Adda

Hi Ken,

I actually tried something like that a few days ago but
could not get the correct syntax (still in the migration
process from vb6). So I tried it with your syntax, but I
was having an implicit conversion problem since I had
Option Strict On. So I turned it off. Now I get an error
that the specified cast is not valid. So I tried

Dim p as frmParent = CType(Me.Parent,
System.Windows.Forms.Form)

but still having problem with specified cast not valid. I
appologize for my newbieness with .Net (just know that I
hate having to go through the amateur phase all over
again :).

Adda
 
S

Sameh Ahmed

Hello Adda
Try Dim p as NEW frmParent = CType(Me.Parent, System.Windows.Forms.Form)
 
K

Ken Tucker [MVP]

Hi,

Dim p as frmParent = CType(Me.Parent, frmParent)

Ken
---------------------
 
A

Adda

Well, I tried this but still getting the casting problem.
The only thing that is working here is

Dim p as New frmParent

My goal is to be able to refresh the datagrid in the
parent form from the childform. I'm sure there is a way,
but I just don't have the proficiency yet with vb.net to
intuit how to accomplish this. Maybe API code? It seems
like only adding data to the dataset from the parent form
does the trick. How can I trick the parent form from the
childform? Oh well, back to the drawing board.

But I thank everyone for all the replies and suggestions.

Adda
 
C

Cor Ligthert

Hi Adda,

I was curious if this would work.

I think that it does as you wanted?

(Most code is to build the sample table)

Cor

\\\Mdiform
Private Sub FormMdi_Load(ByVal _
sender As Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
dt.CreateTable()
Dim frm1 As New Form1
frm1.MdiParent = Me
frm1.Show()
Dim frm2 As New Form2
frm2.MdiParent = Me
frm2.Show()
AddHandler dt.dvchanged, AddressOf myrefresh
End Sub
Private Sub myrefresh()
Me.Refresh()
End Sub
///
\\\2 forms form1 and form2 both the same
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
DataGrid1.DataSource = dt.dv
End Sub
///
\\\Class to build a sample table as and to set the events
Public Class dt
Public Shared WithEvents dv As DataView
Public Shared Event dvchanged()
Public Shared Sub CreateTable()
Dim dt As New DataTable("Sample")
For i As Integer = 0 To 7
Dim dc As New DataColumn(Chr(i + 65))
dt.Columns.Add(dc)
Next
For i As Integer = 0 To 8
dt.Rows.Add(dt.NewRow)
For y As Integer = 0 To dt.Columns.Count - 1
dt.Rows(i)(y) = (Chr(i + 48))
Next
Next
dv = New DataView(dt)
End Sub
Private Shared Sub dv_ListChanged(ByVal sender _
As Object, ByVal e As System.ComponentModel.ListChangedEventArgs) _
Handles dv.ListChanged
RaiseEvent dvchanged()
End Sub
End Class
///
 
K

Ken Tucker [MVP]

Hi,

Dim p as frmParent = DirectCast(Me.MdiParent, frmParent)

Ken
--------------
 
A

Adda

Thank you. This is an interesting approach. I did try it
and got a message that an mdiparent can't be a parent and
also a child. I will experiment with this. Here is the
truth. I am taking a class in ADO.net using VB.net (or
C#). The instructor has a solution, but I won't know it
(until it is too late :). The only thing I can really
think of to evoke something in the parent form would be to
show the childform as a dialog form. But I tried that
only to get the message that only top level forms can be
showed as dialog. In the meantime, I can always read the
contents of the childform from a menu on the parent form.
My instructions are not completely clear (to me) but the
idea is that I add data to a dataset on the parent form
but by clicking a button on the childform and on the
update (from the childform) the datagrid gets refreshed
(also from the button click on the childform).

Adda



-----Original Message-----
Hi Adda,

I was curious if this would work.

I think that it does as you wanted?

(Most code is to build the sample table)

Cor

\\\Mdiform
Private Sub FormMdi_Load(ByVal _
sender As Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
dt.CreateTable()
Dim frm1 As New Form1
frm1.MdiParent = Me
frm1.Show()
Dim frm2 As New Form2
frm2.MdiParent = Me
frm2.Show()
AddHandler dt.dvchanged, AddressOf myrefresh
End Sub
Private Sub myrefresh()
Me.Refresh()
End Sub
///
\\\2 forms form1 and form2 both the same
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
DataGrid1.DataSource = dt.dv
End Sub
///
\\\Class to build a sample table as and to set the events
Public Class dt
Public Shared WithEvents dv As DataView
Public Shared Event dvchanged()
Public Shared Sub CreateTable()
Dim dt As New DataTable("Sample")
For i As Integer = 0 To 7
Dim dc As New DataColumn(Chr(i + 65))
dt.Columns.Add(dc)
Next
For i As Integer = 0 To 8
dt.Rows.Add(dt.NewRow)
For y As Integer = 0 To dt.Columns.Count - 1
dt.Rows(i)(y) = (Chr(i + 48))
Next
Next
dv = New DataView(dt)
End Sub
Private Shared Sub dv_ListChanged(ByVal sender _
As Object, ByVal e As
System.ComponentModel.ListChangedEventArgs) _
 
C

Cor Ligthert

Hi Adda,

I did test that sample and did not get an error, did you set the mdi = true
in the mdi form that I did using the designer?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top