小编Tor*_*lin的帖子

在当前线程上执行任务

是否可以强制任务在当前线程上同步执行?

也就是说,通过例如传递一些参数StartNew()来制作这段代码是否可能:

Task.Factory.StartNew(() => ThisShouldBeExecutedSynchronously());
Run Code Online (Sandbox Code Playgroud)

表现得像这样:

ThisShouldBeExecutedSynchronously();
Run Code Online (Sandbox Code Playgroud)

背景:

我有一个名为的界面IThreads:

public interface IThreads
{
    Task<TRet> StartNew<TRet>(Func<TRet> func);
}
Run Code Online (Sandbox Code Playgroud)

我想有两个这样的实现,一个使用线程的普通:

public class Threads : IThreads
{
    public Task<TRet> StartNew<TRet>(Func<TRet> func)
    {
        return Task.Factory.StartNew(func);
    }
}
Run Code Online (Sandbox Code Playgroud)

并且不使用线程(在某些测试场景中使用):

public class NoThreading : IThreads
{
    public Task<TRet> StartNew<TRet>(Func<TRet> func)
    {
        // What do I write here?
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以让NoThreading版本调用func(),但我想返回一个Task<TRet>我可以执行操作的实例,例如ContinueWith().

c# task-parallel-library

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

错误:文件路径太长

我试图在C#中使用各种文件函数File.GetLastWriteTime,复制命令放在路径上的文件大于Windows 7上的最大允许路径,即260.它给我一个长路径名错误.在MSDN支持上,他们要求使用\\?\前面的路径.我做了同样的但仍然得到了同样的错误,它似乎没有做任何改变.以下是我的代码.如果我正确使用它或者我需要添加任何东西,请告诉我:
这些我正在使用的所有lib作为代码还有其他东西:

以下是各自的代码:

filesToBeCopied = Directory.GetFiles(path,"*",SearchOption.AllDirectories);
for (int j = 0; j < filesToBeCopied.Length; j++)
{
    try
    {
        String filepath = @"\\?\" + filesToBeCopied[j];
        File.GetLastWriteTime(filepath);
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error Inside the single file iteration for the path:" +
            filesToBeCopied[j] + " . The exception is :" + ex.Message);
    }
}
Run Code Online (Sandbox Code Playgroud)

其中path是Windows机器上以驱动器号开头的文件夹的路径.例如:d:\abc\bcd\cd\cdc\dc\..........

c#

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

在逻辑或可视树中查找工具提示弹出窗口

假设我有一个ToolTip在XAML中指定的样式,如下所示:

<Button Content="Click me" ToolTip="Likes to be clicked">
    <Button.Resources>
        <Style TargetType="{x:Type ToolTip}" BasedOn="{StaticResource {x:Type ToolTip}}">
            <Setter Property="OverridesDefaultStyle" Value="true" />
            <Setter Property="HasDropShadow" Value="True" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToolTip}">
                        <StackPanel Background="Wheat" Height="200" Width="200">
                            <TextBlock x:Name="TxbTitle" FontSize="24" Text="ToolTip" Background="BurlyWood" />
                            <ContentPresenter />
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Button.Resources>
</Button>
Run Code Online (Sandbox Code Playgroud)

鉴于我的一个参考Button,而且ToolTip显示时,我怎么能找到PopupToolTip(后来找其视觉儿童,例如TxbTitle)?

更新:

基于pushpraj的回答,我能够抓住(完整)可视化树,它看起来像这样:

System.Windows.Controls.Primitives.PopupRoot
  System.Windows.Controls.Decorator
    System.Windows.Documents.NonLogicalAdornerDecorator
      System.Windows.Controls.ToolTip
        System.Windows.Controls.StackPanel
          System.Windows.Controls.TextBlock (TxbTitle)
          System.Windows.Controls.ContentPresenter
            System.Windows.Controls.TextBlock
      System.Windows.Documents.AdornerLayer
Run Code Online (Sandbox Code Playgroud)

在这里我可以找到TxbTitle TextBlock.

(像这样的逻辑树:) …

c# wpf

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

仅在三个表上加入一行

