Accessing Data between threads

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Slaughter

    #1

    Accessing Data between threads

    What concepts do I have to know to access data between two threads safely?

    Right now I'm using a delegate method and Invoke to call a function in the
    other thread that handles the data. This method seems a little bulky and I
    read that it can have problems as it stalls.

    for example, heres how I update the text to a textbox and the a status bar
    label

    this.Invoke(new TextDelegate(Up dateTextBox2), text);

    this.Invoke(new TextDelegate(Up dateStatus), new object[] { "Finished" });



    I call this function which is inside the forms class which is attached to an
    event in another class which is controled by a thread.



    i.e.



    I have two classes, One is a the main gui form and the other is a
    "computatio n" class that does things that take a long time.



    In the computation class I have an event that the computation class calls
    when it is finished with its computation to let the form know it can then
    display the results. I then attach a function to this event that is in the
    forms class so I can use the data in the form directly.



    The methodology I use seems kinda bulky and probably has issues.



    The project is located at





    if anyone is interested.

    -----



    Also, something that I'm not sure about is when I attach a function to a
    thread, when I call thread.Start(); thread.Stop(); Thread.Start(); Each time
    on Thread.Start() does it re enter the function? (I'm using abort which I
    read somewhere was bad)



    Basically what I want call the function to compute and when it finished
    display the result but the computation must be done in the background. I
    also want to be able to "abort" the computation and restart it with
    different parameters because the user might want to change something.



    Anyways, any help would be appreciated. I guess I have to get a good book on
    threading as I can't seem to find any coherent information the net.



    Thanks,

    Jon




  • Marc Gravell

    #2
    Re: Accessing Data between threads

    Yes, you broadly need the Invoke approach (although .BeginInvoke can
    help avoid deadlocks in some specific situations). In 2.0 you can make
    life easier by using inline delegates, though:

    string text = "something" ;
    this.BeginInvok e((MethodInvoke r) delegate {
    txtSomething.Te xt = text;
    // other code
    });

    For abort, *don't do this*. You need to use a safer approach. If using
    BackgroundWorke r, you can use the in-built cancel features;
    alternatively a thread-sync'd bool would do. For a good write-up on
    threading, Jon Skeet has a very good article at
    http://www.yoda.arachsys.com/csharp/threads/ (note: several pages
    deep).

    Marc

    Comment

    • Jon Slaughter

      #3
      Re: Accessing Data between threads


      "Marc Gravell" <marc.gravell@g mail.comwrote in message
      news:1164060832 .066825.257350@ k70g2000cwa.goo glegroups.com.. .
      Yes, you broadly need the Invoke approach (although .BeginInvoke can
      help avoid deadlocks in some specific situations). In 2.0 you can make
      life easier by using inline delegates, though:
      >
      string text = "something" ;
      this.BeginInvok e((MethodInvoke r) delegate {
      txtSomething.Te xt = text;
      // other code
      });
      >
      For abort, *don't do this*. You need to use a safer approach. If using
      BackgroundWorke r, you can use the in-built cancel features;
      alternatively a thread-sync'd bool would do. For a good write-up on
      threading, Jon Skeet has a very good article at
      http://www.yoda.arachsys.com/csharp/threads/ (note: several pages
      deep).
      >
      Thanks. I'll read this and see what happens.
      Jon


      Comment

      Working...