Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delegates and Threads - Convert C# to VB.Net 1

Status
Not open for further replies.

eatwork

Technical User
May 16, 2005
155
CA
Hi everyone,
I am attempting to learn about threads and multithreading in VB.net and stumbled across a tutorial online. I was following until the article, which is in C# did this:
Code:
        // update the grid a thread safe fasion!
        MethodInvoker updateGrid = delegate
        {
          m_grid.DataSource = ds.Tables(0)
        }

Does anyone know what this is doing and how to translate this into vb.net? thank you
 
No clue, but there a three excellent FAQs here on TekTips that cover Threading:

An Intro to Threading: faq796-5929
An Intro to Threading 2: Delegates faq796-6036
An Intro to Threading 3: Crossing thread boundries (and passing parameters) faq796-6056

Check those out and maybe your question will be answered.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
This is the full function posted in the tutorial:
Code:
    private void CallBack(IAsyncResult ar)
    {
        // get the dataset as output
        DataSet ds = m_invokeMe.EndInvoke(ar);

        // update the grid a thread safe fasion!
        MethodInvoker updateGrid = delegate
        {
            m_grid.DataSource = ds.Tables[0];
        };

        if (m_grid.InvokeRequired)
            m_grid.Invoke(updateGrid);
        else
            updateGrid();
    }
 
Thanks for the post jebenson,
I looked up the FAQ's you posted, although informative, I couldn't really see an answer to my question from there. But I will definately keep those in mind as a future reference. Thanks
 
Hi earthandFire,
thank you for the post. I took a look at the site you have provided, and I am still unable to understand the section where they set the method invoker = to a delegate. What is the purpose of the squigley brackets? Thanks
Code:
        // update the grid a thread safe fasion!
        MethodInvoker updateGrid = delegate
        {
            m_grid.DataSource = ds.Tables[0];
        };
 
Generally:

{ = BEGIN
} = END

In C-based languages, all functions (subs and functions in VB), method, properties, for loops, if statements etc. have there statements in braces. For example:

In VB

If a = 10 Then
b = 5
End If

C-type languages would use something like:
If (a=10)
{
b=5;
};

Hope this helps.

[vampire][bat]
 
eatwork, the reason for that code is to have the update to the m_grid object take place on the thread that owns it. I am not totally familiar with the C# syntax, I have a feeling that there may be a bit more code in that class that is relevant. Chiph is the local C#/VB.Net translation pro, and I know he is familiar with threading in both syntaxes.

If you have any questions regarding threading in VB.Net, I'd be glad to help as I can.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Hi earthAndFire and ThatRickGuy,
Thank you for your posts. I was able to create a seperate function and create a seperate delegate to handle the code above, something to the effect of:
Code:
Private Delegate Sub doUpdateGrid(ByVal ds As DataSet)

Private Sub CallBack(ByVal ar As IAsyncResult)
 '// get the dataset as output
 Dim ds As DataSet = m_invokeMe.EndInvoke(ar)

 Dim updateGrid As doUpdateGrid = New doUpdateGrid(AddressOf updGrid)

 If (m_grid.InvokeRequired) Then
  Dim tag As IAsyncResult = m_grid.BeginInvoke(updateGrid, ds)
 Else
  updateGrid(ds)
 End If
End Sub

Private Sub updGrid(ByVal ds As dataset)
  m_grid.DataSource = ds.Tables(0)
End Sub

Thanks for your help. This threading subject is just hard for me to get my head around. Thanks for your help again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top