Results 1 to 15 of 15

Thread: PLZ HELP ME WITH Multithreading

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    182

    Arrow PLZ HELP ME WITH Multithreading

    Im going crazy with this i dont understand at all invokeee i want to run 2 subs at the same time when i click a button check 1 and check 2 are 2 subs when i click a button it calls sub pi which then calls the 2 forms but idk how.. this is the code for callin the 2 subs i want together to run
    vb.net Code:
    1. Public Sub pi()
    2. Dim thrMyThread As New System.Threading.Thread(AddressOf check2)
    3.         thrMyThread.Start()
    4.  
    5.         Dim thrMyThread1 As New System.Threading.Thread(AddressOf check1)
    6.         thrMyThread1.Start()End Sub
    7.  
    8. Private Sub check2()
    9. textbox1.text="12"
    10. end sub
    11.  
    12. Private Sub check1()
    13. textbox2.text="14"
    14. end sub


    i figure it out, i just want to know if im doing it right...cuz apparently both are running at the same time..

    this is the new part in the code

    Code:
    Dim thrMyThread As New System.Threading.Thread(AddressOf check2)
            thrMyThread.Start()
    
            Dim thrMyThread1 As New System.Threading.Thread(AddressOf check1)
            thrMyThread1.Start()End Sub
    Last edited by justme369; Jun 17th, 2010 at 10:16 AM. Reason: i figure it out, i just want to know if im doing it right...cuz apparently both are running at the same time

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: TELL ME IF IM RIGHT OR WRONG HOW TO Multithreading arghhhhh

    That code is successfully starting two new threads, but you should not be directly accessing TextBoxes on those threads. Generally speaking, anything that involves the UI needs to be done on the UI thread. You might start a new thread to perform some long-running task that intermittently invokes a method back on the UI thread to update the UI, e.g. display some text or update a ProgressBar.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    182

    Re: TELL ME IF IM RIGHT OR WRONG HOW TO Multithreading arghhhhh

    the text boxes are an example....wat each thread does its run some codes for 2 separately datagrids

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    182

    Re: TELL ME IF IM RIGHT OR WRONG HOW TO Multithreading arghhhhh

    one thing i notice its that after each thread has finish...they both turn on 1 flag...in the main thread theres a code that checks for both flags..if theyre both on..then the datagrids adjust size,colums..and some buttons turn on..but it gives me an error saying i cant access a thread other then the other it started from...why am i getin this?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    182

    Re: TELL ME IF IM RIGHT OR WRONG HOW TO Multithreading arghhhhh

    this is the error im getin when the 2 threads are done i call another thread and this appears


    Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    182

    Re: TELL ME IF IM RIGHT OR WRONG HOW TO Multithreading arghhhhh

    this is the code

    1 button access the 2 diferent threads,

    i want to execute sub thisisnext when thread mythread1 finish...but i dont know how because i keep getting an error saying i cannot acces one thread other then the thread it was creating from...so how can i do this


    vb.net Code:
    1. Private Sub pingipss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pingipss.Click
    2.         cargaesto()
    3.         staprog.Enabled = False
    4.         stoprog.Enabled = False
    5.         Ipnoreg.Enabled = False
    6.         bck.Enabled = False
    7.         mensaje = True
    8.         If DataGridView1.Visible = False Then
    9.             DataGridView1.Visible = True
    10.             guardaripsactivas.Visible = True
    11.             DataGridView2.Visible = False
    12.             guardaipnoreg.Visible = False
    13.             inventario.Visible = True
    14.         End If
    15.        
    16.         PictureBox1.Visible = True
    17.         DataGridView2.Rows.Clear()
    18.         pingipss.Enabled = False
    19.         cncel.Enabled = True
    20.         guardaripsactivas.Enabled = False
    21.         DataGridView2.Visible = False
    22.  
    23.         MyThread1 = New Thread(New ThreadStart(AddressOf thischecck))
    24.         MyThread2 = New Thread(New ThreadStart(AddressOf thischecck2))
    25.         MyThread1.Start()
    26.         MyThread2.Start()
    27.  
    28.     End Sub
    29.  
    30.  
    31.     Private Sub thischecck()
    32.         Dim found As Boolean = True
    33.         Dim macfound As Boolean
    34.         Dim num1 As String
    35.         Dim num2 As String
    36.         Dim num01 As String
    37.         Dim num02 As String
    38.         Dim hello As String
    39.         Dim sum As Integer
    40.         Dim dom As String
    41.  
    42.  
    43.         For sum = 0 ip28.Rows.Count - 1
    44.             hello = ip28.Rows.Item(sum).Cells.Item(1).Value  ''this checks the ip of row 2 cell 2
    45.  
    46.  
    47.            
    48.             Try
    49.                 If My.Computer.Network.Ping(hello, 300) Then    ''this will ping the ip address and return true or false
    50.  
    51.                     ip28.Rows.Item(sum).Cells.Item(2).Value = "ON" '' this checks or not the checkbox for true or false
    52.                     ip28.Rows.Item(sum).Cells.Item(3).Value = "-"
    53.                                         For ver = 0 To inventario.Rows.Count - 1
    54.  
    55.                             If inventario.Rows.Item(ver).Cells.Item(1).Value Is DBNull.Value Then
    56.                                 num2 = "0"
    57.                             Else
    58.                                 num2 = inventario.Rows.Item(ver).Cells.Item(1).Value 'ip
    59.                             End If
    60.  
    61.                             If inventario.Rows.Item(ver).Cells.Item(7).Value Is DBNull.Value Then
    62.                                 num02 = "0"
    63.                             Else
    64.                                 num02 = inventario.Rows.Item(ver).Cells.Item(7).Value 'mac
    65.                             End If
    66.                             If num1 = num2 Then
    67.                                 If num01 = num02 Then
    68.                                     found = True
    69.                                     macfound = True
    70.                                     ip28.Rows.Item(sum).Cells.Item(7).Value = "YES"
    71.                                     inventario.Rows.Item(ver).Cells.Item(0).Value = "YES"
    72.                                     inventario.Rows.Item(ver).Cells.Item(4).Value = (DataGridView1.Rows.Item(sum).Cells.Item(6).Value)
    73.                                     Exit For
    74.                                 Else
    75.                                     found = True
    76.                                     macfound = False
    77.                                     ip28.Rows.Item(sum).Cells.Item(8).Value = "YES"
    78.                                     inventario.Rows.Item(ver).Cells.Item(0).Value = "YES"
    79.                                                         ip28.Rows.Item(sum).Cells.Item(7).Value = "YES But Different MAC "
    80.                                     Exit For
    81.                                 End If
    82.                             Else
    83.                                 found = False
    84.                             End If
    85.                         Next
    86.                 If found = False Then
    87.                         ip28.Rows.Item(sum).Cells.Item(7).Value = "Not Found"
    88.                     End If
    89. Dim hi As String
    90.                     SyncLock DataGridView1
    91.                         For oki = 0 To DataGridView1.Rows.Count - 1
    92.                             hi1 = DataGridView1.Rows.Item(oki).Cells.Item(1).Value
    93.                             If hi = hi Then
    94.                                 DataGridView1.Rows.Item(oki).Cells.Item(2).Value = ip28.Rows.Item(sumadoe).Cells.Item(2).Value
    95.                                 DataGridView1.Rows.Item(oki).Cells.Item(3).Value = ip28.Rows.Item(sumadoe).Cells.Item(3).Value
    96.                                Exit For
    97.                             End If
    98.                         Next
    99.                     End SyncLock
    100.                 End If
    101.             Catch ex As Exception
    102.                 Debug.WriteLine(ex.Source)
    103.             End Try
    104.             My.Application.DoEvents()
    105.             found = True
    106.             Next
    107. end sub
    108.  
    109.  
    110. Private Sub thischecck2()
    111.         Dim found1 As Boolean = True
    112.         Dim macfind As Boolean
    113.         Dim no1 As String
    114.         Dim no2 As String
    115.         Dim no01 As String
    116.         Dim no02 As String
    117.         Dim hi As String
    118.         Dim sumadoe As Integer
    119.         Dim dmnio1 As String
    120.  
    121.  
    122.         For sumadoe = 0 To 100
    123.             hi = ip42.Rows.Item(sumadoe).Cells.Item(1).Value  ''this checks the ip of row 2 cell 2
    124.             Try
    125.                 If My.Computer.Network.Ping(hi, 300) Then    ''this will ping the ip address and return true or false
    126.                     ip42.Rows.Item(sumadoe).Cells.Item(2).Value = "ON" '' this checks or not the checkbox for true or false
    127.                     ip42.Rows.Item(sumadoe).Cells.Item(3).Value = "-"
    128.                         For ver = 0 To inventario.Rows.Count - 1
    129.                             If inventario.Rows.Item(ver).Cells.Item(1).Value Is DBNull.Value Then
    130.                                 no2 = "0"
    131.                             Else
    132.                                 no2 = inventario.Rows.Item(ver).Cells.Item(1).Value 'ip
    133.                             End If
    134.                             If inventario.Rows.Item(ver).Cells.Item(7).Value Is DBNull.Value Then
    135.                                 no02 = "0"
    136.                             Else
    137.                                 no02 = inventario.Rows.Item(ver).Cells.Item(7).Value 'mac
    138.                             End If
    139.  
    140.                             If no1 = no2 Then
    141.                                 If no01 = no02 Then
    142.                                     found1 = True
    143.                                     macfind = True
    144.                                     ip42.Rows.Item(sumadoe).Cells.Item(7).Value = "YES"
    145.                                     inventario.Rows.Item(ver).Cells.Item(0).Value = "YES"
    146.                                     inventario.Rows.Item(ver).Cells.Item(4).Value = (ip42.Rows.Item(sumadoe).Cells.Item(6).Value)
    147.                                     Exit For
    148.                                 Else
    149.                                     found1 = True
    150.                                     macfind = False
    151.                                     ip42.Rows.Item(sumadoe).Cells.Item(8).Value = "YES"
    152.                                     inventario.Rows.Item(ver).Cells.Item(0).Value = "YES"
    153.                                                                        ip42.Rows.Item(sumadoe).Cells.Item(7).Value = "YES But Different MAC "
    154.                                     Exit For
    155.                                 End If
    156.                             Else
    157.                                 found1 = False
    158.                             End If
    159.                         Next
    160.                
    161.                     If found1 = False Then
    162.                         ip42.Rows.Item(sumadoe).Cells.Item(7).Value = "Not found"
    163.                     End If
    164.  
    165.                    Dim hi1 As String
    166.                     SyncLock DataGridView1
    167.                         For oki = 0 To DataGridView1.Rows.Count - 1
    168.                             hi1 = DataGridView1.Rows.Item(oki).Cells.Item(1).Value
    169.                             If hi1 = hi Then
    170.                                 DataGridView1.Rows.Item(oki).Cells.Item(2).Value = ip42.Rows.Item(sumadoe).Cells.Item(2).Value
    171.                                 DataGridView1.Rows.Item(oki).Cells.Item(3).Value = ip42.Rows.Item(sumadoe).Cells.Item(3).Value
    172.                        
    173.                                 Exit For
    174.                             End If
    175.                         Next
    176.                     End SyncLock
    177.                
    178.                 End If
    179.             Catch ex As Exception
    180.                 Debug.WriteLine(ex.Source)
    181.             End Try
    182.             My.Application.DoEvents()
    183.             found1 = True
    184.                 Next
    185.                flagi2 = True
    186.     End Sub  
    187.  
    188. public sub thisisnext()
    189.  
    190.         pingipss.Enabled = True
    191.         DataGridView1.AutoResizeColumn(1, DataGridViewAutoSizeColumnMode.AllCells)
    192.         DataGridView1.AutoResizeColumn(2, DataGridViewAutoSizeColumnMode.AllCells)
    193.         inventario.AutoResizeColumn(0, DataGridViewAutoSizeColumnMode.AllCells)
    194.         inventario.AutoResizeColumn(1, DataGridViewAutoSizeColumnMode.AllCells)
    195.    
    196.         If mensaje = True Then
    197.             MsgBox("El en Linea")
    198.         End If
    199.         If parado = True Then
    200.             MsgBox("ElCancelado")
    201.         End If
    202.         PictureBox1.Visible = False
    203.         pingipss.Enabled = True
    204.         cncel.Enabled = False
    205.         guardaripsactivas.Enabled = True
    206.         guardaripsactivas.Visible = True
    207.            staprog.Enabled = True
    208.         bck.Enabled = True
    209.  
    210.              Ipnoreg.Enabled = True
    211.         stoprog.Enabled = True
    212.         Ipnoreg.Visible = True
    213.  
    214.     End Sub
    Last edited by justme369; Jun 17th, 2010 at 09:40 AM.

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: PLZ HELP ME WITH Multithreading

    You're getting the error because you're accessing UI elements from your secondary threads... as jmc explained in post #2...

    what is ip28 and ip42?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    182

    Re: PLZ HELP ME WITH Multithreading

    there are datagrids

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    182

    Re: PLZ HELP ME WITH Multithreading

    i sort of knew it had something to do with wat jmc explained but how can i solve it?

    since the code for both threads are really long i cut some code from both threads...

    my interest its how to perform an action (RUN THE SUB thisisnext()) once both threads are finish

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: PLZ HELP ME WITH Multithreading

    use background workers... put your subs in the doWork events... in the completed event, set your flags... then check the flags, and if both are set, call your other sub.

    I'm a little surprised that your code even works... if ip28 is a DGV, you should be getting errors the first time you access it about cross threading... you can't (or shouldn't) be able to access the UI from a non-UI thread......

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    182

    Re: PLZ HELP ME WITH Multithreading

    how can i place the subs in the do work
    Code:
    Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    public check ()
    end sub
    
    public check1 ()
    end sub
    end sub

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    182

    Re: PLZ HELP ME WITH Multithreading

    or u mean i call the threads in the do work?

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    182

    Re: PLZ HELP ME WITH Multithreading

    tell me if this is ok to do..


    i run only one sub in the background worker...this sub its the one that takes longer to finish ...

    and when i click button 1
    i run the background worker along 2 more threads...that finish really fast..

    so when background worker finish basically all the others are done..

    so the do work runs my other sub..

    its this programatically correct to do..

    i did it and no errors...i just want to know if this way its GOOD ONE

  14. #14
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: PLZ HELP ME WITH Multithreading

    As stated by jmcilhinney in his reply you normally not update the UI in the secondary thread but if you must you need to use Threading.SynchronizationContext in tangent with Threading.SendOrPostCallback which gets ugly fast. The attached demo uses two threads where the secondary thread updates the first thread, the UI.

    The project is VS2008
    Last edited by kareninstructor; Jul 9th, 2011 at 10:09 PM.

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: PLZ HELP ME WITH Multithreading

    Quote Originally Posted by kevininstructor View Post
    As stated by jmcilhinney in his reply you normally not update the UI in the secondary thread but if you must you need to use Threading.SynchronizationContext in tangent with Threading.SendOrPostCallback which gets ugly fast. The attached demo uses two threads where the secondary thread updates the first thread, the UI.

    The project is VS2008
    That's not really accurate. The SynchronizationContext is not a way to update the UI from a secondary thread. It is a way to marshal a method call to a specific thread, usually the UI thread, in order to execute a method on that thread. Calling the Send or Post method of a SynchronizationContext is equivalent to calling Invoke or BeginInvoke on a control: they both marshal a method call to the thread that owns the object you call the method on.

    If you check out my CodeBank submission on Accessing Controls From Worker Threads it uses the ISynchronizeInvoke implementation of the Control class in most examples, i.e. the InvokeRequired property and the Invoke method, but, towards the end, I've re-implemented one of the examples using the SynchronizationContext. That demonstrates the equivalence between the two.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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