Results 1 to 33 of 33

Thread: thread pooling

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    thread pooling

    i am trying to do thread pooling with:
    Code:
    			AutoResetEvent Estado = new AutoResetEvent(false);
    			ThreadPool.QueueUserWorkItem(new WaitCallback(Trabalhadora), Estado);
    			Estado.WaitOne();
    it gives me great control but i soon discovered that the main thread freezes...do i have to create a thread for the thread who controls or threads? or am i missing something?
    \m/\m/

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    What does the worker function do with the event object you pass to it? Are you sure you want to wait for the object to be signaled? If you have to wait for the signal then you shouldn't use a thread.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    well i have a download class...that must run on a separate thread..it has an event that fires when the download is complete...when in my project i try to create a object at run-time from that event i cant because it doesnt me allow to make that on a separate thread..so now i am making that the download class has a function that calls the thread and then waits for the thread to stop and sends the event itself so it wont raise that problem when i try to use it in my project..or u have any better idea?
    \m/\m/

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    How does the first thread react when the download is done? Maybe the other could send a user-defined message instead of raising the event. But I admit I have no idea how to send messages in .Net. Must have something to do with the struct System.Windows.Forms.Message.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    oh...i dont know how messages work and even dont even know what a message really is
    \m/\m/

  6. #6

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemwindowsformsmessageclasstopic.htm
    i dont get what i can do for me...it lets me the child function(download) thread to talk to the main (download class) thread?
    \m/\m/

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Basically my idea is that the download thread should somehow set a flag for the main thread which the main thread checks from time to time. A message posted to the message loop would be ideal, but I'm not sure if you can send user-defined messages in .Net.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Control.BeginInvoke looks interesting...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Like this:
    Code:
    class MyForm : Form
    {
      // ...
      delegate void NoArgDelegate();
    
      void DownloadFinished() {
        // whatever
      }
    
      // ...
      void Trabalhadora(object state) {
        // download here
        // and when finished:
        BeginInvoke(new NoArgDelegate(DownloadFinished));
      }
    }
    I might have syntax errors there as I don't really program in C#.

    This code should call DownloadFinished in the thread that created the form some time after the download is finished. Not immediatly, only when the thread has some time.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    the problem would still be the same...i cant call directly a function from the function who's downloading because then all functions that are made in future can't create form objects because this is a separate thread...
    \m/\m/

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    That's what I'm saying. BeginInvoke calls the function with the thread that created the form.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmm but to use beginvoke i have to inherit from Control...that sucks! and i dont want to inherit my control of anything...one of the reasons is that my control might already inherit from another control
    \m/\m/

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You only need access to your main form and call BeginInvoke on that. That can't be too hard, right?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    ah..i dont do that in the download class but in the class (form) that will use it?
    \m/\m/

  15. #15

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    i had an idea now that i saw it on msdn...my download function could have a reference to my form and then it'd use the form's begininvoke
    \m/\m/

  16. #16

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    delegate void NoArgDelegate();
    how do i use this? i know how to use delegates but i am getting an "<function name> is referred without parantheses

    on the form...
    Code:
    private void lol() {
    }
    
    form load(blablabla) {
            this.BeginInvoke(Form1.NoArgDelegate(lol));
    }
    how should i write then? i tryed puting 'lol()' instead 'lol' but it gives me error once again ('WindowsApplication34.Form1.NoArgDelegate' denotes a 'class' which is not valid in the given context)
    \m/\m/

  17. #17

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    wait a minute...System.Delegate isnt the same than delegate is it?
    when i get home ill see that more carefully
    \m/\m/

  18. #18
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    new NoArgDelegate(lol)

    The delegate keyword declares a delegate, which is a special kind of class that always derives from System.MultiCastDelegate, which in turn derives from System.Delegate.

    i had an idea now that i saw it on msdn...my download function could have a reference to my form and then it'd use the form's begininvoke
    That's what I meant.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  19. #19

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmm k ill try it
    \m/\m/

  20. #20

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    tks its working great
    \m/\m/

  21. #21

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmm have a problem now...first i tried to assign to a var in the download class a var that would be assigned in the constructor pointing to the form who executed it:

    constructor(this);

    but soon i realised i couldn't do this because this way i was sending a new var to the class..so i tried with:
    constructor(ref this);

    but i can't because i get that this is read-only...what do i do then?
    \m/\m/

  22. #22
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I don't understand your question.
    But it sounds to me like you should start a new thread for it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  23. #23

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmm i think u didnt get it lol

    i want that when my download thread download finishes instead of directly fire the event to use the BeginInvoke with a reference made by the constructor to the main form and only then i'd fire the event.

    the problem is that i can't make refereces to the 'this' word...how can i achieve the same?
    \m/\m/

  24. #24
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327

    no problem

    You can make a reference to this without a problem.

    What type are you using when you pass the object in? Why don't you try storing the object reference as an object vs. a particular form type.

    p.s. you don't need to pass it by reference...
    -scott
    he he he

  25. #25
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Code:
    class Download
    {
      private System.Windows.Forms.Control m_control;
      public Download(System.Windows.Forms.Control c) {
        m_control = c;
      }
    }
    
    class MyForm : System.Windows.Forms.Form
    {
      public StartDownload() {
        Download dl = new Download(this);
      }
    }
    Can't imagine this not working...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  26. #26
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Hey PT, this might be of some help. I was browsing CSharpToday.com and found an article on Thread Pooling. Its from the subscribers section, so I copied it to a word doc for you. Here is the link.

    Good luck.
    Dont gain the world and lose your soul

  27. #27

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    ohhh much tks

    edit: and yes bee ur right lol but i have some error in my code what is driving me crazy is the message error i'm getting. i posted about that in vb.net's forum cuz i though that more ppl passed by there so i'd get a answer quickly but i didnt get a solution yet u might if u want to drop a view by the thread: http://www.vbforums.com/showthread.p...01#post1325401
    Last edited by PT Exorcist; Jan 10th, 2003 at 03:14 PM.
    \m/\m/

  28. #28

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    ive been puting try's and catchs all over the code and still cant find the damn bug..i really need that damn thing that points to the error line...
    \m/\m/

  29. #29
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Sorry, don't have an answer for that.

    Have you tried inserting a breakpoint and stepping over the code step-by-step?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  30. #30

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    yes thats what i am doin now...puted break points in ALL FUNCTIONS that are used and it still gives me error "outside" the break points...i dont get it..without the damn message saying where is the error's line this sucks and i am not willin to have to re-install the damn visual studio......
    \m/\m/

  31. #31

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    well i hate doin this but...can someone download and try to help me catchin the bug? it has to be something related to delegates..anyway i would be much appreciated...
    Attached Files Attached Files
    \m/\m/

  32. #32
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    That was a damn tricky one, but I solved it.

    The problem was that the delegate expected one argument but you gave him none.

    Here are some changes to HTTPDownloader.EventHandler that solve the error:
    Code:
    Form parent = ((Form)this._parent);
    Object[] args = new Object[1];
    args[0] = this;
    switch (e)
    {
    case callEvents.ConnectionStateChanged:
    	System.Diagnostics.Debug.WriteLine("Calling...");
    	parent.BeginInvoke(_eventStateChanged, args);
    	break;
    case callEvents.FileSizeKnown:
    	System.Diagnostics.Debug.WriteLine("Calling...");
    	parent.BeginInvoke(_eventSizeKnown, args);
    	break;
    case callEvents.ProgressChanged:
    	System.Diagnostics.Debug.WriteLine("Calling...");
    	parent.BeginInvoke(_eventProgressChanged, args);
    	break;
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  33. #33

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    oh im too tired to see now but im glad u've done it...tomorrow ill examine it carefully...much tks



    edit: yea it really works.....u rule D
    Last edited by PT Exorcist; Jan 11th, 2003 at 10:41 PM.
    \m/\m/

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