小编Hel*_*lic的帖子

为什么我们需要在ConnectionString中设置Min池大小

对于SQL连接池,为什么我们需要设置最小池大小?由于连接将保存在连接池中并重用,为什么我们需要保持最小池大小指定的实时连接?谢谢.

c# connection sqlconnection pool

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

AggregateExpression中的C#InnerException

我有如下代码来捕获使用TaskFactory和Task.Run创建的任务抛出异常.如果我使用TaskFactory,我可以检查继续任务中从上一个任务抛出的异常,而不必使用Task.WaitAll方法.如果我使用Task.Run,​​则除非我明确等待子任务完成,否则将不会执行Continued任务.TaskFactory.StartNew中的哪个标志改变了这种行为?

另外,AggregateException类中的InnerException和InnerExceptions有什么区别?InnerExceptions返回一个由子任务抛出的所有异常的只读集合.InnerException返回仅由一个子任务抛出的异常的AggregateExcpetion.

//Use Factory
TaskCreationOptions atp = TaskCreationOptions.AttachedToParent;
Task.Factory.StartNew(() =>
{
    Task.Factory.StartNew (() => { throw null; }, atp);
    Task.Factory.StartNew (() => { throw new NullReferenceException();}, atp);
    Task.Factory.StartNew (() => { throw new Exception("Test"); }, atp);
})
.ContinueWith (p => p.Exception.Dump(),TaskContinuationOptions.OnlyOnFaulted);

//Use Task.Run
Task.Run(()=>
{
    TaskCreationOptions op = TaskCreationOptions.AttachedToParent;
    var t1 = Task.Factory.StartNew(()=> {throw null;}, op);
    var t2 = Task.Factory.StartNew(()=> {throw new NullReferenceException();}, op);
    var t3 = Task.Factory.StartNew(()=> {throw new Exception("Test");}, op);

    //This will trigger the continued task
    //Task.WaitAll(new Task[]{t1,t2,t3}); …
Run Code Online (Sandbox Code Playgroud)

c# aggregateexception

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

上下文菜单绑定到父窗口的数据上下文

我有一个 TreeListControl 绑定到我的虚拟机中的一个集合。我还想在树列表控件内定义上下文菜单,使其标题文本绑定到虚拟机中的另一个字符串。在这种情况下如何设置数据上下文?我尝试过了

<Window.DataContext>
    <model:ViewModel></model:ViewModel>
</Window.DataContext>
<Grid>
<Button Grid.Row="1"  Command="{Binding CellCheckedCommand}"></Button>

    <TextBlock Text="{Binding HeaderText}" Grid.Row="2">
        <TextBlock.ContextMenu>
            <ContextMenu>
                <MenuItem DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext}"  Header="{Binding HeaderText}"></MenuItem>
            </ContextMenu>
        </TextBlock.ContextMenu>
    </TextBlock>
</Grid>
Run Code Online (Sandbox Code Playgroud)

但它不起作用。

这是视图模型

public DelegateCommand CellCheckedCommand { get; set; }

private String _HeaderText;

public String HeaderText 
{
    get
    {
        return _HeaderText;
    }
    set
    {
        _HeaderText = value;
        NotifyPropertyChanged("HeaderText");
    }
}

public void NotifyPropertyChanged(String name)
{ 
    if(PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
}

private void CellCheckedMethod()
{
    HeaderText = "Changed";
}
Run Code Online (Sandbox Code Playgroud)

wpf datacontext binding c#-4.0

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

Windows中的C#get file owner

我想使用下面的代码获取文件的所有者

File.GetAccessControl(filename).GetOwner(typeof(SecurityIdentifier)).Translate(typeof(NTAccount))
Run Code Online (Sandbox Code Playgroud)

但是,它给了我BUILTIN\Administrators作为所有者,但我可以在文件资源管理器中看到所有者是Domain\MyUserName.

为什么会发生这种情况以及如何解决?

编辑: 此链接说明发生了什么.它与管理员组中的用户创建的文件以及Windows如何处理这些文件的所有者有关.

c# file owner

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

C#中的ThreadLocal与局部变量

这个ThreadName和LocalName在下面的代码中有什么区别?他们都是ThreadLocal吗?

// Thread-Local variable that yields a name for a thread
ThreadLocal<string> ThreadName = new ThreadLocal<string>(() =>
{
    return "Thread" + Thread.CurrentThread.ManagedThreadId;
});

// Action that prints out ThreadName for the current thread
Action action = () =>
{
    // If ThreadName.IsValueCreated is true, it means that we are not the 
    // first action to run on this thread. 
    bool repeat = ThreadName.IsValueCreated;
    String LocalName = "Thread" + Thread.CurrentThread.ManagedThreadId;
    System.Diagnostics.Debug.WriteLine("ThreadName = {0} {1} {2}", ThreadName.Value, repeat ? "(repeat)" : "", …
Run Code Online (Sandbox Code Playgroud)

c# multithreading local

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

LambdaExpression构造函数

我看到了如下代码.我的问题是:

1> ()=>Name这是什么意思?

2>是Expression<Func<>>一样的Expression<TDelegate>吗?如何使用()=>Name转换Expression<Func<>>和使用哪个构造函数?大多数Expression类没有公共构造函数.C#编译器如何从Lambda转换为Expression?

3> Parse功能的性能成本是多少?

public class Test
{
    public string Name {get;set;}

    public void Start()
    {
        Parse(()=>Name);
    }

    public string Parse<T>(Expression<Func<T>> exp)
    {
        var mexp = (System.Linq.Expressions.MemberExpression)expression.Body;
        return mexp == null ? "" : mexp.Member.Name;
    }
}
Run Code Online (Sandbox Code Playgroud)

c# linq expression

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

TryTake vs GetConsumingEnumerable

解决方案1和2有什么区别,_taskQ是BlockingCollection,我正在尝试实现Producer-Consumer方案.BlockingCollection使用默认的ConcurrentQueue进行内部存储.

//Solution 1
foreach (Action action in _taskQ.GetConsumingEnumerable())
{
    action(); // Perform task.
    Thread.Sleep(1000);
}
Run Code Online (Sandbox Code Playgroud)

没有项目时TryTake会阻止

//Solution 2
Action t;
while(_taskQ.TryTake(out t))
{
    t();
    Thread.Sleep(1000);
}
Run Code Online (Sandbox Code Playgroud)

c# blockingcollection

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

方法签名中的ICollection []

我有以下代码.为什么总是采用"采取(ICollection a)"方法?我认为它自己的对象应该是LinkedList或HashSet,所以应该调用另外两个take方法.

class Program
{
    static void Main(string[] args)
    {
        Program p = new Program();

        ICollection<String>[] ary = { new LinkedList<String>(), new HashSet<String>() };

        foreach (ICollection<String> a in ary)
        {
            p.take(a);
        }

        for (int i = 0; i < ary.Length; i++)
        {
            p.take(ary[i]);
        }
    }


    public void take(HashSet<String> a)
    { }

    public void take(LinkedList<String> a)
    {}

    public void take(ICollection<string> a)
    { }
}
Run Code Online (Sandbox Code Playgroud)

c# signature

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