TRC is primarily funded by ad revenue. If you like the content you find here, please do not block our ads. Thank you.
Results 1 to 11 of 11

Thread: Multithread VB.Net handling

  1. #1
    Should Get Out More Kebab The Cat's Avatar
    Join Date
    Jan 2008
    Location
    Live and work in the midlands
    Posts
    5,519
    Like
    0
    Liked 0 in 0 posts
    Blog Entries
    3

    Default Multithread VB.Net handling

    :/

    If I've got this at the top of the class,
    Code:
    Delegate Sub myJobManagerDelegate(ByVal sender As Object, ByVal e As CogJobManagerActionEventArgs)
    Why doesn't the standard
    Code:
    If InvokeRequired Then
                Dim myDel As New myJobManagerDelegate(AddressOf myJobManager_UserResultAvailable)
                Dim eventArgs() As Object = {sender, e}
                Invoke(myDel, eventArgs) 'what's the problem? it's the same as the example file??
                Return
            End If
    work?

    I'm getting an "Index was outside the bounds of the array" error.

    My Brian hurts. This bit of code works in other subs.



  2. TRC is primarily funded by ad revenue. If you like the content you find here, please do not block our ads. Thank you.
  3. #2
    Not Much To Do Pugwash's Avatar
    Join Date
    Jan 2008
    Location
    The High Seas
    Posts
    995
    Like
    0
    Liked 0 in 0 posts

    Default Re: Multithread VB.Net handling

    Have you tried changing "Invoke(myDel, eventArgs)" to "myDel.invoke(eventargs)" ?

    edit:

    or even just myDel (sender,e)?

  4. #3
    Should Get Out More Kebab The Cat's Avatar
    Join Date
    Jan 2008
    Location
    Live and work in the midlands
    Posts
    5,519
    Like
    0
    Liked 0 in 0 posts
    Blog Entries
    3

    Default Re: Multithread VB.Net handling

    Cheers dude, but it's all a no go. This type of looking after the possibility that a different thread might try and access the process other than the one who started it has got my head in a bit of a mash. Setting up the pointers and whatnot is all a bit new.

    Bring back VB6, all is forgiven

  5. #4
    Not Much To Do Pugwash's Avatar
    Join Date
    Jan 2008
    Location
    The High Seas
    Posts
    995
    Like
    0
    Liked 0 in 0 posts

    Default Re: Multithread VB.Net handling

    Ok. The only other thing I could think of is that the 'e' you are sending into the delegate doesn't matching the signature of 'CogJobManagerActionEventArgs'.

    Good luck

  6. #5
    Should Get Out More Kebab The Cat's Avatar
    Join Date
    Jan 2008
    Location
    Live and work in the midlands
    Posts
    5,519
    Like
    0
    Liked 0 in 0 posts
    Blog Entries
    3

    Default Re: Multithread VB.Net handling

    Well, I'm basing that chunk of code on the helpfiles and examples distributed by the monufacturers of some hardware.

    If you're really feeling helpful, you could compare the C# example I've got...
    Code:
       private void myJobManager_UserResultAvailable(object sender, CogJobManagerActionEventArgs e)
              {
                  if (InvokeRequired)
                  {
                      Invoke(new myJobManagerDelegate(
                        myJobManager_UserResultAvailable),new object[] 
                        { sender, e });
      
                      return;
      
                  }
    with what I've tried to do in VB
    Code:
    If InvokeRequired Then
                     Dim myDel As New myJobManagerDelegate(AddressOf myJobManager_UserResultAvailable)
                     Dim eventArgs() As Object = {sender, e}
                     Invoke(myDel, eventArgs) 'what's the problem? it's the same as the example file??
                     Return
            End If
    and tell me where I'm going wrong...

    Ta

  7. #6
    Not Much To Do Pugwash's Avatar
    Join Date
    Jan 2008
    Location
    The High Seas
    Posts
    995
    Like
    0
    Liked 0 in 0 posts

    Default Re: Multithread VB.Net handling

    I'm a bit stuck without context and the rest of what you're doing.

    Which line does it actually choke on? What's actually getting passed in CogJobManagerActionEventArgs? If invoke isn't required because you are on the the first thread, does it work?

    My approach would be to poke it with a stick until it does something different and take it from there....

  8. #7
    Not Much To Do Pugwash's Avatar
    Join Date
    Jan 2008
    Location
    The High Seas
    Posts
    995
    Like
    0
    Liked 0 in 0 posts

    Default Re: Multithread VB.Net handling

    Delegate Sub myJobManagerDelegate(ByVal sender As Object, ByVal e As Object)


    Private Sub myJobManager_UserResultAvailable(ByVal sender As Object, ByVal e As Object)
    'If InvokeRequired Then
    Dim myDel As New myJobManagerDelegate(AddressOf myJobManager_UserResultAvailable)
    Me.Invoke(myDel, New Object() {sender, e})
    Return
    ' End If

    End Sub

    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim newsender As New System.Object
    Dim newE As New EventArgs
    myJobManager_UserResultAvailable(newsender, newE)
    ........


    When I run the above it works fine (as long as I stop it recursing off the top of the stack) So I would stick to my guess that it's to do with your arguments. Don't know whats in CogJobManagerActionEventArgs or whatever it was, so can't check that

  9. #8
    Should Get Out More Kebab The Cat's Avatar
    Join Date
    Jan 2008
    Location
    Live and work in the midlands
    Posts
    5,519
    Like
    0
    Liked 0 in 0 posts
    Blog Entries
    3

    Default Re: Multithread VB.Net handling

    We're falling over on the Invoke(myDel,eventArgs) with an array error - the index was out of bounds usually.

    I'm a little lost tbh, I'm trying to glue example files into something useful, and it's slow going. I'll be off on a course in a week or two, which will hopefully make at least their methods clearer, but for noew, it's a pain in the arse.

    This is the best example I can find:
    Code:
       private void myJobManager_UserResultAvailable(object sender, CogJobManagerActionEventArgs e)
              {
                  if (InvokeRequired)
                  {
                      Invoke(new myJobManagerDelegate(
                        myJobManager_UserResultAvailable),new object[] 
                        { sender, e });
      
                      return;
      
                  }
      
                  Cognex.VisionPro.ICogRecord topRecord = myJobManager.UserResult();
      
                  RunStatusTextBox.Text =  topRecord.SubRecords["UserResultTag"].Content + ": " 
                    + topRecord.SubRecords["JobName"].Content + " --> " 
                    + topRecord.SubRecords["RunStatus"].Content.ToString();
      
                  double distance = (double)topRecord.SubRecords["Tools.Item[\"CogDistancePointLineTool1\"].CogDistancePointLineTool.Distance"].Content;
      
                  Console.WriteLine("Distance = " + distance.ToString());
      
                  Cognex.VisionPro.ICogRecord tmpRecord;
                  // Assume the required record is present and get it.
                  tmpRecord = topRecord.SubRecords["ShowLastRunRecordForUserQueue"];
                  tmpRecord = tmpRecord.SubRecords["LastRun"];
                  tmpRecord = tmpRecord.SubRecords["Image Source.OutputImage"];
                  cogRecordDisplay1.Record = tmpRecord;
                  cogRecordDisplay1.Fit(true);
              }
    but that's obviously C# and I need to be in VB. So I'm trying to convert, and everything else works apart from the invoke part (which sometimes works - I assume when invoke isn't required), which is supposed to be dealing with the multithreading issue. (the whole problem of which seems to be a ridiculous mess - why isn't it sorted in the framework of .NET, rather than leaving it for monkeys to do each and every time?)

    Anyway, it's Friday afternoon and I'll just bounce around this a few more times and go home.. Beer is calling me..

    Thanks for your thoughts..

  10. #9
    Should Get Out More Kebab The Cat's Avatar
    Join Date
    Jan 2008
    Location
    Live and work in the midlands
    Posts
    5,519
    Like
    0
    Liked 0 in 0 posts
    Blog Entries
    3

    Default Re: Multithread VB.Net handling

    Quote Originally Posted by Pugwash View Post
    Don't know whats in CogJobManagerActionEventArgs or whatever it was, so can't check that
    That's from a company's library that I've referenced. I'm not sure I can show you much more without you installing tonnes of stuff from a company you've never heard of.

    Best I go and have a pint I think....

  11. #10
    Not Much To Do Pugwash's Avatar
    Join Date
    Jan 2008
    Location
    The High Seas
    Posts
    995
    Like
    0
    Liked 0 in 0 posts

    Default Re: Multithread VB.Net handling

    Having seen your code, I reckon the problem could be the scope of the variables used in the function. If you are referencing any variables expecting them to be global but they're actually class level, then you could get the kind of random issues you are seeing. Rem out all the code* in the function and put in a 'hello world' to the console. If that works, then add lines back in one at a time and see where it falls over. Check the scope of the variables on that line and you may be lucky. Now where's that beer?



    * not the invokedrequired bit, obviously

  12. #11
    Should Get Out More Kebab The Cat's Avatar
    Join Date
    Jan 2008
    Location
    Live and work in the midlands
    Posts
    5,519
    Like
    0
    Liked 0 in 0 posts
    Blog Entries
    3

    Default Re: Multithread VB.Net handling

    Arsebiscuits! The problem was entirely elsewhere, and a simple syntax one at that. The debugger was misleading me as to where the error was.

    Got to love those cross thread delegate invocation issues

Go Back to Forum My Forum