Page 4 of 4 FirstFirst 1234
Results 121 to 149 of 149

Thread: Understanding Multi-Threading in VB.Net

  1. #121
    Junior Member
    Join Date
    Jul 2012
    Posts
    21

    Re: Understanding Multi-Threading in VB.Net

    Hi, just wanted to ask something, if we start an app, and in the app start a thread, and then we close the app, will the thread also close or will it continue until it's done with what it had to do?

  2. #122
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Understanding Multi-Threading in VB.Net

    It depends on if the thread is a Background thread (it will end when the main thread, aka the GUI thread in a winforms application ends) or not.
    If it is not a background thread, it will continue to run until it finishes.
    <edit> Just realized that this in the CodeBank forum. You should post questions like these in a new thread, rather than in a CodeBank thread. </edit>

  3. #123
    New Member
    Join Date
    Dec 2014
    Posts
    3

    Re: Understanding Multi-Threading in VB.Net

    It will continue, you can test this by (while debug mode) creating and starting a new thread with an infinite loop and closing the app. Now if you look at your controls inf Visual Studio you will see that the START button is disabled and the STOP button is enabled even though your app should be done.

  4. #124
    New Member
    Join Date
    Dec 2014
    Posts
    3

    Re: Understanding Multi-Threading in VB.Net

    This thread is ancient but it still seams to get some interest. If you guys are interested I created a jump-start Video Tutorial on Multi-Threading. It's on YouTube http://www.youtube.com/playlist?list...3Sfvzlsha4Jdnk

  5. #125

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Understanding Multi-Threading in VB.Net

    Quote Originally Posted by walterwjl View Post
    Hi, just wanted to ask something, if we start an app, and in the app start a thread, and then we close the app, will the thread also close or will it continue until it's done with what it had to do?
    Quote Originally Posted by Irek Janek View Post
    It will continue, you can test this by (while debug mode) creating and starting a new thread with an infinite loop and closing the app. Now if you look at your controls inf Visual Studio you will see that the START button is disabled and the STOP button is enabled even though your app should be done.
    As passel said, it depends on if the thread in question is a background thread. I explained in the tutorial that an application stops only when all foreground threads are stopped. When all foreground threads are stopped then all background threads will also be terminated and no infinite loop will prevent this.
    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

    Copy/move files using Windows Shell | I'm not wanted

    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

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #126

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Understanding Multi-Threading in VB.Net

    Quote Originally Posted by StatShacker View Post
    Niya,
    At this point I don't suppose you ~need~ to hear again what an outstanding job you've done here but I think you should here it!!! And like several others, your posts and in particular your responses to the replies of others inspired me to register here just to tell you how much I appreciate how cogent your instructions are, how patient you are in your responses to nuubs (myself included hopefully), how generous you are of your time to continue supporting this thread now for over almost two and a half years and (he bows his head in embarrassment) to ask a few questions of my own.
    Notwithstanding my questions, my inspiration to register came from my need to tell you that you are truly the "Angel of Code," for this is, by far, the best thread on multi-threading (sorry for the pun) that I've found and I am certain not to find one better. What has made this thread even better are some of the queries to it and your thoughtful, helpful and instructive responses to them. I hope that (even as a nuub) my questions can make a contribution in this realm.
    Nice to hear this has been helpful.

    Quote Originally Posted by StatShacker View Post
    Question 1) Please confirm. (my situation) If you have a third party component instantiated in the main thread; when you handle a callback from that component, the callback is directly run in the main UI thread, if the callback delegate and method are in the code for the main form, yes? I am pretty sure the answer is 'Yes,' but I'm not 100% sure that the code in the component (clearly a separate thread) extend itself into the callback method...
    I think you may be holding onto a few misconceptions. Firstly, objects aren't really thread specific in the strictest technical terms. While some classes might goad you into paying attention to the thread they were created on, know that there is nothing inherent in the operation of the .Net Framework that says that an object belongs to a particular thread. This is all the work of the person who wrote the class. They can choose to have that class execute its methods on that thread or to have them executed on only the UI thread regardless of where it was created.

    Executing methods are thread specific, not the classes that define them. That being said, you can never make assumptions about what thread a callback will be executed on. The only way to be certain is to consult the documentation for the component. A properly documented component will inform you how their callbacks are executed. However, there are common patterns surrounding callbacks that can help you make a well informed guess. One such pattern is the asynchronous pattern which you might have seen on classes like the Socket class. The Socket class has several methods which have asynchronous variants. In those cases, callbacks are employed to signal either the failure or success of an operation. These callbacks are usually executed on a worker thread. If you see a pattern like that, you can make a well informed guess. But its still not a guarantee.


    Quote Originally Posted by StatShacker View Post
    Question 2) Please confirm. Some of your responses to queries were (understandably) VB.Net Framework version dependent . With the final version of your post (i.e., using the "QueueUserWorkItem" of ThreadPool, which works under Framework 3.5, my target), is it necessary to "EndInvoke" this thread? Again I'm pretty sure, based on reading descriptions written elsewhere about ThreadPool, but ask so I can sleep at night with no lingering doubts...
    No, you don't need to call and type of 'EndInvoke' method for QueueUserWorkItem. EndInvoke is usually employed with the asynchronous pattern I mentioned above.
    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

    Copy/move files using Windows Shell | I'm not wanted

    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

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #127
    New Member
    Join Date
    Feb 2015
    Posts
    4

    Re: Understanding Multi-Threading in VB.Net

    How exchange messages in string threads? Like " ok finish functions" or " not yet wait..."

  8. #128

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Understanding Multi-Threading in VB.Net

    Well it depends. If you want some thread to indicate when its finished doing something, you can have the object raise an event signalling that its finished. If you want different threads within the same object to communicate, you can use some kind of polling. You will have to use a class scoped variable and poll against that.
    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

    Copy/move files using Windows Shell | I'm not wanted

    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

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  9. #129
    New Member
    Join Date
    Feb 2015
    Posts
    4

    Re: Understanding Multi-Threading in VB.Net

    receiveThrd = New Threading.Thread(AddressOf recv)
    procprotoThrd = New Threading.Thread(AddressOf Protocol)

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    send data
    pooling thread protocol ' wait device response
    send string tell me data packet arrived
    yes finish!
    data expectate = data received
    process...

    End Sub

    Private Sub protocol()


    End Sub

    Private Sub recv()

    End Sub

    sorry incomplete code, but searching and not found how exchange information in threads. Thanks for repply.

  10. #130
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,191

    Re: Understanding Multi-Threading in VB.Net

    Your code looks fairly similar to dbasnett's so check out his Protocol sub in this post

  11. #131

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Understanding Multi-Threading in VB.Net

    Quote Originally Posted by topshot View Post
    Your code looks fairly similar to dbasnett's so check out his Protocol sub in this post
    Yea, that seems to be precisely it.
    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

    Copy/move files using Windows Shell | I'm not wanted

    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

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  12. #132
    New Member
    Join Date
    Feb 2015
    Posts
    4

    Re: Understanding Multi-Threading in VB.Net

    Yes, it works fine but the thread only update Label and process datareceived. I want do in realtime, send command and pooling thread Protocol() with data arrived. I'm using this post for understanding how threads interact.

    I try this sample: https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

    thanks!

  13. #133

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Understanding Multi-Threading in VB.Net

    I don't understand what you're asking.
    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

    Copy/move files using Windows Shell | I'm not wanted

    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

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  14. #134
    New Member
    Join Date
    Mar 2015
    Posts
    5

    Re: Understanding Multi-Threading in VB.Net

    Hi!

    First Post Here! Self-taught vb.net programmer here!

    Thanks for this (old!) topic which is still very relevant!

    I do have a problem adapting your example to my situation though.

    I have made this ticket monitoring app that makes requests to a DB and puts the info into Datagridview object. Now I want to put the DB subroutines through another process, and your code achieives this. Where I have a problem is in updating the DGV...

    The sub that is threaded make these call to the thread that will invoke:
    Code:
    row = New String() {OdbcDr.GetValue(0).ToString(), OdbcDr.GetValue(9).ToString(), OdbcDr.GetValue(10).ToString(), OdbcDr.GetValue(1).ToString(), OdbcDr.GetValue(11).ToString(), OdbcDr.GetValue(2).ToString(), OdbcDr.GetValue(7).ToString(), OdbcDr.GetValue(4).ToString(), ONS & strHour}
    ThreadUpdateDGV(row)
    As you can see I pass a string array into an rows.add to my DGV...

    And it works because when I debug I can see my values are passed correctly

    here is part of the sub that use the Invoke:

    Code:
       Protected Sub ThreadUpdateDGV(ByVal myrow As String())
                    If dgvTickets.InvokeRequired Then
                        dgvTickets.Rows.Clear()
                        dgvTickets.Invoke(New Action(Of String())(AddressOf ThreadUpdateDGV), myrow)
              Else
                        dgvTickets.Rows.Add(myrow)
                    End If
    I do think I incorrectly call the Invoke for the DVG but, for the life of me, I come up empty. Can you tell me what I do wrong?

  15. #135

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Understanding Multi-Threading in VB.Net

    You are clearing the DGV every time you check InvokeRequired. Remove that line:-
    vbnet Code:
    1. dgvTickets.Rows.Clear()
    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

    Copy/move files using Windows Shell | I'm not wanted

    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

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  16. #136
    New Member
    Join Date
    Mar 2015
    Posts
    5

    Re: Understanding Multi-Threading in VB.Net

    Quote Originally Posted by Niya View Post
    You are clearing the DGV every time you check InvokeRequired. Remove that line:-
    vbnet Code:
    1. dgvTickets.Rows.Clear()
    I did (got it when reviewing the code I sent, but I still have the same problem..

  17. #137

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Understanding Multi-Threading in VB.Net

    Did you check the value of that String array you're passing in ?
    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

    Copy/move files using Windows Shell | I'm not wanted

    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

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  18. #138
    New Member
    Join Date
    Mar 2015
    Posts
    5

    Re: Understanding Multi-Threading in VB.Net

    I get a "TargetParameterCountException was hunandled" error...

  19. #139
    New Member
    Join Date
    Mar 2015
    Posts
    5

    Re: Understanding Multi-Threading in VB.Net

    Yeah. The values are passed correctly, I see that they are what I am waiting for. It seem the Invoke does not like working with arrays...

  20. #140
    New Member
    Join Date
    Mar 2015
    Posts
    5

    Re: Understanding Multi-Threading in VB.Net

    WHile using a break point I see this in the properties of the DGV I am invoking:

    AccessibilityObject = {System.InvalidOperationException: Opération inter-threads non valide*: le contrôle 'dgvTickets' a fait l'objet d'un accès à partir d'un thread autre que celui sur lequel il a été créé. à System.Windows.Forms.Control.get_Handle() à System.Windows.Fo...

    Translation: the control is subject to access from another thread than the one it was created on...

  21. #141

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Understanding Multi-Threading in VB.Net

    Your problem might have something to do with how you are adding those rows. The threading code is correct so I can only assume the problem lies elsewhere. For one thing the documentation for the DGV doesn't indicate that there is an Add overload that takes a String array.
    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

    Copy/move files using Windows Shell | I'm not wanted

    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

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  22. #142
    New Member
    Join Date
    Apr 2015
    Posts
    4

    Re: Understanding Multi-Threading in VB.Net

    Niya,

    Thanks! As many have said, this was a great explanation of threading, and your continued support is really impressive! Thanks for giving back!

    Personally, I am having a little trouble relating your example to my situation. I have successfully connected to a serial port via the serial port class which of course has its own events. I then use the incoming data to update a textbox. There are plenty of examples of how to do this elsewhere but they are over simplified and simple use Textbox.Invoke to get back to the UI thread from the serial port. Those work, but since the serial port is constantly receiving data, the UI thread never regains control. Plus, eventually, I want to use the incoming data to update multiple custom controls rather than simply updating just one textbox. Previously, I initialized mySerialPort in the main UI thread, but now that I read through your examples, I'm thinking I should open the serial port in a separate class, but then I circle back and say, "well the serial port is already a separate class isn't it?"

    Thanks in advance!

  23. #143

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Understanding Multi-Threading in VB.Net

    Well I've never written anything that uses serial ports but I imagine that whatever class you're using to receive data has asynchronous methods. You can simply use those methods if you don't want to wrap the serial port code inside another class. However, if you're going to use synchronous methods then it would ultimately cause less headaches if you wrap your serial port communication inside a class and use worker threads to run the methods asynchronously.
    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

    Copy/move files using Windows Shell | I'm not wanted

    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

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  24. #144
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Understanding Multi-Threading in VB.Net

    I assume you're processing data in the DataReceived event, which executes in a thread kicked off by the SerialPort object.
    If your data is coming fast enough that your GUI has a hard time doing the things it needs because of constant invoking, as Niya says you can try BeginInvoke instead which is asynchronous.
    I'm a little cautious with BeginInvoke. If the data is slow enough that I know I won't be overlapping BeginInvoke calls, then using BeginInvoke to avoid holding the receiving thread any longer than necessary I like. But if the data is coming fast, and I'm calling BeginInvoke repeatedly, so that calls can overlap with previous asynchronous invocations not completed, can lead to issues, depending on if you're using reference types, etc.

    My preference is to push the received data in the serial receiver onto a concurrent queue, and have a "GUI Timer", that is responsible in the GUI thread of periodically checking the queue and processing the data and updating the GUI elements. Usually I have the GUI timer operating at 10hz to update the GUI, while the various Serial ports, and Ethernet ports communicating with various machines at various rates at the same time.
    That keeps the GUI thread responsive, and reflecting data the user needs, and keeps from tying up the various communication interfaces that are receiving data.
    Last edited by passel; Apr 9th, 2015 at 02:24 PM.

  25. #145
    New Member
    Join Date
    Apr 2015
    Posts
    4

    Re: Understanding Multi-Threading in VB.Net

    I guess the problem I'm having is the fundamental difference between the example and my goal. Your example uses a for loop iterating I to trigger the CountChanged Event within the Function Count. I have no such function. The serial port object is created and a connection established, then the DataReceived event fires on its own thread. I have been able to intentionally open the serial port on a new thread, but since I'm not in control of its events, I can't (or don't know how to) reacquire the thread to the UI.
    Mind you I can of course, just use invoke on all the UI objects individually, but that's what we are trying to avoid here isn't it?

  26. #146
    New Member
    Join Date
    Apr 2015
    Posts
    4

    Re: Understanding Multi-Threading in VB.Net

    @passel

    Quote Originally Posted by passel View Post
    I assume you're processing data in the DataReceived event, which executes in a thread kicked off by the SerialPort object.
    That's exactly right.

    Yep you lost me. You're suggesting to store the incoming data in a buffer, and only update the GUI periodically?

  27. #147
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Understanding Multi-Threading in VB.Net

    Quote Originally Posted by BrandonL View Post
    ... Your example uses a for loop iterating I to trigger the CountChanged Event within the Function Count. I have no such function. ...
    Which example are you referring to? I have used different flavors of processing at different times, so I'm not sure what we're referencing. Also, if this is a particular discussion of implementation, it probably should be continue in another (new) thread, not this one.

  28. #148
    New Member
    Join Date
    Apr 2015
    Posts
    4

    Re: Understanding Multi-Threading in VB.Net

    Sorry, I should have said THE example, referring to Niya's original examples.

  29. #149
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Understanding Multi-Threading in VB.Net

    I would say, start a new thread, where this can be discussed, and you can show examples etc. without diluting the purpose of the CodeBank thread, which is to provide a working example of code.

Page 4 of 4 FirstFirst 1234

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