C# ASP.NET应用程序中任务的怪异行为

C# ASP.NET应用程序中任务的怪异行为,c#,asp.net,asynchronous,task,C#,Asp.net,Asynchronous,Task,我正在尝试异步连接和接收来自所有分支的Mysql服务器的少量数据,而无需等待一个分支完成 我有自己的事件驱动的.NET MySQL连接器包装库,它可以在没有异步的情况下正常工作 看起来我在处理多个任务时遗漏了一些东西,但我不知道为什么 public void GetALL() { TaskList = new Task[RemSQL.Count]; Response.Write("<h1>starting..........</h1&g

我正在尝试异步连接和接收来自所有分支的Mysql服务器的少量数据,而无需等待一个分支完成

我有自己的事件驱动的.NET MySQL连接器包装库,它可以在没有异步的情况下正常工作

看起来我在处理多个任务时遗漏了一些东西,但我不知道为什么

public void GetALL()
    {
        TaskList = new Task[RemSQL.Count];

        Response.Write("<h1>starting..........</h1>");

        Task t;
        BranchInfo b;
        Wrapper w;

        for (int tx = 0; tx < RemSQL.Count; tx++)
        {
            int txx = tx; //strongly suggested on MSDN while using tasks/threads in loops
            b = RemSQL[txx];
            w = b.Wrapper;
            Response.Write("<h2>TASK #" + txx.ToString() + " branch.id #" + w.id + " starts...</h2>");

            w.Connecting += Wrapper_Connecting;            
            w.Connected += Wrapper_Connected;
            w.ConnectionError += Wrapper_ConnectionError;

            //w.Connect() //disabling multitasking works just fine

            t = new Task(() =>
            {
                w.Connect();
            });

            TaskList[txx] = t;
            t.Start();

        }

        Task.WaitAll(TaskList);

        Response.Write("<h1>Tasks completed</h1>");

        foreach(BranchInfo bb in RemSQL)
        {
            bb.Wrapper.Dispose();
        }

        Response.Flush();
        Response.End();

    }

    private void Wrapper_Connected(object sender)
    {
        Wrapper w = (Wrapper)sender;
        WriteScript("Connected('" + w.id + "');");
    }

    private void Wrapper_Connecting(object sender)
    {
        Wrapper w = (Wrapper)sender;
        WriteScript("Connecting('" + w.id + "');");
    }

    private void Wrapper_ConnectionError(object sender, Exception ex)
    {
        Wrapper w = (Wrapper)sender;
        WriteScript("ConnectionFailed('" + w.id + "', '" + ex.Message + "');");
    }

private void WriteScript(string scr)
{
    Response.Write("<script>" + scr + "</script>\n");
    Response.Flush();
}
以下是输出:

<h1>starting..........</h1><h2>TASK #0 branch.id #2 starts...</h2>
<h2>TASK #1 branch.id #3 starts...</h2>
<h2>TASK #2 branch.id #4 starts...</h2>
<h2>TASK #3 branch.id #5 starts...</h2>
<h2>TASK #4 branch.id #6 starts...</h2>
<h2>TASK #5 branch.id #7 starts...</h2>
<h2>TASK #6 branch.id #8 starts...</h2>
<h2>TASK #7 branch.id #9 starts...</h2>
<h2>TASK #8 branch.id #10 starts...</h2>
<h2>TASK #9 branch.id #11 starts...</h2>
<h2>TASK #10 branch.id #13 starts...</h2>
<h2>TASK #11 branch.id #14 starts...</h2>
<h2>TASK #12 branch.id #15 starts...</h2>
<h2>TASK #13 branch.id #16 starts...</h2>
<h2>TASK #14 branch.id #17 starts...</h2>
<h2>TASK #15 branch.id #19 starts...</h2>
<h2>TASK #16 branch.id #20 starts...</h2>
<script>Connecting('20');</script>
<script>Connecting('20');</script>
<script>Connecting('20');</script>
<script>Connecting('20');</script>
<script>Connecting('20');</script>
<script>Connecting('20');</script>
<script>Connecting('20');</script>
<script>Connecting('20');</script>
<script>Connecting('20');</script>
<script>Connecting('20');</script>
<script>Connecting('20');</script>
<script>Connecting('20');</script>
<script>Connecting('20');</script>
<script>Connecting('20');</script>
<script>Connecting('20');</script>
<script>Connecting('20');</script>
<script>Connecting('20');</script>
<script>ConnectionFailed('20', 'There is already an open DataReader associated with this Connection which must be closed first.');</script>
<script>ConnectionFailed('20', 'There is already an open DataReader associated with this Connection which must be closed first.');</script>
<script>ConnectionFailed('20', 'There is already an open DataReader associated with this Connection which must be closed first.');</script>
<script>ConnectionFailed('20', 'There is already an open DataReader associated with this Connection which must be closed first.');</script>
<script>ConnectionFailed('20', 'There is already an open DataReader associated with this Connection which must be closed first.');</script>
<script>ConnectionFailed('20', 'There is already an open DataReader associated with this Connection which must be closed first.');</script>
<script>ConnectionFailed('20', 'There is already an open DataReader associated with this Connection which must be closed first.');</script>
<script>ConnectionFailed('20', 'There is already an open DataReader associated with this Connection which must be closed first.');</script>
<script>ConnectionFailed('20', 'There is already an open DataReader associated with this Connection which must be closed first.');</script>
<script>Connected('20');</script>
<script>ConnectionFailed('20', 'There is already an open DataReader associated with this Connection which must be closed first.');</script>
<script>ConnectionFailed('20', 'There is already an open DataReader associated with this Connection which must be closed first.');</script>
<script>ConnectionFailed('20', 'There is already an open DataReader associated with this Connection which must be closed first.');</script>
<script>ConnectionFailed('20', 'There is already an open DataReader associated with this Connection which must be closed first.');</script>
<script>ConnectionFailed('20', 'There is already an open DataReader associated with this Connection which must be closed first.');</script>
<script>ConnectionFailed('20', 'There is already an open DataReader associated with this Connection which must be closed first.');</script>
<script>Connected('20');</script>
<h1>Tasks completed</h1>
正如您所看到的,我猜包装器w似乎总是获取循环中最后一个调用的引用。但是当我注释掉任务部分并在循环中直接使用w.Connect时,一切都解决了


有什么想法吗

您的问题是,您正在关闭局部变量w,稍后将对其进行更改,因此,当您到达任务时,它已经包含运行时的其他值

解决方案很简单:在循环中声明t、b和w。事实上,如果我是你,我会把它改写成简单的选择:


谢谢。真不敢相信我在使用计数器“txx”时错过了什么,同样的事情。
Response.Write("<h1>starting..........</h1>");
Task.WaitAll(
  RemSQL.Select(b => b.Wrapper).Select(w => TaskFactory.Run(() => {
        Response.Write("<h2>TASK #" + txx.ToString() + " branch.id #" + w.id + " starts...</h2>");

        w.Connecting += Wrapper_Connecting;            
        w.Connected += Wrapper_Connected;
        w.ConnectionError += Wrapper_ConnectionError;

        w.Connect();
  })).ToArray());