小编dtb*_*dtb的帖子

结合时间紧密的事件,提高效率和公平性

我有一堆线程生成类型A和类型的事件B.

我的程序接收这些事件,将它们包装在一条消息中并通过网络发送.消息可以包含一个A事件,一个B事件或一个A事件和一个B事件:

SendMessage(new Message(a: 1,    b: null));
SendMessage(new Message(a: null, b: 2   ));
SendMessage(new Message(a: 3,    b: 4   ));
Run Code Online (Sandbox Code Playgroud)

类型A事件经常发生,而类型事件发生的频率则B低得多.因此,当一个线程生成一个B事件时,我的程序会稍等一下,看看另一个线程是否生成一个A事件,A并在B可能的情况下组合事件和事件.

这是我的代码:

object gate = new object();
int? pendingB;

Message WrapA(int a, int millisecondsTimeout)
{
    int? b;

    lock (gate)
    {
        b = pendingB;
        pendingB = null;
        Monitor.Pulse(gate);
    }

    return new Message(a, b);
}

Message WrapB(int b, int …
Run Code Online (Sandbox Code Playgroud)

c# concurrency

26
推荐指数
2
解决办法
986
查看次数

使用XDocument&Linq读取XML - 检查元素是否为NULL?

我正在使用LINQ和XDocument来读取XML文件.这是代码:

XDocument xml = XDocument.Load(filename);

var q = from b in xml.Descendants("product")
        select new
        {
            name = b.Element("name").Value,
            price = b.Element("price").Value,                    
            extra = b.Element("extra1").Value,
            deeplink = b.Element("deepLink").Value                   
        };
Run Code Online (Sandbox Code Playgroud)

现在的问题是,该extra1领域并不总是存在.没有该节点的XML文件中有项目.如果发生这种情况,它会因NullReferenceException而崩溃.

有没有可能包括"检查是否为空",以便我可以防止它崩溃?

c# xml linq asp.net

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

C#中的枚举使用负数的负面影响

在C#枚举中,使用负数是否有任何负面影响?

我正在建模响应代码,其中一个代码为负数.这编译,但我想知道是否有任何负面影响.

public enum ResponseCodes
{
    InvalidServerUserPasswordCombo = -1,

    // etc.
}
Run Code Online (Sandbox Code Playgroud)

c#

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

使用用户名和密码连接到网络驱动器

如何提供凭证以便我可以连接到.NET中的网络驱动器?

我正在尝试从网络驱动器检索文件,并需要提供用户凭据才能访问该驱动器.

.net c#

21
推荐指数
4
解决办法
10万
查看次数

如何XML序列化字典

我已经能够以这种方式序列化IEnumerable:

[XmlArray("TRANSACTIONS")]
[XmlArrayItem("TRANSACTION", typeof(Record))]
public IEnumerable<BudgetRecord> Records
{
    get 
    {
        foreach(Record br in _budget)
        {
            yield return br;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我意识到现在我需要一个包含集合的字典Dictionary<string, RecordCollection>(RecordCollection实现IEnumerable).

我怎样才能做到这一点?

.net c# xml-serialization xmlserializer

20
推荐指数
4
解决办法
6万
查看次数

HttpClient不在CookieContainer中存储cookie

我正在使用VS2010+ .NET 4.0+ System.Net.Http(来自Nuget).

由于我无法理解的原因,我收到的会话cookie HttpResponseMessage不会自动保存在HttpClient CookieContainer.这是我的代码的样子:

CookieContainer cookies = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = cookies;
HttpClient client = new HttpClient(handler);

Uri site = new Uri("https://www.mywebsite.com");
var response1 = client.SendAsync(new HttpRequestMessage(HttpMethod.Get,site)).Result;
Run Code Online (Sandbox Code Playgroud)

我可以在响应头中看到我有以下内容:

Set-Cookie: JSESSIONID=FC8110E434C2C6DAB78B4E335024A639; Path=/member; Secure
Run Code Online (Sandbox Code Playgroud)

但是我的cookie容器仍然是空的...为什么?

c# cookies httpclient

20
推荐指数
2
解决办法
3万
查看次数

什么是.NET中的渠道工厂?

什么是渠道工厂,为什么要使用它?

c# wcf remoting

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

压缩WeakReference词典

我有一个带有属性Id的Foo类.我的目标是同时没有两个具有相同IdFoo实例.

所以我创建了一个工厂方法CreateFoo,它使用缓存来为同一个Id返回相同的实例.

static Foo CreateFoo(int id) {
    Foo foo;
    if (!cache.TryGetValue(id, out foo)) {
        foo = new Foo(id);
        foo.Initialize(...);
        cache.Put(id, foo);
    }
    return foo;
}
Run Code Online (Sandbox Code Playgroud)

缓存实现为Dictionary <TKey,WeakReference>,基于@JaredParBuilding a WeakReference Hashtable:

class WeakDictionary<TKey, TValue> where TValue : class {
    private readonly Dictionary<TKey, WeakReference> items;
    public WeakDictionary() {
        this.items = new Dictionary<TKey, WeakReference>();
    }
    public void Put(TKey key, TValue value) {
        this.items[key] = new WeakReference(value);
    }
    public …
Run Code Online (Sandbox Code Playgroud)

c# weak-references .net-4.0

16
推荐指数
2
解决办法
5581
查看次数

了解32位与64位之间的CLR对象大小

我试图了解32位和64位处理器之间的对象大小差异.假设我有一个简单的课程

class MyClass   
{  
    int x;  
    int y;  
}  
Run Code Online (Sandbox Code Playgroud)

所以在32位机器上,整数是4个字节.如果我将Syncblock添加到其中(另外4个字节),则对象大小将为12个字节.为什么显示16个字节?

0:000> !do 0x029d8b98  
Name: ConsoleApplication1.Program+MyClass  
MethodTable: 000e33b0  
EEClass: 000e149c  
Size: 16(0x10) bytes  
 (C:\MyTemp\ConsoleApplication1\ConsoleApplication1\bin\x86\Debug\ConsoleApplication1.exe)  
Fields:  
      MT    Field   Offset                 Type VT     Attr    Value Name  
71972d70  4000003        4         System.Int32  1 instance        0 x  
71972d70  4000004        8         System.Int32  1 instance        0 y  

在64位机器上,一个整数仍然是4个字节,唯一改变的是Syncblock将是8个字节(因为指针是64位机器上的8个字节).这意味着对象大小将是16个字节.为什么显示24个字节?

0:000> !do 0x00000000028f3c90  
Name: ConsoleApplication1.Program+MyClass  
MethodTable: 000007ff00043af8  
EEClass: 000007ff00182408  
Size: 24(0x18) bytes  
 (C:\MyTemp\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe)  
Fields:  
              MT    Field   Offset                 Type VT     Attr            Value Name  
000007fef4edd998  4000003        8         System.Int32  1 instance                0 x  
000007fef4edd998 …

.net windbg sos

16
推荐指数
2
解决办法
2787
查看次数

ProgressBar没有值,只是加载

使用WinForms创建这样的进度条我需要做什么?

进度条

c# winforms progress-bar

15
推荐指数
1
解决办法
7501
查看次数