-
Dec 2nd, 2014, 03:23 AM
#121
Junior Member
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?
-
Dec 2nd, 2014, 07:02 AM
#122
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>
-
Dec 4th, 2014, 01:18 PM
#123
New Member
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.
-
Dec 4th, 2014, 01:21 PM
#124
New Member
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
-
Dec 11th, 2014, 04:49 PM
#125
Re: Understanding Multi-Threading in VB.Net
Originally Posted by walterwjl
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?
Originally Posted by Irek Janek
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.
-
Dec 15th, 2014, 06:50 AM
#126
Re: Understanding Multi-Threading in VB.Net
Originally Posted by StatShacker
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.
Originally Posted by StatShacker
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.
Originally Posted by StatShacker
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.
-
Feb 24th, 2015, 03:19 PM
#127
New Member
Re: Understanding Multi-Threading in VB.Net
How exchange messages in string threads? Like " ok finish functions" or " not yet wait..."
-
Feb 24th, 2015, 03:52 PM
#128
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.
-
Feb 25th, 2015, 09:47 AM
#129
New Member
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.
-
Feb 25th, 2015, 10:39 AM
#130
Re: Understanding Multi-Threading in VB.Net
Your code looks fairly similar to dbasnett's so check out his Protocol sub in this post
-
Feb 25th, 2015, 12:46 PM
#131
Re: Understanding Multi-Threading in VB.Net
Originally Posted by topshot
Your code looks fairly similar to dbasnett's so check out his Protocol sub in this post
Yea, that seems to be precisely it.
-
Feb 25th, 2015, 02:24 PM
#132
New Member
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!
-
Feb 27th, 2015, 04:02 PM
#133
Re: Understanding Multi-Threading in VB.Net
I don't understand what you're asking.
-
Mar 11th, 2015, 08:14 AM
#134
New Member
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?
-
Mar 13th, 2015, 01:29 PM
#135
Re: Understanding Multi-Threading in VB.Net
You are clearing the DGV every time you check InvokeRequired. Remove that line:-
-
Mar 13th, 2015, 01:32 PM
#136
New Member
Re: Understanding Multi-Threading in VB.Net
Originally Posted by Niya
You are clearing the DGV every time you check InvokeRequired. Remove that line:-
I did (got it when reviewing the code I sent, but I still have the same problem..
-
Mar 13th, 2015, 01:35 PM
#137
Re: Understanding Multi-Threading in VB.Net
Did you check the value of that String array you're passing in ?
-
Mar 13th, 2015, 01:35 PM
#138
New Member
Re: Understanding Multi-Threading in VB.Net
I get a "TargetParameterCountException was hunandled" error...
-
Mar 13th, 2015, 01:37 PM
#139
New Member
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...
-
Mar 13th, 2015, 01:39 PM
#140
New Member
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...
-
Mar 13th, 2015, 03:28 PM
#141
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.
-
Apr 9th, 2015, 11:19 AM
#142
New Member
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!
-
Apr 9th, 2015, 01:26 PM
#143
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.
-
Apr 9th, 2015, 02:21 PM
#144
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.
-
Apr 9th, 2015, 02:21 PM
#145
New Member
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?
-
Apr 9th, 2015, 02:27 PM
#146
New Member
Re: Understanding Multi-Threading in VB.Net
@passel
Originally Posted by passel
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?
-
Apr 9th, 2015, 02:30 PM
#147
Re: Understanding Multi-Threading in VB.Net
Originally Posted by BrandonL
... 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.
-
Apr 9th, 2015, 02:32 PM
#148
New Member
Re: Understanding Multi-Threading in VB.Net
Sorry, I should have said THE example, referring to Niya's original examples.
-
Apr 9th, 2015, 02:42 PM
#149
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|