PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 [RESOLVED] Better Multi-Threads For My Approach?-VBForums
Results 1 to 22 of 22

Thread: [RESOLVED] Better Multi-Threads For My Approach?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    337

    Resolved [RESOLVED] Better Multi-Threads For My Approach?

    Greetings, hope all are fine...

    here is the scenario (just a sample to let you understand)
    i have a listview control with few items.
    List view has say 2 columns.
    1. File Name
    2. conversion status.

    Steps With Out Multithreading...
    _______________________________

    sub button1_click()
    for intLoop = 1 to lstView.listitems.count
    sConvertFile(intLoop)
    next intloop
    end sub

    sub sConvertFile(index as integer)
    rem do the conversion and update the progress by changing the list view related items
    end sub

    but that goes one by one .. and that's where i want to do multithreading...

    for example i want to use 5 (will vary) threads .. that will do the conversation 5 (will vary) file a time and update the conversion progress on listview..

    i am very sorry for the very rough idea.. but i hope that is enough to get the idea what i want to achieve?

    any help/walkthrough will be highly appreciated

    thanks in advance

    best regards

  2. #2
    Hyperactive Member
    Join Date
    May 2007
    Posts
    300

    Re: Better Multi-Threads For My Approach?

    Use a BackgroundWorker.

    Just search here for BackgroundWorker and you'll see many examples.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    337

    Re: Better Multi-Threads For My Approach?

    Quote Originally Posted by Meestor_X View Post
    Use a BackgroundWorker.

    Just search here for BackgroundWorker and you'll see many examples.
    thanks for your reply. but as i told i need multiple (number will vary from 1 to 100 threads) so, is background worker is good for that? do i have to create 100 background worker for that?

    however thanks again for your help...

    best regards

  4. #4
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Better Multi-Threads For My Approach?

    have you come across Parallel.ForEach?
    W o t . S i g

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    337

    Re: Better Multi-Threads For My Approach?

    Quote Originally Posted by Milk View Post
    have you come across Parallel.ForEach?
    thanks for your reply

    but i did a bit research and it seems parallel is much more complex than multithreading

    thanks again for your reply...

    best regards

  6. #6
    Hyperactive Member
    Join Date
    May 2007
    Posts
    300

    Re: Better Multi-Threads For My Approach?

    The BackgroundWorker can be part of the class that makes up each individual item that needs conversion, so no need to separately create 100's of them, one will be created each time you create a new item.

    vb.net Code:
    1. Class MyClass
    2.    Property FileName as String = ""
    3.    Property ConversionStatus as Integer = 0
    4.    Private WithEvents BGWorker as New BackgroundWorker
    5.  
    6.    Sub New()
    7.       AddHandler BGPlayer.DoWork, AddressOf BGPlayer_DoWork
    8.    End Sub
    9.  
    10.    Private Sub ConvertMe
    11.       BGWorker.RunWorkerAsync()
    12.    End Sub
    13.  
    14.    Private Sub BGWorker_DoWork(sender as Object, e as DoWorkEventArgs)
    15.       While Not e.Cancel
    16.          ' Do the conversion
    17.       End While
    18.    End Sub
    19.  
    20. End Class

    You'll need to work out how to use ReportProgress to update your ConversionStatus and your listbox.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,893

    Re: Better Multi-Threads For My Approach?

    Forget about the number of threads, because you won't be able to control that without doing something bad. If your computer has four cores, then the most processes you can profitably do at the same time is four. You can have more threads, and sometimes it is a good idea, but you can't do more simultaneous actions than the number of cores you have. If each thread spends a significant amount of time waiting for something, such as a webservice call, then you can profitably use many more threads than you have cores, but it doesn't sound like you have that situation. It sounds like each thread could run without interruption, in which case if you make more threads than you have cores, you will actually slow your program down as the threads swap their context in and out of the CPU with every thread change.

    Also, if you are going to be interacting with the Listview, which is on the UI thread, then all of the threads will be taking turns at the UI thread, which will probably slow everything down even more. You could improve performance, in that case, by holding the information in variables that are not controls.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    337

    Re: Better Multi-Threads For My Approach?

    Quote Originally Posted by Shaggy Hiker View Post
    Forget about the number of threads, because you won't be able to control that without doing something bad. You could improve performance, in that case, by holding the information in variables that are not controls.
    thanks a lot for your informative reply
    however.. yes sir, in my post i told that is just a sample. In real case i would use web service call but yes that is truth too that in some cases i may use file interaction too (like conversation etc)

    and also i have seen many apps running that use multiple threads and updating multiple list items at the same time

    so, it would be far great if you would be enough kind to help me out with a solution please?

    thanks in advance

    best regards

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    337

    Re: Better Multi-Threads For My Approach?

    Quote Originally Posted by Shaggy Hiker View Post
    Also, if you are going to be interacting with the Listview, which is on the UI thread, then all of the threads will be taking turns at the UI thread, which will probably slow everything down even more. You could improve performance, in that case, by holding the information in variables that are not controls.
    Please sir, check out the attached image:

    Name:  IDM-Multithreads.jpg