(我的问题很多时候都涉及到两个表,并且已经在这里,这里这里得到了解答.但是我无法弄清楚如何对涉及的三个表做同样的事情.)

我有三个表,A,B和C,其中A有很多B,B有很多C.我想加入这些表,每个A选择零或一行,其中一个应该基于C中的条件.

例如,假设:

SELECT
     a.aId
    ,b.bId
    ,c.cId
FROM
    a
    INNER JOIN b ON b.aId=a.aId
    INNER JOIN c ON c.bId=b.bId
WHERE
    c.someColumn='foo'
Run Code Online (Sandbox Code Playgroud)

...产生以下结果:

aId   bId   cId
===   ===   ===
1     11    101
1     12    102
1     12    103
2     21    201
2     21    203
2     22    202
Run Code Online (Sandbox Code Playgroud)

...那么我想,例如,检索两个不同的A行,最高的那些cId.

aId   bId   cId
===   ===   ===
1     12    103
2     21    203
Run Code Online (Sandbox Code Playgroud)

sql sql-server

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

当依赖属性具有 RelativeSource 绑定时,GetTemplateChild 返回 null

我创建了一个自定义控件,MyTextBoxTextBox. 它有一个与之关联的样式,其中包含一个命名控件:

<Style x:Key="{x:Type MyTextBox}" TargetType="{x:Type MyTextBox}">
    <!-- ... -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type MyTextBox}">
                <!-- ... -->
                    <SomeControl x:Name="PART_SomeControl" />
                <!-- ... -->
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

MyTextBox有一个依赖属性,当设置时,将其值传播到SomeControl

public class MyTextBox : TextBox
{
    // ...

    public static new readonly DependencyProperty MyParameterProperty =
        DependencyProperty.Register(
            "MyParameter",
            typeof(object),
            typeof(MyTextBox),
            new PropertyMetadata(default(object), MyParameterChanged));

    private static void MyParameterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var me = (MyTextBox)d;
        var someControl = (SomeControl)me.GetTemplateChild("PART_SomeControl");
        someControl.SetValue(SomeControl.MyParameterProperty, e.NewValue);
    }
}
Run Code Online (Sandbox Code Playgroud)

这在进行简单绑定时工作正常,如下所示: …

c# wpf xaml dependency-properties relativesource

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

可视树中的ComboBox弹出窗口在哪里?

在可视化树中我可以找到ComboBox弹出窗口(带有ComboBoxItems的列表)?

我已经以编程方式打开了一个ComboBox,当在调试器中的WPF Tree Visualizer中观察它时,我看到以下内容:

: ComboBox
  templateRoot : Grid
    PART_Popup : Popup
    toggleButton : ToggleButton
      templateRoot : Border
        splitBorder : Border
          Arrow : Path
    contentPresenter : ContentPresenter
      : TextBlock
Run Code Online (Sandbox Code Playgroud)

我希望看到ScrollViewer带有某种物品主机(StackPanel?),也许是PART_Popup所在的地方,但什么都没有.

那它在哪里?

c# wpf combobox visual-tree

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

Cassandra DataStax驱动慢?

我刚刚开始尝试使用Cassandra,我正在使用C#和DataStax驱动程序(v 3.0.8).我想做一些性能测试,看看Cassandra处理时间序列数据的速度有多快.

结果是因为它需要一个永恒的时间SELECT.所以我想我做错了什么.

我在本地计算机上安装了Cassandra,并创建了一个表:

CREATE KEYSPACE dm WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}  AND durable_writes = true;

CREATE TABLE dm.daily_data_by_day (
    symbol text,
    value_type int,
    as_of_day date,
    revision_timestamp_utc timestamp,
    value decimal,
    PRIMARY KEY ((symbol, value_type), as_of_day, revision_timestamp_utc)
) WITH CLUSTERING ORDER BY (as_of_day ASC, revision_timestamp_utc ASC)
    AND bloom_filter_fp_chance = 0.01
    AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
    AND comment = ''
    AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
    AND compression = {'chunk_length_in_kb': '64', …
Run Code Online (Sandbox Code Playgroud)

c# cassandra datastax

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