小编Maf*_*fii的帖子

正确使用CancellationToken

这是我的情况:

    private CancellationTokenSource cancellationTokenSource;
    private CancellationToken cancellationToken;

    public IoTHub()
    {
        cancellationTokenSource = new CancellationTokenSource();
        cancellationToken = cancellationTokenSource.Token;

        receive();
    }

    private void receive()
    {
        eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, iotHubD2cEndpoint);
        var d2cPartitions = eventHubClient.GetRuntimeInformation().PartitionIds;

        foreach (string partition in d2cPartitions)
        {
            ReceiveMessagesFromDeviceAsync(partition, cancellationToken);
        }
    }

    private async Task ReceiveMessagesFromDeviceAsync(CancellationToken ct)
    {
        var eventHubReceiver = eventHubClient.GetDefaultConsumerGroup().CreateReceiver(partition, DateTime.UtcNow);

        while (true)
        {
            if(ct.IsCancellationRequested)
            {
                break;
            }

            EventData eventData = await eventHubReceiver.ReceiveAsync();
            if (eventData == null) continue;

            string data = Encoding.UTF8.GetString(eventData.GetBytes());

            // Javascript function with Websocket
            Clients.All.setMessage(data);
        } …
Run Code Online (Sandbox Code Playgroud)

c# cancellationtokensource cancellation-token

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

从foreach循环获取当前索引

使用C#和Silverlight

如何获取列表中当前项的索引?

码:

IEnumerable list = DataGridDetail.ItemsSource as IEnumerable;
List<string> lstFile = new List<string>();


foreach (var row in list)
{
  bool IsChecked = (bool)((CheckBox)DataGridDetail.Columns[0].GetCellContent(row)).IsChecked;
  if (IsChecked)
  {

    // Here I want to get the index or current row from the list                   

  }
}
Run Code Online (Sandbox Code Playgroud)

如何实现这一目标

c# silverlight foreach for-loop

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

如何阻止在textarea中输入回车?

在按键事件期间,如何使用asp.net mvc阻止在textarea中输入回车符,新行,单引号和双引号?

javascript asp.net-mvc textarea keypress

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

类库(.Net Core)"在2017年的Visual Studio上不可用

在Visual Studio 2015中添加新项目时,您将获得"类库(.Net Core)"

VS 2017中似乎不再提供此选项.

我如何获得"类库(.Net标准版)"?

visual-studio-2017

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

为什么IL将此值设置两次?

当我输入这段代码时,我尝试使用Try Roslyn:

using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.CSharp;

public class C {

    public C()
    {
        x = 4;
    }

    public int x { get; } = 5;
}
Run Code Online (Sandbox Code Playgroud)

它给了我这个代码:

using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
[assembly: AssemblyVersion("0.0.0.0")]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[module: UnverifiableCode]
public class C
{
    [DebuggerBrowsable(DebuggerBrowsableState.Never), CompilerGenerated]
    private readonly int <x>k__BackingField; …
Run Code Online (Sandbox Code Playgroud)

c# cil roslyn c#-6.0

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

正在转换为IList,然后调用ToList()时null比普通ToList()更好?

当我有一个IEnumerable<SomeClass>我不知道它的列表与否(就其而言List<T>)时,我必须列举IEnumerable以确保我不会枚举两次可枚举(例如循环两次,或类似的东西)那).

Resharper警告我可能多次枚举IEnumerable - 这很好,有时你会忘记它 - 并允许你选择quickfixes:

  • 枚举到数组
  • 枚举列表

当我选择Enumerate列表时,resharper引入了一个局部变量,并按以下方式分配IEnumerable(示例):

  var enumeratedItems = items as IList<SomeClass> ?? items.ToList();
Run Code Online (Sandbox Code Playgroud)

现在我的问题是:

为什么不简单items.ToList();呢?

尝试施展到IList<SomeClass>第一位的优势是什么?

.net c# resharper ienumerable list

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

LINQ选择新对象,在函数中设置对象的值

LINQ用来在这个对象中选择一个新twoWords对象List,并通过调用一个函数/方法来设置这些值.

请看看这是否有意义,我已经简化了很多.我真的想使用linq语句from select.

第一个函数GOGO将起作用,第二个函数失败(尽管它们不执行相同的任务)

// simple class containing two strings, and a function to set the values
public class twoWords
{
    public string word1 { get; set; }
    public string word2 { get; set; }

    public void setvalues(string words)
    {
        word1 = words.Substring(0,4);
        word2 = words.Substring(5,4);
    }
}

public class GOGO
{

    public void ofCourseThisWillWorks()
    {
        //this is just to show that the setvalues function is working
        twoWords twoWords = …
Run Code Online (Sandbox Code Playgroud)

c# linq list

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

将Word应用程序带到前面

我以编程方式创建Word文档,并询问用户是否要立即显示文档.

为了将文档放在前面,我最小化并最大化文档.文档出现在屏幕前面,但是以"不礼貌"的方式:你可以看到它被最小化和最大化.

知道如何"教育"它吗?

wordApp.Visible = true;//objApp is the word application defined in my application
//minimizing and maximizing bring the Word application to front
wordApp.WindowState=WdWindowState.wdWindowStateMinimize;
wordApp.WindowState=WdWindowState.wdWindowStateMaximize;
wordApp.Activate();
Run Code Online (Sandbox Code Playgroud)

c# office-interop

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

在 mongodb 中查询巨大列表的最快方法

我想从 mongodb 获取大量用户的详细信息。用户列表超过10万。由于mongodb不支持一次性非常大的数据查询。我想知道哪种是获取数据的最佳方式。

  1. 将列表分组并获取数据

groups_of_list 包含 10000 个用户 ID 的列表

for group in groups_of_list:
    curr_data = db.collection.find({'userId': {'$in': group}})
    data.append(curr_data)
Run Code Online (Sandbox Code Playgroud)
  1. 循环遍历集合
for doc in db.collection.find({}):
   if i['userId'] in set_of_userIds:
       data.append(doc)
Run Code Online (Sandbox Code Playgroud)

我想得到禁食法。

如果有更好的方法/途径,请指出。

python performance search mongodb pymongo

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

如何在Visual Studio 2015中添加EntityObject Generator?

我在VS2015中安装了最新的Entity Framework版本(EntityFramework.6.1.3).我在项目中添加了EF,并删除了来自DBContext的2 .tt文件(edmx_file_name.ttedmx_file_name.Context.tt).

现在我尝试通过在EF Designer中打开模型来添加EF 6.x代码生成模板,右键单击设计图面并选择Add Code Generation Item.

要在ObjectContext中添加代码生成,我需要安装EF 6.x EntityObject Generator.我从这里下载了它.

但我无法在VS 2015中安装它.

我该如何解决这个问题?

c# entity-framework windows-applications entity-framework-6

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