110,537
社区成员
发帖
与我相关
我的任务
分享
public partial class frmThread : Form
{
public frmThread()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
public delegate void PercentCompleteEventHandler(string Message, int Percent);
event PercentCompleteEventHandler PercentComplete;
private void button1_Click(object sender, EventArgs e)
{
m_tspbProgressBar.Value = 0;
m_tspbProgressBar.Minimum = 0;// 进度条
m_tspbProgressBar.Maximum = 100;
m_tspbProgressBar.Visible = true;
PercentComplete = new PercentCompleteEventHandler(Step);
ThreadStart start = new ThreadStart(DataRebateExcute);
start.BeginInvoke(AsyncCallbackMethod,null);
}
/// <summary>
/// 数据处理
/// </summary>
public void DataRebateExcute()
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(100);
PercentComplete.Invoke(i.ToString(), i);
}
}
/// <summary>
/// 进度条调整
/// </summary>
public void Step(string Message, int percent)
{
m_tspbProgressBar.Value = percent;
Application.DoEvents();
}
/// <summary>
/// 线程完成后回调
/// </summary>
/// <param name="myIAsyncResult"></param>
public void AsyncCallbackMethod(System.IAsyncResult myIAsyncResult)
{
m_tspbProgressBar.Visible = false;
MessageBox.Show("成功", this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
/*
* 先进先出缓冲
* 缓冲满,丢掉后来的数据
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using System.Collections;
public class ZDropFifo : IDisposable
{
#region variable
protected Queue m_inputQueue;
protected int 主缓冲长度 = 0;
protected int 允许读取的起始位置 = 0;
protected byte[] 主缓冲 = null;
protected int 写指针 = 0;
protected bool 写位置 = false;
protected int 读指针 = 0;
protected bool 读位置 = false;
//计算位置使用的变量
protected int 到缓冲结尾的距离 = 0;
protected int 剩余的字节长度 = 0;
public bool 可以读 = false;
public int 有效数据长度 = 0;
protected int 写入长度 = -2;
protected int 读取长度 = -2;
int temp = 0;
#endregion
#region public function
public ZDropFifo(int 长度, int 起始)
{
m_inputQueue = new Queue();
主缓冲长度 = 长度;
允许读取的起始位置 = 起始;
主缓冲 = new byte[主缓冲长度];
}
public void Dispose()
{
主缓冲 = null;
m_inputQueue = null;
}
public int 写(byte[] 源, int 源长度)
{
lock (m_inputQueue)
{
//Monitor.Enter(m_inputQueue);
//Console.WriteLine("写");
if (可以读 == false && 写指针 >= 允许读取的起始位置)
可以读 = true;
if (写位置 == 读位置) //写指针 读指针 在同一圈
{
//计算有效数据长度
有效数据长度 = 写指针 - 读指针;
到缓冲结尾的距离 = 主缓冲长度 - 写指针;
剩余的字节长度 = 源长度 - 到缓冲结尾的距离;
if (到缓冲结尾的距离 > 源长度)
{
Buffer.BlockCopy(源, 0, 主缓冲, 写指针, 源长度);
写指针 += 源长度;
//Monitor.Exit(m_inputQueue);
//return 源长度;
写入长度 = 源长度;
}
else //写指针到缓冲结尾的距离<源长度
{
if ((读指针 + 到缓冲结尾的距离) > 源长度)
{
Buffer.BlockCopy(源, 0, 主缓冲, 写指针, 到缓冲结尾的距离);
Buffer.BlockCopy(源, 到缓冲结尾的距离, 主缓冲, 0, 剩余的字节长度);
写指针 = 剩余的字节长度;
写位置 = !写位置;
//Monitor.Exit(m_inputQueue);
//return 源长度;
写入长度 = 源长度;
}
else //读指针 + 到缓冲结尾的距离 < 源长度
{
//Monitor.Exit(m_inputQueue);
//return 0;
写入长度 = 0;
}
}
}
else //写指针 读指针 不在同一圈
{
//计算有效数据长度
有效数据长度 = 主缓冲长度 + 写指针 - 读指针;
剩余的字节长度 = 读指针 - 写指针;
if (剩余的字节长度 > 源长度)
{
Buffer.BlockCopy(源, 0, 主缓冲, 写指针, 源长度);
写指针 += 源长度;
//Monitor.Exit(m_inputQueue);
//return 源长度;
写入长度 = 源长度;
}
else //写指针到读指针的字节长度 < 源长度
{
//Monitor.Exit(m_inputQueue);
//return 0;
写入长度 = 0;
}
}
}
return 写入长度;
}
public int 读(byte[] 目的, int 目的长度)
{
//Monitor.Enter(m_inputQueue);
lock (m_inputQueue)
{
if (可以读)
{
if (写位置 == 读位置)
{
有效数据长度 = 写指针 - 读指针;
if ((写指针 - 读指针) > 目的长度)
{
Buffer.BlockCopy(主缓冲, 读指针, 目的, 0, 目的长度);
读指针 += 目的长度;
//Monitor.Exit(m_inputQueue);
//return 目的长度;
读取长度 = 目的长度;
}
else
{
//Monitor.Exit(m_inputQueue);
//return 0;
读取长度 = 0;
}
}
else
{
有效数据长度 = 主缓冲长度 + 写指针 - 读指针;
到缓冲结尾的距离 = 主缓冲长度 - 读指针;
if (到缓冲结尾的距离 > 目的长度)
{
Buffer.BlockCopy(主缓冲, 读指针, 目的, 0, 目的长度);
读指针 += 目的长度;
//Monitor.Exit(m_inputQueue);
//return 目的长度;
读取长度 = 目的长度;
}
else
{
剩余的字节长度 = 目的长度 - 到缓冲结尾的距离;
if (剩余的字节长度 < 写指针)
{
Buffer.BlockCopy(主缓冲, 读指针, 目的, 0, 到缓冲结尾的距离);
Buffer.BlockCopy(主缓冲, 0, 目的, 到缓冲结尾的距离, 剩余的字节长度);
读指针 = 剩余的字节长度;
读位置 = !读位置;
//Monitor.Exit(m_inputQueue);
//return 目的长度;
读取长度 = 目的长度;
}
else
{
//Monitor.Exit(m_inputQueue);
//return 0;
读取长度 = 0;
}
}
}
}
}
return 读取长度;
}
public byte[] 缓冲区
{
get
{
return 主缓冲;
}
}
#endregion
protected int 加指针(int n)
{
if (写位置 == 读位置)
{
temp = 读指针 + n;
}
else
{
temp = 读指针 + n;
if (temp < 主缓冲长度)
{
//return temp;
}
else
{
temp -= 主缓冲长度;
//return 0;
}
}
return temp;
}
}