Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. MultyThreading in C#: Communication between threads

MultyThreading in C#: Communication between threads

Scheduled Pinned Locked Moved C#
helpquestioncsharp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jasper4C
    wrote on last edited by
    #1

    Hi! May be yo can help me with this problem: I need to create a new instance of class (DLL or Windows Class file) that part of the project in a separate thread. Then I need to communicate with that class by passing some varibales or calling function/methods in this class. So question is: How I can do that (with multythreading) ? I appreciate if you write me some code too - thanks "I have not failed. I've just found 10,000 ways that won't work." - Thomas Alva Edison (1847-1931)

    M 1 Reply Last reply
    0
    • J Jasper4C

      Hi! May be yo can help me with this problem: I need to create a new instance of class (DLL or Windows Class file) that part of the project in a separate thread. Then I need to communicate with that class by passing some varibales or calling function/methods in this class. So question is: How I can do that (with multythreading) ? I appreciate if you write me some code too - thanks "I have not failed. I've just found 10,000 ways that won't work." - Thomas Alva Edison (1847-1931)

      M Offline
      M Offline
      Marc Clifton
      wrote on last edited by
      #2

      Jasper4C# wrote: Then I need to communicate with that class by passing some varibales or calling function/methods in this class. So question is: How I can do that (with multythreading) ? That's bit vague, so here goes: 1. look at the "lock" keyword in C# for preventing multiple threads from writing to a variable at the same time. 2. Look at the Mutex class for synchronizing between threads and blocking one thread while another thread is calling a method you want protected from simultaneous execution. 3. Look at AutoResetEvent and ManualResetEvent for signalling between threads. Hope that helps! Marc Latest AAL Article My blog Join my forum!

      J 1 Reply Last reply
      0
      • M Marc Clifton

        Jasper4C# wrote: Then I need to communicate with that class by passing some varibales or calling function/methods in this class. So question is: How I can do that (with multythreading) ? That's bit vague, so here goes: 1. look at the "lock" keyword in C# for preventing multiple threads from writing to a variable at the same time. 2. Look at the Mutex class for synchronizing between threads and blocking one thread while another thread is calling a method you want protected from simultaneous execution. 3. Look at AutoResetEvent and ManualResetEvent for signalling between threads. Hope that helps! Marc Latest AAL Article My blog Join my forum!

        J Offline
        J Offline
        Jasper4C
        wrote on last edited by
        #3

        10x for information but what I mean was - for example: -------------------------------------------------------- using .... ; //doesn't matter namespace DllClass { public class ClassA { private string m_message; public ClassA() { //Empty constr (??) } public void ThreadEntrancePoint() { //This method will be called when creating //a new thread //TODO: Should I put here infinity loop ??? } //This method will chande variable in this class public string SetMessage { set { if (value != null) m_message = value; } } } } // End of DLL file --------------------------------------------------------- using ....; //All the other using System.Threading; using DllClass; //need to create class namespace MyFormNameSpace { public class MyForm: Windows.Form { public int Main (...) { //Doesn't matter for now } //This function will be called when user want's //to create a new thread public void btn_CreateThread(...) { Thread t = new Thread (new ThreadStart DllClass.ClassA.ThreadEntrancePoint); t.Start(); } //This function will be called when user want //to send a new messge to the class that inside //thread public void SendNewMessageToThread (string msg) { //TODO: What code should I put here to send //message to ClassA.SetMessage ?? } }//class close }//namespace close --------------------------------------------------------- You see - in this example I want to completly run ClassA in new thread and also pass some variables to this class and may be receive some inforamtion from it. How I can do that ? "I have not failed. I've just found 10,000 ways that won't work." - Thomas Alva Edison (1847-1931)

        M 1 Reply Last reply
        0
        • J Jasper4C

          10x for information but what I mean was - for example: -------------------------------------------------------- using .... ; //doesn't matter namespace DllClass { public class ClassA { private string m_message; public ClassA() { //Empty constr (??) } public void ThreadEntrancePoint() { //This method will be called when creating //a new thread //TODO: Should I put here infinity loop ??? } //This method will chande variable in this class public string SetMessage { set { if (value != null) m_message = value; } } } } // End of DLL file --------------------------------------------------------- using ....; //All the other using System.Threading; using DllClass; //need to create class namespace MyFormNameSpace { public class MyForm: Windows.Form { public int Main (...) { //Doesn't matter for now } //This function will be called when user want's //to create a new thread public void btn_CreateThread(...) { Thread t = new Thread (new ThreadStart DllClass.ClassA.ThreadEntrancePoint); t.Start(); } //This function will be called when user want //to send a new messge to the class that inside //thread public void SendNewMessageToThread (string msg) { //TODO: What code should I put here to send //message to ClassA.SetMessage ?? } }//class close }//namespace close --------------------------------------------------------- You see - in this example I want to completly run ClassA in new thread and also pass some variables to this class and may be receive some inforamtion from it. How I can do that ? "I have not failed. I've just found 10,000 ways that won't work." - Thomas Alva Edison (1847-1931)

          M Offline
          M Offline
          Marc Clifton
          wrote on last edited by
          #4

          Ok, first off, you can't call ThreadStart() the way you described because you need an instance of the class or the method you're calling has to be static. If it's static, then anything it manipulates has to be static as well, and that means anything outside of the thread can access it as well, as long as it's static. If you made all the data and methods in ClassA static, then you could simply call ClassA.SetMessage from any thread--main application thread included. If it's not static, then you need an instance of the object to pass along to the delegate. Since you have the instance, you can also call the SetMessage method from the application thread. For example:

          ClassA myClassA=new ClassA();
          ThreadStart threadDelegate=new ThreadStart(myClassA.ThreadEntrancePoint);
          Thread newThread=new Thread(threadDelegate);
          newThread.Start();

          // in my main application:
          myClassA.SetMessage="foobar";
          ...

          I hope that helps. Note that you probably would want a "lock" statement to bracket the setter. Marc Latest AAL Article My blog Join my forum!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups