Results 1 to 8 of 8

Thread: Code FOr CroSS THreAD

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    182

    Code FOr CroSS THreAD

    I would like some code and how to place it in my windows form

    This is inside a thread, that is called by button 2

    vb.net Code:
    1. If portdata2.Rows.Item(sum).Cells.Item(1).Value = "" Then
    2.                             portdata2.Rows.Add(1)     ''' i want to add a row to this datagridview
    3. end if

    when my code arrives this line it gives me the cross thread error





    What i srt of understand its that in
    portdata2.rows(1) instead of this i should place a code like invoque something

    and in my form i should add a sub where invoque is compare...

    i just dont know how


    I dont want link to read about how to do it..because i just dont get it...
    i have always have trouble with this safe thread calls so i want someone to teach me with lil apples..

    This goes here and so on..

    PLZZZZZZZZZZZZZZZZ
    Last edited by justme369; Jul 9th, 2010 at 02:12 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Code FOr CroSS THreAD

    So what you're saying is that you're not interested in learning how to do it and understanding it, but rather you just want someone to write the code for you, correct? If that's the case then I'm afraid I can't help you but, if you are interested in learning, I definitely can. Follow the CodeBank link in my signature and check out my Accessing Controls From Worker Threads post. It provides a clear explanation and step by step instructions. If you follow those instructions step by step then it will work. If you don't complete the first step before moving on then you can expect it to likely fail, so don't. Each step is simple so there's no reason that you can't at least perform the first step and post your results here. If you can do that then I'm more than willing to help you go further, but if you just expect it happen of its own accord then you'll be sadly disappointed.

    To provide an analogy that might give you a better understanding of what you're doing and why, think of a thread as a person on a production line. Each person can only do one job at a time, so if you have one person working then you are limited to the amount of work that that person can perform. If you have that person working on task A then they can't simultaneously work on task B.

    If you hire another person then you can get twice as much work done. You might have one person working repeatedly on task A while the other works repeatedly on task B or you might have both working alternately on task A and task B.

    Now, let's imagine that those two people are working on either side of a window. They can see each other but they can't touch. There's a particular tool that needs to be used to complete a task but there's only one of them. Let's say that it is on the first person's side of the window. That person can pick up the tool and use it to perform the task. The other person can't though. They reach out for the tool and they bump into the window. They just checked whether they could use the tool or not and found that they couldn't. As such, they can't complete the task. They then call over someone else and ask them to go around to the other side of the window and ask the other person to use the tool to complete the task. That person walks around to a door and crosses to the other side of the window and asks the person who does have access to the tool to complete the task. The second person delegated a task to the first person because they had access to the tool. Does that sound similar to any other situation? Like maybe a secondary thread delegating a task to the UI thread because it has access to the controls on the form?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Code FOr CroSS THreAD

    That is one convoluted metaphor. I prefer my flaming highway of death metaphor :P

  4. #4

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    182

    Re: Code FOr CroSS THreAD

    thank you jmcilhinney..is not that i dont want to learn i doo...i like your example..is just that i needed examples like 1 apple + 2 apples= 3 apples...sort of ,for kids..cuz this part multi threding i dont understand much ..im still learning...and thiis site and ppl like you have help me a lot, obviously showing me through the right direction, ill read the link you suggested

    thank you all for your replys..

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Code FOr CroSS THreAD

    Multi-threading is not fruit. It is complicated. My CodeBank submission breaks it down about as simply as is possible. If you follow the steps I provided then it will work every time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Code FOr CroSS THreAD

    Quote Originally Posted by Emcrank View Post
    You need to use delegates, i suggest you search google.
    This is a prime example of the kettle calling the pot black.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  8. #8
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Code FOr CroSS THreAD

    Quote Originally Posted by jmcilhinney View Post
    Multi-threading is not fruit.
    Quote of the day
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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