我在C#3.0和Monitor.Enter中使用Generic.Queue,等待,在使用队列之前退出等待(等待元素入队).现在我转到C#4.
任何人都可以建议我哪一个是快速的,尤其是避免锁定..
BlockingCollection vs concurrentQueue或其他任何东西......
注意.我不想限制我的制片人
提前致谢..
如果Queue中没有项目,ConcurrentQueue中的TryDequeue将返回false.
如果队列是空的,我需要我的队列将等待,直到新项目被添加到队列中并且它将新队列出列,并且该过程将继续这样.
我应该在C#4.0中使用monitor.enter,wait,pulse或任何更好的选项
string[] strArray = new string[10] { "21.65", "30.90", "20.42", "10.00", "14.87", "72.19", "36.00", "45.11", "18.66", "22.22" };
float temp = 0.0f;
Int32 resConvert = 0;
Int32 resCast = 0;
for (int i = 0; i < strArray.Length; i++)
{
float.TryParse(strArray[i], out temp);
resConvert = Convert.ToInt32(temp * 100);
resCast = (Int32)(temp * 100);
Console.WriteLine("Convert: " + resConvert + " ExplCast: " + resCast);
}
Run Code Online (Sandbox Code Playgroud)
答案:
Convert: 2165 ExplCast: 2164 // ??
Convert: 3090 ExplCast: 3089 // ??
Convert: 2042 ExplCast: 2042 …Run Code Online (Sandbox Code Playgroud) class A
{
public virtual void WhoAreYou() { Console.WriteLine("I am an A"); }
}
class B : A
{
public override void WhoAreYou() { Console.WriteLine("I am a B"); }
}
class C : B
{
public new virtual void WhoAreYou() { Console.WriteLine("I am a C"); }
}
class D : C
{
public override void WhoAreYou() { Console.WriteLine("I am a D"); }
}
C c = new D();
c.WhoAreYou();// "I am a D"
A a = new D();
a.WhoAreYou();// "I …Run Code Online (Sandbox Code Playgroud) 用异步委托(回调)替换线程(而不是ThreadPool线程).
我的场景:为每个客户端生成一个Thread/del.beginInvoke().
据我说,
原因
如果上述原因错误,请纠正我.
例
public delegate void SendCallbackType();
SendCallbackType senderdel= new SendCallbackType(SendData);
public void StartSend() // This method Could be Called more than 700 times (Thread per Client)
{
senderdel.BeginInvoke(SendCallback,null);
// (or)
Thread t = new Thread(new ThreadStart(ThreadSend));
t.IsBackground = true;
t.Start();
}
//Async Delegate
void SendData()
{
string data = QueData.DeQueue();
RaiseOnData(data); // Raise to event.
}
void SendCallback(IAsyncResult ar)
{
senderdel.BeginInvoke(SendCallback, null);
}
//Thread
void ThreadSend()
{
while (true) …Run Code Online (Sandbox Code Playgroud) 我的wcf客户端出现以下错误.
NetDispatcherFaultException未处理.
格式化程序在尝试反序列化消息时抛出异常:尝试反序列化参数http://tempuri.org/:GetVehicleResult时出错.InnerException消息是'第1行中的错误266.元素' http://tempuri.org/:GetVehicleResult '包含映射到名称' http://schemas.datacontract.org/2004/07/的类型的数据WCFServer:Car '.反序列化器不知道映射到此名称的任何类型.考虑使用DataContractResolver或将与"Car"对应的类型添加到已知类型列表中 - 例如,通过使用KnownTypeAttribute属性或将其添加到传递给DataContractSerializer的已知类型列表中.有关更多详细信息,请参阅InnerException.
任何人都可以帮助我在哪里是错误.
WCF服务器
IVehicle
--------
[ServiceContract]
public interface IVehicleService
{
[OperationContract]
Vehicle GetVehicle(int type);
[OperationContract]
int GetNumberOfWheels(Vehicle vehicle);
}
Run Code Online (Sandbox Code Playgroud)
VehicleService
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class VehicleService : IVehicleService
{
public Vehicle GetVehicle(int type)
{
switch (type)
{
case 0:
return new Car()
{
ID = 10,
Brand = "Volvo",
SteeringWheelPosition = "left"
};
case 1:
return new bike()
{
ID = 11,
Brand = "Scott",
HasFrontWheelBreak = true …Run Code Online (Sandbox Code Playgroud) 方案如下:
我从C#Server Application向客户端发送大量数据.
突然数据流停止,客户端没有数据更新,端口被阻止.
发生这种情况时,服务器端的症状是
目前,我们会在出现问题时重新启动服务器.显然这不是一个解决方案.
有人可以为Windows Server 2008中的Close_Wait中的服务器套接字提供解决方案吗?对此场景的任何建议都将不胜感激.
提前致谢.
public delegate void SendCallbackType();
public class SenderBase
{
SenderBase()
{
mySend = new SendCallbackType(SendData);
mySend.BeginInvoke(SendCallback, null);
}
void SendData()
{
// process / sending data
}
void SendCallback(IAsyncResult ar)
{
**SendCallbackType worker = (SendCallbackType)((AsyncResult)ar).AsyncDelegate;
worker.EndInvoke(ar);**
//Above code is mandatory ? Working fine without them.
mySend.BeginInvoke(SendCallback, null);
}
// Test
Dictionary<SenderBase> SenderCollection = new Dictionary();
SenderCollection.Add(new SenderBase());
SenderCollection.Remove(0);
// Add and remove seven times
Run Code Online (Sandbox Code Playgroud)
对象(SenderBase)不是垃圾收集的.他们不断向下一代移动.
使用RedAnts Memory Profiler,

任何清理对象的建议.
谢谢.
c# ×6
delegates ×2
asynchronous ×1
c#-3.0 ×1
callback ×1
concurrency ×1
consumer ×1
int ×1
known-types ×1
memory-leaks ×1
producer ×1
sockets ×1
tcp ×1
wcf ×1