小编C-v*_*-va的帖子

快速和最佳生产者/消费者队列技术BlockingCollection与并发队列

我在C#3.0和Monitor.Enter中使用Generic.Queue,等待,在使用队列之前退出等待(等待元素入队).现在我转到C#4.

任何人都可以建议我哪一个是快速的,尤其是避免锁定..

BlockingCollection vs concurrentQueue或其他任何东西......

注意.我不想限制我的制片人

提前致谢..

concurrency consumer producer c#-3.0

39
推荐指数
1
解决办法
6万
查看次数

尝试在ConcurrentQueue中出列队列

如果Queue中没有项目,ConcurrentQueue中的TryDequeue将返回false.

如果队列是空的,我需要我的队列将等待,直到新项目被添加到队列中并且它将新队列出列,并且该过程将继续这样.

我应该在C#4.0中使用monitor.enter,wait,pulse或任何更好的选项

c# producer-consumer concurrent-queue

24
推荐指数
1
解决办法
2万
查看次数

C#中的整数转换

        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)

c# int

14
推荐指数
1
解决办法
3906
查看次数

C#中的动态绑定

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)

c# dynamic-binding

10
推荐指数
1
解决办法
8343
查看次数

异步委托与线程

用异步委托(回调)替换线程(而不是ThreadPool线程).

我的场景:为每个客户端生成一个Thread/del.beginInvoke().

据我说,

原因

  1. 需要在Callback中通过Callback/Call delegate再次通知
  2. 避免线程开销,(委托使用Threadpool线程)
  3. 传递参数(避免转换为对象)并需要从方法返回值.

如果上述原因错误,请纠正我.

  1. 还有其他原因吗?
  2. 什么情况我确实需要用线程不能的异步委托做一些事情?
    3.表现?

    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)

c# delegates asynchronous

7
推荐指数
1
解决办法
1万
查看次数

wcf中的knowntype属性问题

我的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)

wcf known-types

6
推荐指数
2
解决办法
1万
查看次数

服务器套接字挂起在Close_Wait中

方案如下:
我从C#Server Application向客户端发送大量数据.
突然数据流停止,客户端没有数据更新,端口被阻止.

发生这种情况时,服务器端的症状是

  1. Close_Wait在该端口(netstat)
  2. 操作将阻止(错误代码:135)在日志中.
  3. 无法从客户端建立新连接.
  4. Telnet失败到该端口

目前,我们会在出现问题时重新启动服务器.显然这不是一个解决方案.

有人可以为Windows Server 2008中的Close_Wait中的服务器套接字提供解决方案吗?对此场景的任何建议都将不胜感激.

提前致谢.

c# sockets tcp

5
推荐指数
1
解决办法
3645
查看次数

具有回调的代理上的内存泄漏

 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# delegates memory-leaks callback

4
推荐指数
1
解决办法
1543
查看次数