解决方案 »
- C# 关于DataTable的问题 很奇怪
- datagrid1-dataset1-datagrid2问题
- 想问一下手机定位一般采用什么方式?
- 〓〓〓〓 求:从数据库中查询数据到dataset,把dataset中的数据存成XML,然后把XML写成dataset再写入数据库的完整例子!
- [*119*]如何实现变量来定义类型???????
- C#简单的数据传输的服务器问题
- 有关连接字串ConnectionString部署问题!
- duwawish7.0安装问题,在线等待.........
- 关于crystalreport的问题.在线等待.
- 请教有使用C#开发“条码技术"吗?
- 建议csdn开个讨论"MONO"论坛,支持者请跟贴
- 请教微软专家.NET写Access数据库的问题
List<byte>
Imports System.Threading
Imports System.ComponentModel
Imports System.Reflection''' <summary>
''' 用于多线程下存取对象。
''' MSTOP
''' 2011.5.6
''' </summary>
''' <res></res>
<DefaultMemberAttribute("Value")> Public Class MyObjBox(Of T)#Region "声明/变量"
<Browsable(False), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(CType(False, DebuggerBrowsableState))>
Private ObjBox As New ConcurrentDictionary(Of String, T)(8, 50)
#End Region#Region "接口" ''' <summary>
''' 读盒子所有值的KEY.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <res></res>
Public ReadOnly Property Keys As String()
Get
Dim KeyArry() As String = ObjBox.Keys().ToArray
Return KeyArry
End Get
End Property Public Function ContainsKey(Key As String) As Boolean
Key = Key.ToUpper
Return ObjBox.ContainsKey(Key)
End Function ''' <summary>
''' 移除一个值.
''' </summary>
''' <param name="Key"></param>
''' <returns></returns>
''' <res></res>
Public Function RemoveKey(Key As String) As Boolean
Dim ReturnBln As Boolean = False
Dim Val As T = Nothing
Dim NextID As Int32 = 0
Dim EndID As Int32 = 200
Dim Random As Int32 TryNextControl:
If ObjBox.ContainsKey(Key) Then
If Not ObjBox.TryRemove(Key, Val) Then
Random = (1 + Asc(Guid.NewGuid.ToString("N")) Mod 50)
Windows.Forms.Application.DoEvents()
Thread.Sleep(Random)
NextID += 1
If NextID < EndID Then
GoTo NextControl
Else
ReturnBln = False
GoTo EndFunction
End If
Else '// TryRemove 成功
ReturnBln = True
GoTo EndFunction
End If
Else
ReturnBln = False
GoTo EndFunction
End If
Catch
ReturnBln = False
GoTo EndFunction
End TryEndFunction:
Return ReturnBln
End Function ''' <summary>
''' 清空.
''' </summary>
''' <res></res>
Public Sub Clear()
ObjBox.Clear()
End Sub Public Function Count() As Int32
Return ObjBox.Count
End Function ''' <summary>
''' 根据 Key 对盒子 赋值/取值.( 赋值时 Key 如果不存在,会新增,如果存在,则覆盖。)
''' </summary>
''' <param name="Key"></param>
''' <value>不能是 NULL,NOTHING,""," " 等 空或非初始化的对象。</value>
''' <returns></returns>
''' <res></res>
Public Property Value(Key As String) As T Set(value As T)
If value Is Nothing Then Exit Property
Key = Key.ToUpper
Call Add(Key, value)
End Set Get
Key = Key.ToUpper
Return GetValue(Key)
End Get End Property#End Region#Region "操作" ''' <summary>
''' 读
''' </summary>
''' <param name="Key"></param>
''' <returns></returns>
''' <res></res>
<Browsable(False), EditorBrowsable(EditorBrowsableState.Never)>
Private Function GetValue(Key As String) As T Dim ReturnObj As T = Nothing
Dim NextID As Int32 = 0
Dim EndID As Int32 = 200
Dim Random As Int32 TryNextControl:
If ObjBox.ContainsKey(Key) Then
If Not ObjBox.TryGetValue(Key, ReturnObj) Then
Random = (1 + Asc(Guid.NewGuid.ToString("N")) Mod 50)
Windows.Forms.Application.DoEvents()
Thread.Sleep(Random)
NextID += 1
If NextID < EndID Then
GoTo NextControl
Else
ReturnObj = Nothing
GoTo EndFunction
End If
Else '// TryGetValue 得到了 ReturnVal
GoTo EndFunction
End If
Else
ReturnObj = Nothing
GoTo EndFunction
End If
Catch
ReturnObj = Nothing
GoTo EndFunction
End TryEndFunction:
Return ReturnObj End Function ''' <summary>
''' 添加
''' </summary>
''' <param name="Key"></param>
''' <param name="obj"></param>
''' <returns></returns>
''' <res></res>
<Browsable(False), EditorBrowsable(EditorBrowsableState.Never)>
Private Function Add(Key As String,
Obj As T) As Boolean If (Key Is Nothing) OrElse (Obj Is Nothing) Then Return False Dim ReturnVal As Boolean = False
Dim NextID As Int32 = 0
Dim EndID As Int32 = 200
Dim Random As Int32
Dim vObj As T = Nothing Dim LA As New Lazy(Of T)(Function() Obj, True)
TryNextControl:
vObj = ObjBox.AddOrUpdate(Key, Obj, Function()
Return LA.Value
End Function)
If (vObj Is Nothing) Then
Random = (1 + Asc(Guid.NewGuid.ToString("N")) Mod 50)
Windows.Forms.Application.DoEvents()
Thread.Sleep(Random)
NextID += 1
If NextID < EndID Then
GoTo NextControl
Else
ReturnVal = False
GoTo EndFunction
End If
Else
ReturnVal = True
GoTo EndFunction
End If Catch
ReturnVal = False
GoTo EndFunction
End TryEndFunction:
Return ReturnVal End Function
''' <summary>
''' 移除
''' </summary>
''' <param name="Key"></param>
''' <returns></returns>
''' <res></res>
Public Function Remove(Key As String) As Boolean Dim ReturnVal As Boolean = False
Dim NextID As Int32 = 0
Dim EndID As Int32 = 200
Dim Random As Int32 TryNextControl:
If ObjBox.ContainsKey(Key) Then
If Not ObjBox.TryRemove(Key, Nothing) Then
Random = (1 + Asc(Guid.NewGuid.ToString("N")) Mod 50)
Windows.Forms.Application.DoEvents()
Thread.Sleep(Random)
NextID += 1
If NextID < EndID Then
GoTo NextControl
Else
ReturnVal = False
GoTo EndFunction
End If
Else
ReturnVal = True
GoTo EndFunction
End If
Else
ReturnVal = True
GoTo EndFunction
End If
Catch
ReturnVal = False
GoTo EndFunction
End TryEndFunction:
Return ReturnVal End Function#End RegionEnd Class
做统计需要数据准确,这时需要避免UI线程对主线程的占用,保证IO数据不丢失,这时一般用双进程。
//将数据项目加入缓存
protected void btnAddCache_Click(object sender, EventArgs e)
{
//利用Cache.Add()方法将数据加入缓存
Cache.Add("Name", txtUserName.Text, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);
Cache.Add("Photo", txtTel.Text, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);
Cache.Add("Position", txtJob.Text, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);
txtMsg.Text = "缓存加入成功!";
}
//显示缓存数据
protected void btnDisplayCache_Click(object sender, EventArgs e)
{
IDictionaryEnumerator CacheIDE = Cache.GetEnumerator();//显示缓存数据
int i = 0;
string info = null;
info += "缓存项目数据(Key / Value):" + "<br>";
while (CacheIDE.MoveNext())//循环输出缓存项目
{
info += i.ToString() + ". ";
info += CacheIDE.Key.ToString() + " : ";
info += CacheIDE.Value.ToString() + "<br>";
i++;
}
if (Cache["Name"] == null)//判断缓存是否有数据项目
{
txtMsg.Text = "缓存内容为Null值!";
}
else
{
txtMsg.Text = info;
}
}
使用lock算是保险操作,但针对多线程对“缓冲池” 进行控制,肯定是不可以
lock的,否则不如单线程完成,如果是多线程首先你必须计算你当前的线程总数计算
每条线程所需要负责的“缓冲块”大小包括起始索引 将所有都部署完毕才开始进行 多线
分布式缓冲区读写。