如何获取线程中函数的变量值?不用静态变量,在下例中,Alpha是一个测试类,在启动线程后,希望在外部获取其中的变量iAccount的值用于其它应用。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace testThread
{
    public class Alpha
    {
        public long iAccount = 0;
        public void Beta()
        {
            while (true)
            {
                iAccount++;
                if (iAccount == 1000) iAccount = 0;
            }
        }
    };
    
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            Alpha oAlpha = new Alpha();
            Thread oThread = new Thread(new ThreadStart(oAlpha.Beta));
            oThread.Name = "mythread";
            oThread.Start();
        }        private void button2_Click(object sender, EventArgs e)
        {
            //得到iAccount的值,用于其它应用
        }
    }
}

解决方案 »

  1.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;namespace WindowsApplication13
    {
        public class Alpha
        {
            public long iAccount = 0;
            public void Beta()
            {
                while (true)
                {
                    iAccount++;
                    if (iAccount == 1000) iAccount = 0;
                }
            }
            public long GetAccount()
            {
                return iAccount;
            }
        };
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            Alpha oAlpha=null;
            private void Form1_Load(object sender, EventArgs e)
            {
            }
            private void button1_Click(object sender, EventArgs e)
            {
                oAlpha = new Alpha();
                Thread oThread = new Thread(new ThreadStart(oAlpha.Beta));
                oThread.Name = "mythread";
                oThread.Start();
            }
            private void button2_Click(object sender, EventArgs e)
            {
                if(oAlpha!=null)
                {
                    long ter = oAlpha.GetAccount();
                }
            } 
        }
    }
      

  2.   

    谢谢三楼指点,这样是可以获取变量值,但不知道对于以下情况是否可行,比如同时可能启动多个线程调用oAlpha.Beta,怎么才能获取多个线程对应的变量值?,谢谢!
      

  3.   

    声明多个oAlpha = new Alpha();就可以了。
    Alpha oAlpha1=null;
    Alpha oAlpha2=null;
    Alpha oAlpha3=null;
    ....
    long ter1 = oAlpha1.GetAccount();
    long ter2 = oAlpha2.GetAccount();
      

  4.   

    谢谢,我的想法是,在局部定义实例类,如
           private void button1_Click(object sender, EventArgs e)
            {
                Alpha oAlpha = new Alpha();
                Thread oThread = new Thread(new ThreadStart(oAlpha.Beta));
                oThread.Name = "mythread";
                oThread.Start();
            }
          这样的话,每次按下按钮时,创建一个alpha实例,然后启动一个对应线程,再根据线程来获取变量值,这样是不是可以,因为公共类可能要在多个线程中使用
      

  5.   

    看来LZ是钻牛角尖了,Alpha oAlpha = new Alpha(); 声名为类成员变量的的目的就是保存线程函数的地址的,如果你不想声明多个,可以用ArrayList或数组等保存的Alpha实例的,而不是说通过线程去找对应的函数在找到它的类,然后再找这个类的成员变量,这不是没事找事嘛而且根本无法做到,你运行线程的时候在内存里只保留了这个线程的地址的,没办法找到它的成员变量。