小编luc*_*cky的帖子

ThreadStatic的C#Singleton模式设计

我想弄清楚单身模式设计.我想从我的单例类为每个线程创建单独的实例.所以我在下面提供了两个设计.

这是工作

class Program
{
    static void Main(string[] args)
    {
        Task.Factory.StartNew(() => Console.WriteLine(SingletonClass.Instance.GetHashCode()));
        Task.Factory.StartNew(() => Console.WriteLine(SingletonClass.Instance.GetHashCode()));
        Console.ReadLine();
    }
}
public sealed class SingletonClass
{
    [ThreadStatic]
    private static SingletonClass _instance;

    public static SingletonClass Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new SingletonClass();
            }
            return _instance;
        }
    }
    private SingletonClass()
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

它不起作用(抛出NullReferenceException并且没有创建实例.)

class Program
{
    static void Main(string[] args)
    {
        Task.Factory.StartNew(() => Console.WriteLine(SingletonClass.Instance.GetHashCode()));
        Task.Factory.StartNew(() => Console.WriteLine(SingletonClass.Instance.GetHashCode()));
        Console.ReadLine();
    }
}
public sealed class SingletonClass
{
    [ThreadStatic]
    private …
Run Code Online (Sandbox Code Playgroud)

c# singleton multithreading singleton-methods threadstatic

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

SIP协议中的Call-ID和Branch标签

我正在开发一个SIP客户端.我理解SIP请求和SIP响应,但在SIP消息中,如何生成呼叫ID和分支标签?RFC3261没有指定这一点.

sip

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

C# 监听 TCP 端口

我想监听 TCP 端口。场景:

SIP 服务器和客户端程序。SIP服务器可以使用端口5010来监听并建立多个连接。客户端通过端口 5010 从他的电脑连接到服务器。

当该端口收到数据包时,必须在客户端 PC 上触发一个事件。

有什么想法从哪里开始吗?

c# tcp tcplistener

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

HTML表单文件集值

如果我执行以下操作,浏览文本框仍然无法看到C:/ hello world/test.txt.

<input type="file" name="fileName" value="C:/hello world/test.txt" size=80>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?和解决这个问题的方法?谢谢!!

html forms file

-1
推荐指数
1
解决办法
475
查看次数

将System.Action转换为String

我有一个功能......

var neighbours =
    from x in Enumerable.Range(0, array2.GetLength(0))
        .Where(x => Math.Abs(x - refx) <= 1)
    from y in Enumerable.Range(0, array2.GetLength(1))
        .Where(y => Math.Abs(y - refy) <= 1)
    select new { x, y };
neighbours.ToList().ForEach(Console.WriteLine);
Run Code Online (Sandbox Code Playgroud)

这个功能运作良好.但我想要:

var neighbours =
    from x in Enumerable.Range(0, array2.GetLength(0))
        .Where(x => Math.Abs(x - refx) <= 1)
    from y in Enumerable.Range(0, array2.GetLength(1))
        .Where(y => Math.Abs(y - refy) <= 1)
    select new { x, y };
neighbours.ToList().ForEach(label3.Text);
Run Code Online (Sandbox Code Playgroud)

它不起作用.所以,我想将System.Action转换为String ...对此有何看法?

c#

-3
推荐指数
1
解决办法
1240
查看次数