Understanding Multi-Threading in VB.Net - Page 3-VBForums
Page 3 of 3 FirstFirst 123
Results 81 to 115 of 115

Thread: Understanding Multi-Threading in VB.Net

  1. #81

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    4,985

    Re: Understanding Multi-Threading in VB.Net

    What is that app supposed to do ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  2. #82
    Lively Member
    Join Date
    Feb 2012
    Posts
    106

    Re: Understanding Multi-Threading in VB.Net

    Quote Originally Posted by Niya View Post
    What is that app supposed to do ?
    Its about a process gateway.. where if any new process will execute, app will prompt that new process name. .. It's for my personal use. And to clear my doubt, i thought it should be a good example to ask...

    It's incomplete yet, Please suggest if you have any better idea to do its task.

    Thank you Niya
    Regards,
    Last edited by green.pitch; Jul 20th, 2013 at 01:47 PM.

  3. #83

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    4,985

    Re: Understanding Multi-Threading in VB.Net

    Well you have me at a disadvantage here. I haven't written any apps yet that deal with processes in complex way so I know little on this subject. I'm trying to understand how threading fits in here and what it is that you're trying to do with those Forms
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  4. #84
    Lively Member
    Join Date
    Feb 2012
    Posts
    106

    Re: Understanding Multi-Threading in VB.Net

    Quote Originally Posted by Niya View Post
    Well you have me at a disadvantage here. I haven't written any apps yet that deal with processes in complex way so I know little on this subject. I'm trying to understand how threading fits in here and what it is that you're trying to do with those Forms
    Well Ok, I'm not asking to work on processes. What about the Multi threading on Form2 ? How to make free that form which is hanging.. It can be done by multi threading..

  5. #85

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    4,985

    Re: Understanding Multi-Threading in VB.Net

    Quote Originally Posted by green.pitch View Post
    Well Ok, I'm not asking to work on processes. What about the Multi threading on Form2 ? How to make free that form which is hanging.. It can be done by multi threading..
    What multi-threading on Form2 ? There is no code written inside Form2 at all.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  6. #86
    New Member
    Join Date
    Jul 2013
    Location
    sc-us
    Posts
    14

    Re: Understanding Multi-Threading in VB.Net

    Niya, after reading this thread and forcefeeding myself on it, I registered--Just to thank you!
    I don't mean to slight any other posters, just that this topic is currently important to me.
    I made the decision to finally ween off VB6 after writing an app to read 50,000 INT16 values from an industrial controller.
    Using VB6/Winsocks, it is taking about 90 seconds.
    With vb.net/multithreading/ (and whatever TCP client methodology I choose) I hope to trim down to less than 30 seconds.

  7. #87

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    4,985

    Re: Understanding Multi-Threading in VB.Net

    If the task itself is parallelizable then you would definitely see an improvement in performance especially on systems with multi-core processors. I don't really know anything about reading from controllers but if you can read these integers by starting at an index then you can break the list up into several buckets and assign a thread to read each. However, if the controller itself can only be read at the hardware level as a serial device then that will be a bottleneck as there it wouldn't actually be reading in parallel. There would be no improvement.

    I commend your effort to move away from VB6 and into VB.Net as its a much more powerful development tool by leaps and bounds but I cannot say for certain that it would help in this particular case.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  8. #88
    New Member
    Join Date
    Jul 2013
    Location
    sc-us
    Posts
    14

    Re: Understanding Multi-Threading in VB.Net

    I already have a vb6 app reading the data. The controller registers are indexed.
    The parallel bucket reference is exactly my approach in the VB6 app , but sadly, VB6 isn't suited to multithreading.
    I make multiple run-time instances of the WinSock control, issuing multiple queries on the controller, but then process the return data of each WinSock 'sequentially.'
    That appears to be the bottleneck.
    The controller access is via ethernet connection. My initial concept was: multiple Operator Screens can make simultaneous connections to the controller,
    so a pc app should be able to make multiple connections to the controller. Well, that concept has been proven - except for the VB6 limitations.

    I have come here to read and learn First. Then ask for help later. So, thanks for the opportunity to 'read and learn!' Much appreciated.

  9. #89

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    4,985

    Re: Understanding Multi-Threading in VB.Net

    Quote Originally Posted by OnErrorGoAway View Post
    I already have a vb6 app reading the data. The controller registers are indexed.
    The parallel bucket reference is exactly my approach in the VB6 app , but sadly, VB6 isn't suited to multithreading.
    I make multiple run-time instances of the WinSock control, issuing multiple queries on the controller, but then process the return data of each WinSock 'sequentially.'
    That appears to be the bottleneck.
    The controller access is via ethernet connection. My initial concept was: multiple Operator Screens can make simultaneous connections to the controller,
    so a pc app should be able to make multiple connections to the controller. Well, that concept has been proven - except for the VB6 limitations.
    Well I don't think you will get a speed improvement by using multiple connections. There is only one physical connection so the data being transmitted over these multiple TCP/IP connections will be multiplexed into a single stream to be transmitted across the wire, so it would only appear to be working in parallel in code. The sum total transfer rate of say five TCP connections will be the same transfer rate as a single connection.

    The only way to this could be truly parallel is if the controller itself allowed you to make multiple physical connections via multiple ethernet ports and cables. Then the device itself can truly work in parallel.

    Quote Originally Posted by OnErrorGoAway View Post
    I have come here to read and learn First. Then ask for help later. So, thanks for the opportunity to 'read and learn!' Much appreciated.
    You're welcome
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  10. #90
    New Member
    Join Date
    Jul 2013
    Location
    sc-us
    Posts
    14

    Question Re: Understanding Multi-Threading in VB.Net

    Niya,
    In a preceding post you said that you were not detailing your ThreadExtensions class. I got interested!
    I have spent a couple days poring over those 14 lines, F8'ing through and also reading Nick's thread Here , among others.
    I haven't figured out all of the class and HOW it works, but I think that I have made progress in extending the parameter list.

    I would appreciate if you could tell me if I am heading in the right direction, and if not, tell me so.
    The changes do work, but if I have made a dumb mistake, the IDE didn't tell me. 0 Errors, 0 Warnings, 0 Messages
    [VB2010 Express]

    My changes are highlighted in green, explanations are bold italic
    I used the DateTime because I got bored with simple Integers
    Code:
    'Form1 code
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim DT As DateTime = #8/13/2002 12:14 PM# ' DateTime from MSDN example - just to notice a change when returned
            m_cn.CountAsync(600, 500, 50, DT) 'Changed Max to 600 and Added three new params (2 integer and one DateTime) -  I did change Min/Max of ProgressBar1
        End Sub
    
       Private Sub m_cn_CountChanged(ByVal sender As Object, ByVal e As CountChangedEventArgs) Handles m_cn.CountChanged
            ProgressBar1.Value = CStr(e.CurrentCount)
            Label2.Text = CStr(e.CurrentCount)
            Label3.Text = CStr(e.Mode) 'Show the new param as passed back
            Label4.Text = CStr(e.DT)  'Show the new param as passed back
            Label5.Text = CStr(e.Min) 'Show the new param as passed back
    
        End Sub
    Code:
    'Counter Class code
        Public Sub CountAsync(ByVal Max As Integer, ByVal Min As Integer, ByVal Mode As Integer, ByVal DT As DateTime) 
            '>>------------------------------------ Change New Func Signature and Count Signature
            ThreadExtensions.QueueUserWorkItem(New Func(Of Integer, Integer, Integer, DateTime, String)(AddressOf Count), Max, Min, Mode, DT) 
        End Sub
    
    'Add new variables declarations to the Count signature
       Public Function Count(ByVal Max As Integer, ByVal Min As Integer, ByVal Mode As Integer, ByVal DT As DateTime) As String
            Dim startTime As DateTime = DateTime.Now
            Dim e As CountChangedEventArgs
            Dim msg As String
    
            For i = 1 To Max
                e = New CountChangedEventArgs(i, Max, Min, Mode, DT) 'Add return variables here
    
                If context Is Nothing Then
                    OnCountChanged(e)
                Else
                    ThreadExtensions.ScSend(context, New Action(Of CountChangedEventArgs)(AddressOf OnCountChanged), e)
                End If
    
                Threading.Thread.Sleep(200)
            Next
    
            msg = "Count took : " + (DateTime.Now - startTime).ToString
    
            If context Is Nothing Then
                OnCountCompleted(New CountCompletedEventArgs(msg))
            Else
                ThreadExtensions.ScSend(context, New Action(Of CountCompletedEventArgs)(AddressOf OnCountCompleted), New CountCompletedEventArgs(msg))
            End If
    
            Return msg
        End Function
    Code:
    'CounterChangedEventArgs Class code
    
        Private _Max As Integer
        Private _Min As Integer  ' Support first ADDED variable
        Private _Mode As Integer ' Support second ADDED variable
        Private _DT As DateTime ' Support third ADDED variable
    
       Public Sub New(ByVal cc As Integer, ByVal max As Integer, ByVal min As Integer, ByVal Mode As Integer, ByVal DT As DateTime)
            _CurrentCount = cc
            _Max = max
            _Min = min 'Get the new value to return to UI
            _Mode = Mode 'Get the new value to return to UI
            _DT = DateTime.Now  'Get the new value to return to UI
        End Sub
    
    'Finally, add support for the public properties
    
       Public ReadOnly Property Min() As Integer
            Get
                Return _Min
            End Get
        End Property
        Public ReadOnly Property Mode() As Integer
            Get
                Return _Mode
            End Get
        End Property
        Public ReadOnly Property DT() As DateTime
            Get
                Return _DT
            End Get
        End Property
    
    Thank you for your time and effort!

  11. #91

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    4,985

    Re: Understanding Multi-Threading in VB.Net

    Quote Originally Posted by OnErrorGoAway View Post
    I would appreciate if you could tell me if I am heading in the right direction, and if not, tell me so.
    The changes do work, but if I have made a dumb mistake, the IDE didn't tell me. 0 Errors, 0 Warnings, 0 Messages
    [VB2010 Express]
    You did everything 100% correctly. If this were a test, you'd get an A++

    Quote Originally Posted by OnErrorGoAway View Post
    In a preceding post you said that you were not detailing your ThreadExtensions class. I got interested!
    I have spent a couple days poring over those 14 lines, F8'ing through and also reading Nick's thread Here , among others.
    Note that if you're using VS2010 that the ThreadExtensions class is not necessary because the VB2010 compiler gives you a Sub keyword for lambdas that don't return a value so you can use QueueUserWorkItem of the ThreadPool class directly and specify a Sub lambda to call your functions. Its far more terse that using ThreadExtensions.

    Eg: This:-
    vbnet Code:
    1. ThreadExtensions.QueueUserWorkItem(New Func(Of Integer, Integer, Integer, DateTime, String)(AddressOf Count), Max, Min, Mode, DT)

    Can become:-
    vbnet Code:
    1. ThreadPool.QueueUserWorkItem(Sub() Count(Max, Min, Mode, DT))

    ThreadExtensions
    is necessary if you're using VS2008 which doesn't have a Sub keyword.
    Last edited by Niya; Jul 28th, 2013 at 04:50 AM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  12. #92
    New Member
    Join Date
    Jul 2013
    Location
    sc-us
    Posts
    14

    Re: Understanding Multi-Threading in VB.Net

    You did everything 100% correctly. If this were a test, you'd get an A++
    Thank you. You are too kind.

    the VB2010 compiler gives you a Sub keyword for lambdas that don't return a value so you can use QueueUserWorkItem of the ThreadPool class directly and specify a Sub lambda to call your functions.
    Works as specified. Thank you, again.

  13. #93
    New Member
    Join Date
    Jul 2013
    Location
    sc-us
    Posts
    14

    Re: Understanding Multi-Threading in VB.Net

    Niya,
    I have Button1.Enabled = False in Button1_Click and then Button1.Enabled = True in m_cn_CountCompleted(..)
    I read that threadpool threads do NOT re-init any data from the last usage of that thread. Here
    Now, since I am interlocking Button1 click, the same thread must be used the next time that I click Button1.

    My reason/evidence for this is that the Count function does NOT restart at my new min value -- it is at max immediately!
    Before interlocking the button, 'new' threads started at min.
    In the original function, i is not declared, so I declared Dim i As Integer = 0, and the count starts at min again.

    Does this sound reasonable?

    source MSDN
    When the thread pool reuses a thread, it does not clear the data in thread local storage or in fields that are marked with the ThreadStaticAttribute attribute.
    Therefore, when a method examines thread local storage or fields that are marked with the ThreadStaticAttribute attribute, the values it finds might be left over from an earlier use of the thread pool thread.
    Last edited by OnErrorGoAway; Jul 28th, 2013 at 01:22 PM. Reason: Cite source of threadpool thread statement

  14. #94

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    4,985

    Re: Understanding Multi-Threading in VB.Net

    i is not using thread local storage. It is stored on the stack which means the values no longer exist when the function exits. Thread local storage has to explicitly be used. It is never implicit. So that observation has nothing to do with thread local storage. Please explain what you mean by "interlocking" the button.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  15. #95
    New Member
    Join Date
    Jul 2013
    Location
    sc-us
    Posts
    14

    Re: Understanding Multi-Threading in VB.Net

    Interlocking may have been a poor choice of terminology for .Net.
    I meant that I disable the the button (Button1) when clicking it and then re-enabling it in m_cn_CountCompleted(..) sub.

    I was trying to provide a cause for what I observed. With your assessment I think that I found my problem. -- it was NOT a thread local storage issue...
    I was trying to 'cancel' (or in this case HURRY-UP) the thread by passing a bool value to it.
    I never reset the bool value in the code in Form1.

    I do not know if it is correct to pass a 'Cancel' bool to the running thread, but what I have done is:
    overloaded the Count function with
    Code:
        Public Overloads Sub Count(ByVal Cancel As Boolean)
            CancelRequest = Cancel
        End Sub
    and in the Count function, check for CancelRequest, to let the thread handle the Cancel request. ( I feel that it is better than attempting to abort a thread)
    Code:
            
    Public Overloads Function Count(ByVal Max As Integer, ByVal Min As Integer, ByVal Mode As Integer, ByVal DT As DateTime) As String 
    .
    .
    .
           For i = Min To Max
                If Not CancelRequest Then
                    e = New CountChangedEventArgs(i, Max, Min, Mode, DT) 
    
                    If context Is Nothing Then
                        OnCountChanged(e)
                    Else
                        ThreadExtensions.ScSend(context, New Action(Of CountChangedEventArgs)(AddressOf OnCountChanged), e)
                    End If
    
                    Threading.Thread.Sleep(50)
                Else
                    i = Max
                End If
            Next
    .
    .
    .
    End Function
    If a better/correct way exists, I haven't read about it or found it yet. I will keep plodding on.
    Thank you for your valued input!

  16. #96

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    4,985

    Re: Understanding Multi-Threading in VB.Net

    Quote Originally Posted by OnErrorGoAway View Post
    ( I feel that it is better than attempting to abort a thread)....
    If a better/correct way exists, I haven't read about it or found it yet. I will keep plodding on.
    Thank you for your valued input!
    You have a knack for this. Yes what you're doing is the correct way. You use a cancel field and let the thread cancel itself. You never ever want to abort a running thread. Only the thread itself knows about its internal state so it should be responsible to stopping itself. From the outside you cannot know where the thread is currently at so aborting from the outside can cause some nasty side effects.

    For example, imagine a thread that opens files in a list one by one. You want when you stop the thread, that it closes the current file it has open before aborting. Its easy to do that from inside the thread, if a cancel variable was set you close and exit whatever loop is iterating the list. But if you try to abort the thread from the outside, you could have called it smack in the middle of reading a file and it would just stop abruptly, leaving the file opened and possibly locked.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  17. #97
    New Member
    Join Date
    Jul 2013
    Location
    sc-us
    Posts
    14

    Re: Understanding Multi-Threading in VB.Net

    Niya,
    Am I missing something?
    You mentioned earlier that
    ThreadExtensions class is not necessary because the VB2010 compiler gives you a Sub keyword for lambdas
    Does this hold true for your ThreadExtensions.ScSend(..., or was that statement directed at starting a thread?

  18. #98

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    4,985

    Re: Understanding Multi-Threading in VB.Net

    Yes, it applies to ScSend as well as it also takes a delegate argument.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  19. #99
    New Member
    Join Date
    Jul 2013
    Location
    sc-us
    Posts
    14

    Re: Understanding Multi-Threading in VB.Net

    Thank you. Another puzzle.

    RE: my project (post #86)
    I am StopWatching it at 10.9 seconds in IDE and 10.2 seconds in .EXE.
    I have two 'iterations' of 25 threads, each thread making a connection and reading 10 blocks of data, shutting down and closing the connection, then reporting that data back to UI.
    Each thread updates its UI ProgressBar after each of the 10 reads and then at completion dumps its data to a TextBox.
    This is a learning demo so I am not ready to ask for any help. I know that my project has kludges, but they will be removed as my understanding increases.
    I have merged multiple examples from this forum and other sources such as MSDN. Thanks to all.
    Now, I must understand how it does what it does.

    [EDIT:Replace string functions with stringbuilder]
    I am StopWatching it at 9.7 seconds in IDE and 9.6 seconds in .EXE.

    Edit: just received books
    -Programming VB.Net (Cornell & Morrison)
    -VB 2010 Prog Ref (Stephens)
    So I may be absent for a while
    Last edited by OnErrorGoAway; Aug 3rd, 2013 at 06:58 PM. Reason: Replace string functions with stringbuilder

  20. #100

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    4,985

    Re: Understanding Multi-Threading in VB.Net

    Ok well, good luck to you my friend. I'll always be around if you have any questions.
    Last edited by Niya; Aug 3rd, 2013 at 11:34 AM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  21. #101
    New Member
    Join Date
    Jul 2013
    Location
    sc-us
    Posts
    14

    Re: Understanding Multi-Threading in VB.Net

    RE: ThreadExtensions [Niya VB2008] -vs- SendOrPostCallback [VB2010]
    I think that I may be on to something! I have been F8'ing some more.
    If I have done something really wrong, please do not hesitate to correct me!
    Replace:
    Code:
                      
     ThreadExtensions.ScSend(context, New Action(Of CountChangedEventArgs)(AddressOf OnCountChanged), e)
    with this:
    Code:
     context.Send(New SendOrPostCallback(Sub(state As Object) RaiseEvent CountChanged(Me, e)), Nothing)
    CountChanged worked.

    Then replace this:
    Code:
     ThreadExtensions.ScSend(context, New Action(Of CountCompletedEventArgs)(AddressOf OnCountCompleted), New CountCompletedEventArgs(msg))
    with this:
    Code:
           
     Dim em As CountCompletedEventArgs
    .
    .
    .
     em = New CountCompletedEventArgs(msg)
    .
    .
    .
     context.Send(New SendOrPostCallback(Sub(state As Object) RaiseEvent CountCompleted(Me, em)), Nothing)
    CountComplete worked.


    Notes:
    VB default project settings
    Option Explicit ON
    Option Strict ON
    Option Compare BINARY
    Option Infer ON

    --If I had .Net Reflector, I would need this chair removed surgically!--

  22. #102

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    4,985

    Re: Understanding Multi-Threading in VB.Net

    Yes, those are correct but a little too verbose for my tastes. You could just do this:-
    vbnet Code:
    1. context.Send(Sub() RaiseEvent CountChanged(Me, e)), Nothing)
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  23. #103
    Frenzied Member make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    1,912

    Re: Understanding Multi-Threading in VB.Net

    Niya it will be great to see if you start some thread on LINQ (vb.net) please
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  24. #104

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    4,985

    Re: Understanding Multi-Threading in VB.Net

    Quote Originally Posted by make me rain View Post
    Niya it will be great to see if you start some thread on LINQ (vb.net) please
    Aite
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  25. #105
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    www.vbforums.com
    Posts
    1,248

    Re: Understanding Multi-Threading in VB.Net

    Great post on Multithreading!

    Have you by any chance created a sample code in C#?

    If not, I'll just stick with the VB.NET example and play around
    with it.

    KG
    Thread Helpers 101
    I. Don't forget to
    rate my post

    II. Mark your threads
    Resolved if it has been answered.
    III. Wrap your code snippets with code tags to make it readable.

    ____________________________________________________________________
    CodeBank Submissions (ASP.NET/VB.NET/C#)

  26. #106

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    4,985

    Re: Understanding Multi-Threading in VB.Net

    Quote Originally Posted by KGComputers View Post
    Have you by any chance created a sample code in C#?
    No I haven't. It would be exactly the same in C#. Only the syntax would differ. As a matter of fact, you should have little to no problems using an online converter to convert the samples to C#.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  27. #107
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    www.vbforums.com
    Posts
    1,248

    Re: Understanding Multi-Threading in VB.Net

    Great!

    Thanks..
    Thread Helpers 101
    I. Don't forget to
    rate my post

    II. Mark your threads
    Resolved if it has been answered.
    III. Wrap your code snippets with code tags to make it readable.

    ____________________________________________________________________
    CodeBank Submissions (ASP.NET/VB.NET/C#)

  28. #108
    New Member
    Join Date
    Oct 2013
    Posts
    3

    Re: Understanding Multi-Threading in VB.Net

    Thanks for source code

  29. #109

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    4,985

    Re: Understanding Multi-Threading in VB.Net

    You're welcomed.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  30. #110
    New Member
    Join Date
    May 2014
    Posts
    12

    Re: Understanding Multi-Threading in VB.Net

    Hi Niya

    Sitting here reading your very well described article about Multi-Threading - great reading!! Enjoyed it very much.

    I'm working on a Windows service that amoung other things will scan a number of servers on the Network and due to the length of a server scan I'm thinking of building a Multi-Threading code to handle this. That way I am able to start let's say 50 scans at the same time. There could be a lot more servers - might be up 4.000....
    Now it is just the question on how.

    My Sub called ScanServer(ServerName as string) will need to be called but I want to be able to set a max on how many scans that can run at the same time - 50 is a starting point but not a static number.

    I have the server names in a list

    Private ServerList As New List(Of ServerClass)

    ServerClass defined as:

    Public Class ServerClass

    Public ServerName As String
    Public IPAddress As String

    Sub ServerClass()
    ServerName = String.Empty
    IPAddress = String.Empty
    End Sub

    End Class


    How do I put together a piece of code that can start the scans (up to X number at a time) and keep them filling the queue until the last server is scanned?
    The Sub (ScanServer) will not return any data - the scan will be written into a XML file on the disk so I have no demand for keeping track of the thread while running.


    Thanks in advance!!


    Best Regards
    /Mogge

  31. #111

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    4,985

    Re: Understanding Multi-Threading in VB.Net

    I'm curious, why do you have this:-
    vbnet Code:
    1. ScanServer(ServerName as string)
    instead of:-
    vbnet Code:
    1. ScanServer(ServerName as ServerClass)
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  32. #112
    New Member
    Join Date
    May 2014
    Posts
    12

    Re: Understanding Multi-Threading in VB.Net

    Sure - I'm currently in the building phase and all things are not settle yet.

    I find your suggestion very good.

    /Mogge

  33. #113

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    4,985

    Re: Understanding Multi-Threading in VB.Net

    Booo!
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  34. #114
    New Member
    Join Date
    Jul 2008
    Posts
    9

    Re: Understanding Multi-Threading in VB.Net

    sorry if this has already been answered, lots of activity ....

    Why do you use AddressOf instead of Sub()? Sub() allows you specify multiple parameters. Does AddressOf offer better performance or error handling?

  35. #115

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    4,985

    Re: Understanding Multi-Threading in VB.Net

    Quote Originally Posted by demanaz View Post
    sorry if this has already been answered, lots of activity ....

    Why do you use AddressOf instead of Sub()? Sub() allows you specify multiple parameters. Does AddressOf offer better performance or error handling?
    Could specify exactly where you're talking about? Been a while since I looked over the code. I can't recall off hand.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

Page 3 of 3 FirstFirst 123

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.