Threading Vs. Delegates

  • Thread starter Thread starter Phil G.
  • Start date Start date
P

Phil G.

I was recently struggling to adapt an example I have using delegate methods,
IasynResult and AsynCallback.

Doing a little research I came across an example, which in fact was being
used to return data from an external sql database...exactly what I am doing.
This example used the System.Threading namespace and thread.start etc

What are the pro's and con's of using/choosing either option? They both
'appear' to create a process on a new thread. Is it that you have more
control with delegates?

Thanks for any responses, Phil
 
Hi Phil,

I have a multi-threaded app that pulls data from a non-relational source and
writes this data to multiple Sql Server tables simultaneously - using multi
threading. This works quite well. I will show you what I do - and request
if you could show me your delegate example:

Sub xyz()
....
TNcd = New Thread(AddressOf Me.ThreadNcalCaseDetailed)
TNcd.Start()
End Sub

Sub ThreadNcalCaseDetailed()
Dim LP As New clsNcalincidents("cd")
Application.DoEvents()
LP.GetNotesData(lbl0) '--write count of records pulled to Me.lbl0
LP = Nothing
End Sub

The thread TNcd starts the sub ThreadNcalCaseDetailed(). Here I
instantiate my data pulling class and pass a argument specifying what Sql
Server table the class will write to -- "cd" is the designation for the table
for this sub. I call multiple subs simultaneously -- TNcm, TNqa... all call
the same class -- clsNcalincidents("cd"), and pass in different arguments to
the class constructor for different tables.

How does your delegate routine compare to this routine?

Rich
 
Rich,

I killed of my delegate code in favour of threading, not because I prefer
the threading NS but because I couldn't get the darn thing working ;-)

So, here's the gist from the book I have:-

'declare delegate function. Must have same signature as method it is holding
reference to.
Delegate Function GetSQLdataDelegate(ByVal db as string) as dataset

Dim delGetSQLdata as GetSQLdataDelegate
Dim ACbk as AsynCallback
Dim IARes as IAsyncResult

Private Sub Load..........'Form Load method
ACbk = New AsyncCallback(AddressOf GetSQLdataCompleted)
delGetSQLdata = New delGetSQLdataDelegate(AddressOf Class.Method)
'Class.Method takes a string(db connection) and returns a dataset
End Sub

'Here we start the new process. As can be seen from above, once it completes
it calls the sub below
Private Sub btnGetData_Click......
IARes = delGetSQLdata.BeginInvoke(db,ACbk,Nothing)
End Sub

Sub GetSQLdataCompleted (ByVal IARes as IAsyncResult)
Dim ds as new dataset
ds = delGetSQLdata.EndInvoke(IARes)
End Sub

Rgds, Phil
 
Thanks. I'll try to make your sample code do something. I'm just looking
to gain a little more understanding of delegate usage. I'm sure there are
occasions where delegates are useful - or maybe delegates are getting
deprecated?

Rich
 
i have seen 3 tier aplication that make use of delegates, but i dont have
any expample right now, because i dont work there any more...

but all the data was getting by events, i was fine, but has their cons...

the pros, are that you dont must to wait to do other thing... you could save
data and at the same time getting data, and when their complete a msgbox
indicating that...

cons, if you make to much events, then it is a little difficult to read, and
to follow up the code... i didnt think that there are any improvement...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top