Thread: Threading-passing parameters

  1. #1
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110

    Threading-passing parameters

    Hey,

    this is the first time ever i touch something that has to do with multithreading ... I find it something very confusing and after searching this board i came across this thread, however no solution is provided.

    What I want to do is actually plain simple but I dont have a clue how to go about this threading thing. I have a buffer where i want to get some data from each 10 seconds or whatsoever, but at the same time all the rest of my program should keep running since its supposed to perform multiple tasks at the same time...

    If anyone has a good online reference about threading using C# I´d be gratefull, also I´d like to know how to do what the OP in the link above asked ( passing parameters when having new ThreadStart(this.method(param1,param2) ).

    Thanks in advance,

    Ganglylamb.

    ::edit::
    In case it matters:
    OS: win xp sp2
    Compiler: Visual Studio .NET 2003

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Although you may not pass parameters, you may pass a function that is a member of a class. As such, this function may access all private data of said class. Create a class that has members and properties for your parameters. Set those first, then call ThreadStart. When your threading function is called, it can work with the class instance's properties/private members.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    I'm not sure if it's avaliable in 2003, but in 2005 (well, .NET 2.0) theres a ParameterizedThreadStart rather than the usual ThreadStart.

    As far as reading buffers go, what I usually do is have a timer on the worker thread set to 10 seconds. When it needs data, it fires an event or calls a delegate. This delegate was set up by the main thread (who created this worker thread) and gives it data when it asks for it.

    If the worker thread ever needs to update the user interface, start the method with something like:
    Code:
    void OnWorkerNeedsBuffer(object sender, BufferEventArgs e)
    {
        if (this.InvokeRequired)
        {
            // If it gets to here, we're not in the UI thread
            // I DON'T REMEMBER THE SYNTAX:
            this.Invoke(OnWorkerNeedsBuffer, new object[] {sender, e}); 
            return;
        }
     
        // If it gets to here, we must be in the UI thread, so it's safe to access the controls
        e.Buffer = textBox1.Text;
    }
    I hope that's some help, lemme know if you need clarification.

  4. #4
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    @nvoigt: I guess I could do it that way actually, although it seems quite a hack for such a rather easy thing.

    @stovellp: I just tried the thing with implenting the timer ( since i know it takes less coding then nvoigts solutions - yeah im lazy ). And it works fine, just brought a timer onto my form, set the interval manually and set it to enabled, filled in the only event handler it has and its up and working now.

    It might be I come back to ask more info on nvoigt´s approach since I´ve got it done but in fact I wouldnt really know how to code it myself ( since the visual studio thingy did it for me this time with this timer ).

    Thanks to both of you,

    Ganglylamb.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 01:08 PM
  2. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  3. Question about passing & receiving parameters
    By TCB in forum C Programming
    Replies: 9
    Last Post: 04-11-2006, 06:08 AM
  4. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 03:13 AM
  5. Passing Parameters
    By fry in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2002, 03:06 AM