Results 1 to 4 of 4

Thread: Why this code is going wrong

Hybrid View

  1. #1

    Thread Starter
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Question Why this code is going wrong

    Hi, paste this code in Form1

    This is the code:
    VB Code:
    1. Public Delegate Function MessageBoxShowHandler(ByVal test As String) As DialogResult
    2.     Dim del As New MessageBoxShowHandler(AddressOf MessageBox.Show)
    3.  
    4.     Dim WithEvents clsOuterClass As OuterClass
    5.  
    6.     Public Class OuterClass
    7.         Public OuterOutput0 As Short
    8.         Public Event ThrdCmplt0(ByVal OuterOutput0 As Short)
    9.  
    10.         Dim WithEvents clsInnerClass As InnnerClass
    11.  
    12.         Public Sub OuterProcedure()
    13.             clsInnerClass = New InnnerClass()
    14.             Dim Thread0 As New System.Threading.Thread(AddressOf clsInnerClass.InnerProcedure0)
    15.             clsInnerClass.InnerInput0 = 10000
    16.             Thread0.Start()
    17.         End Sub
    18.  
    19.         Sub InnerProcedure0_EventHandler(ByVal InnerOutput0 As Short) Handles clsInnerClass.ThreadComplete0
    20.             OuterOutput0 = InnerOutput0
    21.             RaiseEvent ThrdCmplt0(OuterOutput0)
    22.         End Sub
    23.     End Class
    24.  
    25.     Public Sub Outer_EventHandler(ByVal OuterOutput0 As Short) Handles clsOuterClass.ThrdCmplt0
    26.         del.BeginInvoke(CStr(OuterOutput0), Nothing, Nothing)
    27.     End Sub
    28.  
    29.     Public Class InnnerClass
    30.         Public InnerInput0 As Short
    31.         Public InnerOutput0 As Short
    32.  
    33.         Public Event ThreadComplete0(ByVal InnerOutput0 As Short)
    34.  
    35.         Public Sub InnerProcedure0()
    36.             Dim iCtr0 As Short
    37.             For iCtr0 = 1 To InnerInput0
    38.                 InnerOutput0 = InnerOutput0 + 1
    39.             Next
    40.             RaiseEvent ThreadComplete0(InnerOutput0)
    41.         End Sub
    42.     End Class
    43.  
    44.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    45.         Dim i As Short
    46.         '
    47.         '-->> Why this code don't displays messagebox 5 times <<--
    48.         For i = 1 To 5
    49.             clsOuterClass = New OuterClass()
    50.             Dim Thrd0 As New System.Threading.Thread(AddressOf clsOuterClass.OuterProcedure)
    51.             Thrd0.Start()
    52.         Next
    53.         '^^^^^^^^^^^^^^^^^^^^^^^
    54.     End Sub


    Please help me

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    Change this
    VB Code:
    1. Public Sub Outer_EventHandler(ByVal OuterOutput0 As Short) Handles clsOuterClass.ThrdCmplt0
    2.         del.BeginInvoke(CStr(OuterOutput0), Nothing, Nothing)
    3.     End Sub
    To this
    VB Code:
    1. Public Sub Outer_EventHandler(ByVal OuterOutput0 As Short) Handles clsOuterClass.ThrdCmplt0
    2.      Dim i As Short
    3.  
    4.      For i = 1 To 5
    5.          del.BeginInvoke(CStr(OuterOutput0), Nothing, Nothing)
    6.      Next
    7. End Sub
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  3. #3

    Thread Starter
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Hi Memnoch1207

    Thanks for replying.

    VB Code:
    1. Public Sub Outer_EventHandler(ByVal OuterOutput0 As Short) Handles clsOuterClass.ThrdCmplt0
    2.      Dim i As Short
    3.  
    4.      For i = 1 To 5
    5.          del.BeginInvoke(CStr(OuterOutput0), Nothing, Nothing)
    6.      Next
    7. End Sub
    Above code just displays same output 5 times. What my requirement is, value of OuterOutput0 may differ in each instance of clsOuterClass, so i am using the code below
    VB Code:
    1. Dim i As Short
    2. '
    3. '-->> Why this code don't displays messagebox 5 times <<--
    4. For i = 1 To 5
    5.     clsOuterClass = New OuterClass()
    6.     Dim Thrd0 As New System.Threading.Thread(AddressOf clsOuterClass.OuterProcedure)
    7.     Thrd0.Start()
    8. Next
    9. '^^^^^^^^^^^^^^^^^^^^^^^

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    It only shows one because the first line in the loop:
    clsOuterClass = New OuterClass
    Writes over the previous instance of clsOuterClass before it has raised the event. Also the samething happens in the OuterClass in the OuterProcedure method. The first line resets the inner instance of InnerClass before the even is raised. I'm not sure what real world example this relates to but that is a lot of wicked threading. Try this:
    VB Code:
    1. Public Delegate Function MessageBoxShowHandler(ByVal test As String) As DialogResult
    2.     Dim del As New MessageBoxShowHandler(AddressOf MessageBox.Show)
    3.  
    4.     Dim WithEvents clsOuterClass As New OuterClass 'intialize with class
    5.  
    6.     Public Sub Outer_EventHandler(ByVal OuterOutput0 As Short) Handles clsOuterClass.ThrdCmplt0
    7.         del.BeginInvoke(CStr(OuterOutput0), Nothing, Nothing)
    8.     End Sub
    9.  
    10.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    11.         Dim i As Short
    12.         '
    13.         '-->> Why this code don't displays messagebox 5 times <<--
    14.         For i = 1 To 5
    15.             'clsOuterClass = New OuterClass
    16.             Dim Thrd0 As New System.Threading.Thread(AddressOf clsOuterClass.OuterProcedure)
    17.             Thrd0.Start()
    18.         Next
    19.         '^^^^^^^^^^^^^^^^^^^^^^^
    20.     End Sub
    21.  
    22. Public Class InnnerClass
    23.     Public InnerInput0 As Short
    24.     Public InnerOutput0 As Short
    25.  
    26.     Public Event ThreadComplete0(ByVal InnerOutput0 As Short)
    27.  
    28.     Public Sub InnerProcedure0()
    29.         Dim iCtr0 As Short
    30.         For iCtr0 = 1 To InnerInput0
    31.             InnerOutput0 = InnerOutput0 + 1
    32.         Next
    33.         RaiseEvent ThreadComplete0(InnerOutput0)
    34.     End Sub
    35. End Class
    36.  
    37. Public Class OuterClass
    38.     Public OuterOutput0 As Short
    39.     Public Event ThrdCmplt0(ByVal OuterOutput0 As Short)
    40.  
    41.     Dim WithEvents clsInnerClass As New InnnerClass 'intialize with class
    42.  
    43.     Public Sub OuterProcedure()
    44.         'clsInnerClass = New InnnerClass
    45.         Dim Thread0 As New System.Threading.Thread(AddressOf clsInnerClass.InnerProcedure0)
    46.         clsInnerClass.InnerInput0 = 1000 'I took a zero out to fit within the limits of short
    47.         Thread0.Start()
    48.     End Sub
    49.  
    50.     Sub InnerProcedure0_EventHandler(ByVal InnerOutput0 As Short) Handles clsInnerClass.ThreadComplete0
    51.         OuterOutput0 = InnerOutput0
    52.         RaiseEvent ThrdCmplt0(OuterOutput0)
    53.     End Sub
    54. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width