Results 1 to 17 of 17

Thread: Threading

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Threading

    I have a textbox which will accept a user input for a email address or phone number.

    I want to using a new thread do a search for various results and populate a listbox.


    I know i need to do this in another thread so my main form will still operate but how can I do this? I've never used threads before and I know it's tricky to get the values from a textbox and also update a list box on another thread.

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

    Re: Threading

    One of the simplest options for multi-threading, especially in a WinForms app, is to use a BackgroundWorker. Follow the CodeBank link in my signature and check out my thread on Using The BackgroundWorker.

    It is easy because it works simply by calling methods and handling events, which we've all done many times before. It's the DoWork event handler that is executed on the secondary thread so the only rule you have to obey is do NOT try to access a control in that event handler.

    My CodeBank example shows you how to pass data into the background operation and also how to pass data out, both during and after the operation.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: Threading

    Quote Originally Posted by jmcilhinney View Post
    One of the simplest options for multi-threading, especially in a WinForms app, is to use a BackgroundWorker. Follow the CodeBank link in my signature and check out my thread on Using The BackgroundWorker.

    It is easy because it works simply by calling methods and handling events, which we've all done many times before. It's the DoWork event handler that is executed on the secondary thread so the only rule you have to obey is do NOT try to access a control in that event handler.

    My CodeBank example shows you how to pass data into the background operation and also how to pass data out, both during and after the operation.
    I will be doing this in WPF probably so don't want to rely on the background worker.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,352

    Re: Threading

    Hmmm... I must have missed where you mentioned that in your first post. Regardless, the BackgroundWorker is not tied to WinForms in any way, shape or form. I've never actually tried it but, as far as I'm aware, it will work just the same in WPF as in WinForms. I'm not sure that you can add an instance in the designer but that's no impediment. Any component can be created in code just as well as in the designer even in WinForms.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: Threading

    Quote Originally Posted by jmcilhinney View Post
    Hmmm... I must have missed where you mentioned that in your first post. Regardless, the BackgroundWorker is not tied to WinForms in any way, shape or form. I've never actually tried it but, as far as I'm aware, it will work just the same in WPF as in WinForms. I'm not sure that you can add an instance in the designer but that's no impediment. Any component can be created in code just as well as in the designer even in WinForms.
    Hmmm likewise I've not asked about the background worker. I've asked about mutli threading so i can learn more for bigger prodjects.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,047

    Re: Threading

    The Background worker IS multithreading, because it runs on a different thread. In fact, lots of things that don't say 'threading' in their names are actually multi-threading. Still, it sounds like what you want is a System.Threading.Thread. Nothing much to that, at the simplest form. You just create the method that you want to have operate on a background thread, create a thread while passing in the AddressOf that method, then run it. Of course, you have to deal with all the other issues that you deal with in multithreading, such as race conditions, but that's what the learning is about. Setting up the thread is pretty trivial. If you want an example of something that uses a simple, raw, thread, in that fashion, you could look in the .NET CodeBank for my UDP class, as it runs a listener on a different thread. Don't expect to be amazed, though, because getting a thread running is pretty dead simple.
    My usual boring signature: Nothing

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,352

    Re: Threading

    Let's try to be clear here. You didn't say that you were using WPF and you didn't say that youj didn't want to use a BackgroundWorker in your first post. In your second post you said the you didn't want use a BackgroundWorker BECAUSE you were using WPF. There is no reason that a BackgroundWorker can't be used in WPF, so that doesn't really make sense. As I and Shaggy have both pointed out, using a BackgroundWorker IS using multi-threading and, as I said, it's the easiest way to do so. If what what you actually want is to learn how to use the Thread class specifically then all you had to do was say so in the first place. That said, you shouldn't really be doing that anyway, if you're using .NET 4. The preferred way to implement multi-threading now is via the new Tasks library. Should we not suggest that either?

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: Threading

    What I asked for was how to use multi threading. if it's in WPF or WinForms it does not matter. You assuemed that the background worker would work for me.

    Perhaps YOU should have said would a backgrounder work and answered my question.

    See this goes both ways mate

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: Threading

    Quote Originally Posted by jmcilhinney View Post
    if you're using .NET 4. The preferred way to implement multi-threading now is via the new Tasks library. Should we not suggest that either?
    No because my systems are not using .NET 4.0 yet.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,352

    Re: Threading

    Quote Originally Posted by AirlineSim View Post
    Perhaps YOU should have said would a backgrounder work and answered my question.

    See this goes both ways mate
    I can only go by the information that you provide. If your application is WPF-specific then it should be posted in the WPF forum. If you post in this forum and say that you're using a TextBox and ListBox and you don't specifically say that it's WPF then it's reasonable for me to assume that you're using WinForms. I know that a BackgroundWorker will work in WinForms and I have no reason to believe that it won't work in WPF too.

    I'm not sure why someone with nearly 800 posts needs to be told to provide all the relevant information. I'm afraid that your threads are too much effort. You won't hear from me again.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,352

    Re: Threading

    Actually, I tell a lie. You'll here from me this one last time. If you follow the CodeBank link in my signature you will find all you need. There is one CodeBank thread about Passing Data Into A Thread Entry Method, which shows you how to pass data from the UI thread into a method executed in a secondary thread. There is another CodeBank thread about Accessing Controls From Worker Threads, which shoes you how to pass data back to the UI thread from that method. That second CodeBank thread even contains an example specific to WPF. Those two CodeBank threads contain all the information you need but, if history is anything to go by, even that won't be enough.

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: Threading

    Quote Originally Posted by jmcilhinney View Post
    I can only go by the information that you provide. If your application is WPF-specific then it should be posted in the WPF forum. If you post in this forum and say that you're using a TextBox and ListBox and you don't specifically say that it's WPF then it's reasonable for me to assume that you're using WinForms. I know that a BackgroundWorker will work in WinForms and I have no reason to believe that it won't work in WPF too.

    I'm not sure why someone with nearly 800 posts needs to be told to provide all the relevant information. I'm afraid that your threads are too much effort. You won't hear from me again.
    I provided you with a question.

    HOw can I use multi threading and take vairbale over from 1 thread to another.

    I would almost prefer to never hear from you again, you never actually help me.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,352

    Re: Threading

    Quote Originally Posted by AirlineSim View Post
    I provided you with a question.

    HOw can I use multi threading and take vairbale over from 1 thread to another.

    I would almost prefer to never hear from you again, you never actually help me.
    I provided you with an answer. You are unwilling to accept that answer but the only reason you have given for that unwillingness is not a valid reason (to be sure, I tested a BGW in a WPF app and it worked fine), so yet again I have tried to help you and you have simply not done what I said. If the only thing that you consider to be help is someone writing your code for you, as seems to be the case, then I wish you luck in your career because you are going to need it.

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: Threading

    jmcilhinney the WPF may work. I never said it didn't. I thought it didn't and that was a very very small factor to why I didn't go there in the first place. The main reason why I don't want to use it is so I can LEARN more on multi-threading without the BGF doing most of the work itself.

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,352

    Re: Threading

    Quote Originally Posted by AirlineSim View Post
    jmcilhinney the WPF may work. I never said it didn't. I thought it didn't and that was a very very small factor to why I didn't go there in the first place. The main reason why I don't want to use it is so I can LEARN more on multi-threading without the BGF doing most of the work itself.
    Then that is all you had to say. We can't read your mind. You said that you wanted to implement multi-threading without any mention of learning anything specific. I suggested the BGW because it is the easiest option. If you had then said something like:
    Thanks for that but I'd like to learn some of the lower-level details of multi-threading so I'd rather not use the BGW.
    which is apparently what you were actually thinking, rather than:
    I don't want to use a BGW because I'm using WPF.
    which is apparently neither here nor there to you or the BGW, then I'd have been happy to provide an alternative. You're asking people who have never met you to volunteer their time to help you so the least you can do is provide all the relevant information so that that time is not wasted. You may not think so but I am trying very hard to provide you with the help that I know that, if used correctly, will benefit you the most in the long run.

    Anyway, the two CodeBank threads of mine that I've previously directed you to provide all you need to work with the Thread class directly and pass data from the UI thread to the background thread and back again, so you have the help you wanted.

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: Threading

    okay; yip you have a point but I jumped to conclutions based on other replies from you. Don't help if you don't want. If you do keep an open mind and so will I.

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,352

    Re: Threading

    You can consider my mind currently open. So, have you checked out those CodeBank threads? Don't make the mistake of trying to go from zero to a hundred in one go. You want to pass data into a background operation, use it and then pass data back out. That's three separate parts to the problem. Tackle each one individually. Create a test project for each one and make sure that each one works in isolation first. When it does, then you can try combining two of them and see if it still works. If it does then put all three together. If that all works then you can try implementing the same principles in your real project. It's this sort of process that makes us software developers and not just code writers.

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