Views: 66
Size:  38.0 KB

    the screenshot is taken from a very well known download manager named IDM.

    for demonstration i have download four file with multiple (8) threads (simultaneous connection).

    if you look closely you will notice that, each download window update it's own window status and also the main window listview items status too. ( i have marked the status area of each download with a color)

    and it preforms fast enough on my pc where i have only 2 core

    so, i think i am not looking for very bad thing right?

    please sir, help me out...

    thanks in advance..

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,893

    Re: Better Multi-Threads For My Approach?

    Working with web services or files gives you much more flexibility, because the processes are not so CPU intensive, which allows them to share much better.

    I can't see that screen shot in any detail, since it's too small, but I believe the text isn't important. I can see that there are multiple windows. It may all be asynchronous processes rather than strictly thread based. That seems to be the preferred method for that kind of thing. There are others on here with more experience with simultaneous file downloads, so I'll step out, as they'll be more helpful (plus, I'm going to sleep).
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    337

    Re: Better Multi-Threads For My Approach?

    Quote Originally Posted by Shaggy Hiker View Post
    Working with web services or files gives you much more flexibility, because the processes are not so CPU intensive, which allows them to share much better.

    I can't see that screen shot in any detail, since it's too small, but I believe the text isn't important. I can see that there are multiple windows. It may all be asynchronous processes rather than strictly thread based. That seems to be the preferred method for that kind of thing. There are others on here with more experience with simultaneous file downloads, so I'll step out, as they'll be more helpful (plus, I'm going to sleep).
    the image is large enough but however this forum shows at smaller size. however i have attached it so, you can view in detail..

    once again thanks a lot for your reply
    Attached Files Attached Files
    Last edited by Shohag_ifas; Jan 4th, 2015 at 09:12 PM.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    337

    Re: Better Multi-Threads For My Approach?

    Quote Originally Posted by Meestor_X View Post
    The BackgroundWorker can be part of the class that makes up each individual item that needs conversion, so no need to separately create 100's of them, one will be created each time you create a new item.

    vb.net Code:
    1. Class MyClass
    2.    Property FileName as String = ""
    3.    Property ConversionStatus as Integer = 0
    4.    Private WithEvents BGWorker as New BackgroundWorker
    5.  
    6.    Sub New()
    7.       AddHandler BGPlayer.DoWork, AddressOf BGPlayer_DoWork
    8.    End Sub
    9.  
    10.    Private Sub ConvertMe
    11.       BGWorker.RunWorkerAsync()
    12.    End Sub
    13.  
    14.    Private Sub BGWorker_DoWork(sender as Object, e as DoWorkEventArgs)
    15.       While Not e.Cancel
    16.          ' Do the conversion
    17.       End While
    18.    End Sub
    19.  
    20. End Class

    You'll need to work out how to use ReportProgress to update your ConversionStatus and your listbox.
    sir, it would be great if you kind be bit more helpful to give the more example/source code to also update the listview status

    would you please?

    thanks in advance

    best regards

  13. #13
    Hyperactive Member
    Join Date
    May 2007
    Posts
    300

    Re: Better Multi-Threads For My Approach?

    I'm not going to write your program for you. Google multi threading forms vb.net and you'll see plenty of examples.
    Here's one to get you started.
    http://www.codeproject.com/Articles/...inner-s-choice

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    337

    Re: Better Multi-Threads For My Approach?

    Quote Originally Posted by Meestor_X View Post
    I'm not going to write your program for you. Google multi threading forms vb.net and you'll see plenty of examples.
    Here's one to get you started.
    http://www.codeproject.com/Articles/...inner-s-choice
    Thanks again for your kind reply..

    yes i am not asking you to code the program for me. just asking to guide me a right and it's seems you did.

    i am investigating the code and it seems this what i need...

    just a request, did you check the code yourself and can you tell me if is it correct approach or is there any better idea?

    thanks in advance

    best regards

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    337

    Re: Better Multi-Threads For My Approach?

    Quote Originally Posted by Meestor_X View Post
    I'm not going to write your program for you. Google multi threading forms vb.net and you'll see plenty of examples.
    Here's one to get you started.
    http://www.codeproject.com/Articles/...inner-s-choice
    HI, now i am in a situation that needs help..

    I can stop a thread by force using that thread object.Abort call. but that is not a good call at all situation

    what if i want to stop the thread for it self. Because Abort call interrupt the thread in middle way.. so, what i want to is..
    my main ui set a variable like bolStop = True. And my thread can check that variable and when it is true it can do it's cleanup and stop.

    is there any way to do that? i hope so

    thanks in advance...

    best regards

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    337

    Re: Better Multi-Threads For My Approach?

    Hi, as told in the previous post i am having issues with stop the running thread. as it old i don't want to use thread.abort call..

    here i attached the test project. it working nice.. just i want to when user click on "Stop" button all running thread can get the call and do the cleanup process and stop the thread it self.

    i hope i am clear..

    please check out the attached project that will help you understand better

    any help would be highly appreciated..

    thanks in advance

    best regards
    Attached Files Attached Files

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    337

    Re: Better Multi-Threads For My Approach?

    hello? any help please?

  18. #18
    Hyperactive Member
    Join Date
    May 2007
    Posts
    300

    Re: Better Multi-Threads For My Approach?

    Really? Please do some searching. It takes only a second.

    http://msdn.microsoft.com/en-us/libr...=vs.95%29.aspx

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Posts
    337

    Re: Better Multi-Threads For My Approach?

    Quote Originally Posted by Meestor_X View Post
    Really? Please do some searching. It takes only a second.

    http://msdn.microsoft.com/en-us/libr...=vs.95%29.aspx
    i am sorry, if i am bothering you a lot. However thanks a lot for your fast reply.

    And it's seems you are very good at search than me. However that's not what i need. As the previous link you gave me i worked based on that.

    and that source/idea don't rely on background worker. It's rely one System.Threading.Thread

    so, i kindly request you to check my source. I think it is matter of minutes if you willing to help me out

    thanks in advance

    best regards

  20. #20
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Better Multi-Threads For My Approach?

    When you want a thread to stop, set a boolean flag that the thread routine checks. If it's true, then it stops what it is doing and exits.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  21. #21
    Hyperactive Member
    Join Date
    May 2007
    Posts
    300

    Re: Better Multi-Threads For My Approach?

    Just use a backgroundworker.

    You really need to take some time to read up on Backgroundworker and try some of the examples.

  22. #22
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,329

    Re: Better Multi-Threads For My Approach?

    Quote Originally Posted by Shohag_ifas View Post
    Please sir, check out the attached image:

    Name:  IDM-Multithreads.jpg
Views: 66
Size:  38.0 KB

    the screenshot is taken from a very well known download manager named IDM.

    for demonstration i have download four file with multiple (8) threads (simultaneous connection).

    if you look closely you will notice that, each download window update it's own window status and also the main window listview items status too. ( i have marked the status area of each download with a color)

    and it preforms fast enough on my pc where i have only 2 core

    so, i think i am not looking for very bad thing right?

    please sir, help me out...

    thanks in advance..
    Interesting. I just viewed the features of that manager and One of them is to have Ccleaner run after download to check for viruses :/

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width