Results 1 to 3 of 3

Thread: [RESOLVED] [2005] Small problem with thread

  1. #1

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Resolved [RESOLVED] [2005] Small problem with thread

    I've found an open source user control which is effectively a clone (almost) of the VB6 Winsock Control. Yes I know theres many out but this is the only one I can get working - sort of. Find it here:
    http://www.codeproject.com/vb/net/winsockdotnet.asp

    You remember the VB6 winsock events? Connected, DataArrival etc etc....well this .Net control has those too, and here's the problem:

    When I place some simple code within the event such as:
    VB Code:
    1. Private Sub Winsock1_Disconnected(ByVal sender As Winsock_Control.Winsock) Handles Winsock1.Disconnected
    2.         Label1.Text = "dc"
    3. 'when the connection is disconnected, set the text of the label
    4.     End Sub

    It throws an exception:
    Cross-thread operation not valid: Control 'Label1' accessed from a thread other than the thread it was created on.

    Surely I should be able to access my controls from the events, otherwise the events here, are pretty much useless??

    Thanks, any help is much appreciated as always
    Chris

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2005] Small problem with thread

    Yes and no.... I think what's happening is that the "winsocks" are being spun off into a thread separate from the main app. As a result, the event fires off in that thread, rather than your own, which is why you can't access Label1.

    I'm not sure how to fix it though. I'm guessing that the callback will need to be setup differently.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: [2005] Small problem with thread

    Hi tg, yes thats exactly the problem, the user control creates another thread(s). I have found a newer version of the control, which is for 2005 and is thread safe, so no problems with it.

    If anyone wants it, its here:
    http://www.codeproject.com/useritems/winsock2005.asp

    Cheers mate
    Chris

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width