大家好,假设有一个数组buffer[],通过方法func_in()向这个数组写入数据,然后通过func_out()读取数组中的数据,我现在实现的就是通过func_in()一次性的把所有数据写入数组,然后才调用func_out()读取数组中的数据,我现在想让func_in()不断地执行,在func_in()执行2秒之后,func_out()开始不断地从数组中取数据,假设func_in()写入的速度和func_out()读取的速度一致,应该怎么实现,望大家给一个思路,谢谢!
解决方案 »
- C#获得IE addon
- c# activex 调用出现对象不支持此属性或方法
- MouseHove事件在控件内部不能触发
- tableTableAdapter.Fill(this.database1DataSet.table)
- 麻烦哪位兄弟有vs2005的帮我把微软的Duwamish7升级一下,我以前升级成了,现在用vs2008,升级不了,郁闷
- C# WPF 中类似于toolstrip的控件
- 怎么样把treeview转换成XML?
- 帮帮我 解决一个对于你们简单的问题急急急!!
- 如何结束一个线程? 高手们,帮帮忙
- 如何将C#开发的组件编译为VB6可用的COM(编译为非托管代码)?
- 如何在类库反射调用的程序?
- 版主看见帮下忙 拜托给100分
while (true)
{
// func_in()
// 如果不是多线程 没必要 停2秒
// func_out()
}
Public Byte[] SaveSomething; //原始数组
int i = 0 ; //记录用来跟2取余//写入,简单的直接New一个
private void func_in()
{
SaveSomething = new Byte[3]{0x1,0x2,0x3};
}//读取
private void func_out()
{
foreach(byte b in SaveSomething)
{
//read b
}
}private void Main()
{
Timer a = new Timer();
a.Invatal = 2000;
a.Tick();
}proviate void a_Tick()
{
i++;
func_in();
if(i%2 == 0 & i!=0)func_Out(); //不等于且除2取余=0时激活func_Out()
}
System.Threading.Thread.Sleep
public void LostTime()
{
long beginTime = DateTime.Now.Ticks;
while (true)
{
long endTime = DateTime.Now.Ticks;
if ((endTime - beginTime) > 1e8) break;
}
return;
}在需要延时的地方加上类似函数就可以
延迟Sleep()
{
bool IsRunning = false;
buffer[] BufferProp = new buffer[Capacity];
private void func_in()
{
BufferProp = InputData();
Console.WriteLine("Input action processed.");
}
private void func_out()
{
OutputDevice = BufferProp;
Console.WriteLine("Output action processed.");
}
void Action(object source, ElapsedEventArgs e)
{
func_out();
func_in();
}
public void Start()
{
IsRunning = true;
if (IsRunning)
{
Timer timer = new Timer(2 * 1000);
timer.Elapsed +=new ElapsedEventHandler(Action);
timer.Start();
}
}
}
注释部分注释掉可以直接测试。 class Sample
{
bool IsRunning = false;
bool IsInputting = false;
bool IsOutputting = false;
//buffer[] BufferProp = new buffer[Capacity];
private void func_in()
{
//BufferProp = InputData();
Console.WriteLine("Input action processed.");
}
private void func_out()
{
//OutputDevice = BufferProp;
Console.WriteLine("Output action processed.");
}
void SwitchStatus(object source, ElapsedEventArgs e)
{
IsInputting = !IsInputting;
IsOutputting = !IsInputting;
while (IsInputting)
func_in();
while (IsOutputting)
func_out();
}
public void Start()
{
IsRunning = true;
if (IsRunning)
{
IsInputting = true;
Timer timer = new Timer(2 * 1000);
timer.Elapsed += new ElapsedEventHandler(SwitchStatus);
timer.Start();
}
}
}
----可以使用队列,一个线程不断的往队列中写数据一个线程不断取数据(队列先进先出),注意在多线程对队列操作时需要加锁,让它在读和写的某一时刻独占队列
using System.Threading;
class Sample
{
bool IsRunning = false; private void func_in()
{
//BufferProp = InputData();
Console.WriteLine("Input action processed.");
}
private void func_out()
{
//OutputDevice = BufferProp;
Console.WriteLine("Output action processed.");
} public void MainThread()
{
while (true)
{
Thread th = new Thread(new ThreadStart(func_in));//读数据线程
th.Start();
Thread.Sleep(2000);//2秒后启动新线程收数据
th = new Thread(new ThreadStart(func_out));//读数据线程
th.Start();
}
} public void Start()
{
if (!IsRunning)
{
//主线程
Thread th = new Thread(new ThreadStart(MainThread));
th.Start();
IsRunning = true;
}
}
}