How to share data between different threads In C# using AOP?

前端 未结 4 1489
名媛妹妹
名媛妹妹 2020-11-28 08:15

How to share data between different threads In C# without using the static variables? Can we create a such machanism using attribute?

Will Aspect oriented programmin

4条回答
  •  心在旅途
    2020-11-28 08:35

    When you start a thread you are executing a method of some chosen class. All attributes of that class are visible.

      Worker myWorker = new Worker( /* arguments */ );
    
      Thread myThread = new Thread(new ThreadStart(myWorker.doWork));
    
      myThread.Start();
    

    Your thread is now in the doWork() method and can see any atrributes of myWorker, which may themselves be other objects. Now you just need to be careful to deal with the cases of having several threads all hitting those attributes at the same time.

提交回复
热议问题