如何获取线程中函数的变量值?不用静态变量,在下例中,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的值,用于其它应用
}
}
}
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的值,用于其它应用
}
}
}
解决方案 »
- Textbox边框颜色设置不了。怎么办?
- 当文本文件到达20000行删除10000行
- DropDownList1_SelectedIndexChanged 不执行事件
- 请问 winform 里的通用数据库操作函数应该怎么写
- 怎么控制flash播放?
- 如何做一个类似于sqlserver创建视图时那种连接字段的连接线,线上还能写条件?
- winform控件DateTimePicker ValueChanged事件引起的异常现象求助
- 来人讨论下,关于多线程内频繁使用匿名方法和ParameterizedThreadStart委托的损耗问题
- mongodb 如何取随机数据? .Net 求高手解答在线等
- 怎样在datagrid里面放置一个checkbox模板列,然后再用一个checkbox2来控制checkbox模板列
- winform自定义控件问题
- 如何停止掉当前正在执行的进程
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();
}
}
}
}
Alpha oAlpha1=null;
Alpha oAlpha2=null;
Alpha oAlpha3=null;
....
long ter1 = oAlpha1.GetAccount();
long ter2 = oAlpha2.GetAccount();
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实例,然后启动一个对应线程,再根据线程来获取变量值,这样是不是可以,因为公共类可能要在多个线程中使用