Dalegate frustrations

fj1200

Member
Joined
Sep 1, 2010
Messages
13
Programming Experience
1-3
Right then - all the previous code is now working perfectly - thanks for all the help guys. However - I'm really having a hard time with delegates. I sort-of understand it, but the way I have my code set up at present, all the calculations and database inserts are done on the thread that reads the serial port rather than the main form, where I want to use the values to plot as points on a graph. Is that the best way to do it or doesn't it matter?

I simply need to get the final values - maxIndex and maxInteger - into a text box and I can work it from there, but no matter what I try it won't work. Been trying for 3 days, spent a lot of time reading tutorials and looking at code examples, still none the wiser really. Don't mean to be thick but it seems I'm not alone in this. Frustration is getting the better of me and I've held off posting here but can't any longer. Can anyone assist? See previous post 2 days ago for code snippets.
 
I'm assuming "Dalegate" is just "Delegate" mistyped.

Now what I'm wondering is what your question is exactly. Your post title is about a frustration with delegates, but your question doesn't mention delegates anywhere.

You talk about two different threads, one of which being the main form thread, and in a windows form project it's sometimes annoying to update the display from a back thread because there is an "invoke required" error. In which case you can call the "Invoke(...)" method to update that display which takes a Delegate as a parameter so you can perform some action.


But you don't really ever specifically refer to delegate OR invoke required, so I have NO idea if that's what you're asking.


What I can do is give you a basic definition of what a delegate is.

A delegate is basically just a reference to a method (sub or function). Just like objects are reference types, and structures can be passed by reference, methods can be wrapped/boxed and given a reference. That reference being a delegate. The Delegate takes care of the job of describing what the input parameters and return result of the method may be (sub's have a return of void... or no return).

You can define your own Delegate with the Delegate keyword, or you can use any of the variousily pre defined Delegates. Most of which can easily cover most of these invoke tasks you may need... check out 'Action' and 'Func' delegates:

Action:
Action Delegate (System)

Actions that allow to define a param:
Action(T) Delegate (System)

There's more Action with expansions on the param options

Func:
Func(TResult) Delegate (System)

Func that allow to define a param:
Func(T, TResult) Delegate (System)

Just like Action, there's more Func delegate types that expand on the available params.
 
Hi Lordofduct

Sorry - I should be clearer.
1) - yup - typo.
2) - As I understand it the COM port data is dealt with on a separate thread to the main form and I have to pass that data back to the main form, which - as I understand it - requires the use of delegate. I'm no programmer but have a previous VB6 and a little VB.Net experience - I'm muddling through trying to understand it as I go. I'm finding it a bit confusing - I just need to pass a value from a serial port to a text box - how hard can it be? I'll have a look through your links anyway and see how I get on. Getting too close to the weekend now and my brain hurts...
 
- how hard can it be?
As easy as can be ;)

Example method signature:
VB.NET:
Sub UpdateUI(value As Integer)
Delegate signature is given:
VB.NET:
Delegate Sub UIHandler(value As Integer)
Can you see the similarity?

Now you can create an instance of the delegate type and point it to your method:
VB.NET:
Dim handler As New UIHandler(AddressOf UpdateUI)

A little diversion here, the delegate instance points to a method and can be called (invoked):
VB.NET:
handler.Invoke(123)
This is equivalent to calling the method directly:
VB.NET:
UpdateUI(123)
This may at first seem like a dumb idea (why not just call the method?), but this allows as mentioned the method to actually be called to be assigned later dynamically. This is also how event handlers work, any method can be set up to handle an event, and the class that defines it can call them simply by invoking the delegate.

Back on track, when you need to have a method called in UI thread you use a delegate instance like above and a ISynchronizeInvoke instance, usually a Control instance - which is why very often this referred to as 'Control.Invoke'. For example you have a MainForm instance you can invoke the UpdateUI method on this thread:
VB.NET:
MainForm.Invoke(handler, 123)
 
Hi JohnH... That's what I've been looking for - clear, clean concise...

Once I get this working I'll probably be fine, and I think I understand the principles behind it, but getting all the pieces in the right places with the right syntax is proving difficult. For me anyway. But I really do appreciate all the help.

OK - Seriously - not trying to be thick here - I have this sub (below) that gets the serial port data... I've moved all the number crunching code and DB insert to the main form and will do that off the textbox I think. Avoids (even more) confusion, although I think I'd rather have it on the originating thread.

Trying to understand your example. Am I right with the Delegate here - what should the textbox signature be if the sub has a SerialDataRecieved sig. as I get errors saying they should be the same?

VB.NET:
    Delegate Sub DataReceived()
    Dim handler As New DataReceived(AddressOf txt_DataReceived)

    Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
       Handles serialPort.DataReceived
        Dim densReading As String = serialPort.ReadLine()
    End Sub

(BTW - what is 'UpdateUI'? Is it a Sub, textbox, etc - don't really understand. Sorry.)

I have a text box txt_DataRecieved, that I want to populate so how would I get the data into it?

So then on the main form would I have a Mainform.Invoke(handler) - would that have to be driven by the originating thread though - if they are on 2 threads how would the mainform know that data has been recieved if that's handled seperately?

Sorry if I'm spouting drivel - it's been a very long week and I've been going round in circles with this so much I don't know which way is up any more! :confused:
 
Last edited:
Your DataReceived function is fine:

VB.NET:
    Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
        Handles serialPort.DataReceived

        Dim densReading As String = serialPort.ReadLine()

    End Sub

When you make a Delegate it's common practice to take the method name and append handler to it.

VB.NET:
Delegate Sub DataReceivedHandler(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)

From there you can create an instance of your handler and point it to your original method.

VB.NET:
Dim receivedHandler As New DataReceivedHandler(AddressOf DataReceived)

From there you can go about using Invoke as described by Lordofduct and JohnH.
 
fj1200 said:
Private Sub DataReceived(blah blah) Handles serialPort.DataReceived
DataReceived event is raised on a secondary thread, so you can't just update UI from here, you must call a method that does the UI updating. Write the method you want to call now, you can for examle name it UpdateUI.
Also, you can't just call this method directly, you must use ctrl.Invoke to call the method so that it is executed in the correct thread. The example I posted should help you achieve this.
fj1200 said:
(BTW - what is 'UpdateUI'? Is it a Sub, textbox, etc - don't really understand. Sorry.)
me previous post said:
Sub UpdateUI(value As Integer)
fj1200 said:
OK - Seriously - not trying to be thick here
I don't really see what is even slightly difficult about this, but it is a frequently asked question, so I guess I'm just very smart :D

Don't know what UI is/means? It is a common abbreviation for User Interface, which means forms and controls and such, these are in particular a GUI = Graphical User Interface.
 
Don't know what UI is/means? It is a common abbreviation for User Interface, which means forms and controls and such, these are in particular a GUI = Graphical User Interface.

I'm an IT Manager and sysadmin, NOT a programmer, and don't have any programmers to hand to assist. Struggling in very unfamiliar territory - I'm more used to PHP but business requirements dictate otherwise. I was simply unsure whether UpdateUI was part of a command, or a name of a component. That's all. It might be simple to you, but I spent 3 days going round in circles last week trying all kinds of things and not getting it right, so frustration was getting the better of me.
 
Back
Top