Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#多线程与异步/等待相结合_C#_Multithreading_Asynchronous - Fatal编程技术网

C#多线程与异步/等待相结合

C#多线程与异步/等待相结合,c#,multithreading,asynchronous,C#,Multithreading,Asynchronous,这是与C#中的多线程相关的另一个问题 然而,当多线程与异步/等待相结合时,似乎很有趣 我有一个生成线程的方法,可以异步调用对象的方法 但是,不同类的对象不共享上下文 例如,考虑代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ThreadDa

这是与C#中的多线程相关的另一个问题

然而,当多线程与异步/等待相结合时,似乎很有趣

我有一个生成线程的方法,可以异步调用对象的方法

但是,不同类的对象不共享上下文

例如,考虑代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ThreadDataSharing
{
    class Data
    {
        public static ThreadLocal<string> Name = new ThreadLocal<string>();
    }

    class Locker { public static object lobj = new object { };}
    class Program2
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 3; i++)
            {
                var pObj = new Program();
                new Thread(new ThreadStart(pObj.MyThreadMethod)).Start();
            }
        }
    }
    class Program
    {

        ///my async method
        ///
        public async Task MyAsyncMethod()
        {
            lock (Locker.lobj)
            {
                Console.WriteLine("From async method");
                Console.WriteLine("thread: " + Thread.CurrentThread.ManagedThreadId);
                Console.WriteLine("id: " + Data.Name.Value);
            }

            await MyAsyncMethod2();

            await new AsyncClass().AsyncMethod();
        }

        public async Task MyAsyncMethod2()
        {
            lock (Locker.lobj)
            {
                Console.WriteLine("From async 2 method");
                Console.WriteLine("thread: " + Thread.CurrentThread.ManagedThreadId);
                Console.WriteLine("id: " + Data.Name.Value);
            }

            await Task.Run(() =>
            {
                lock (Locker.lobj)
                {
                    Console.WriteLine("From task run method");
                    Console.WriteLine("thread: " + Thread.CurrentThread.ManagedThreadId);
                    Console.WriteLine("id: " + Data.Name.Value);
                }
            });
        }

        public void MyThreadMethod()
        {
            lock (Locker.lobj)
            {
                Console.WriteLine("From thread method");
                Console.WriteLine("thread: " + Thread.CurrentThread.ManagedThreadId);

                var id = Guid.NewGuid();
                Data.Name.Value = id.ToString();
                Console.WriteLine("id: " + Data.Name.Value);
            }

            MyAsyncMethod().Wait();
        }
    }


    public class AsyncClass
    {
        public async Task AsyncMethod()
        {
            lock (Locker.lobj)
            {
                Console.WriteLine("From class async method");
                Console.WriteLine("thread: " + Thread.CurrentThread.ManagedThreadId);
                Console.WriteLine("id: " + Data.Name.Value);
            }

            var newId = Guid.NewGuid();
            Data.Name.Value = newId.ToString();

            await AsyncMethod2();
        }

        public async Task AsyncMethod2()
        {
            lock (Locker.lobj)
            {
                Console.WriteLine("From class async 2 method");
                Console.WriteLine("thread: " + Thread.CurrentThread.ManagedThreadId);
                Console.WriteLine("id: " + Data.Name.Value);
            }

            await Task.Run(() => { });
        }
    }

}

问题是,为什么它不与第二个类(
AsyncClass
)的实例共享
ThreadLocal
对象?

在带有wait/async的控制台应用程序中,没有共享的同步上下文,这可能是它不共享的原因

不同类的对象不共享上下文

实际上,您看到的行为是(线程本地)上下文在线程之间不共享


我有一篇博客文章可能会澄清这种行为。基本概念是
async
方法不会在同一线程上恢复;相反,它们在捕获的上下文上恢复(默认情况下)。在本例中,线程池上下文用于恢复
async
方法,这意味着这些延续只是调度到线程池中,并且可以由任何线程池线程运行。

你是说任何异步方法都应该接收它需要处理的所有必要数据吗?@BabuJames:传递和返回数据最干净,而不是依赖隐式上下文,是的。
From thread method
thread: 3
id: 3a7ef8e3-ebe1-49e1-94bb-798358ac1567
From thread method
thread: 4
id: 341d3371-f905-4e1c-aac3-47bda16c8d88
From thread method
thread: 5
id: b7e79901-81b7-430f-b158-59f091e43a0c
From async method
thread: 3
id: 3a7ef8e3-ebe1-49e1-94bb-798358ac1567
From async method
thread: 5
id: b7e79901-81b7-430f-b158-59f091e43a0c
From async method
thread: 4
id: 341d3371-f905-4e1c-aac3-47bda16c8d88
From async 2 method
thread: 3
id: 3a7ef8e3-ebe1-49e1-94bb-798358ac1567
From async 2 method
thread: 4
id: 341d3371-f905-4e1c-aac3-47bda16c8d88
From async 2 method
thread: 5
id: b7e79901-81b7-430f-b158-59f091e43a0c
From task run method
thread: 6
id:
From task run method
thread: 8
id:
From task run method
thread: 7
id:
From class async method
thread: 7
id:
From class async method
thread: 6
id:
From class async method
thread: 8
id:
From class async 2 method
thread: 8
id: f52ed654-0e55-4906-bfc1-65c6b25a7785
From class async 2 method
thread: 7
id: 1e53e03b-a3a0-4296-8622-7716b45d1462
From class async 2 method
thread: 6
id: 1adca81d-b11a-4860-b37d-a017afe877b8