小编Mob*_*erg的帖子

如何动态扩展内存映射文件

我已经使用C#来解决以下要求.. - 创建一个可以快速接收大量数据的应用程序 - 您必须能够分析收到的数据,同时传入更多数据. - 使用尽可能少的CPU和磁盘

我对算法的想法是......

SIZE = 10MB
Create a mmf with the size of SIZE
On data recived:
  if data can't fit mmf: increase mmf.size by SIZE
  write the data to mmf
Run Code Online (Sandbox Code Playgroud)

- >当使用前一个"房间/空间"时,光盘上的大小增加了10MB的块.

如何在C#中"通过SIZE增加mmf.size"?我已经找到了很多关于创建mmfs和视图的简单示例,但是我看到的唯一的地方(链接)实际上增加了mmfs区域的代码使用了无法编译的代码.任何帮助都将受到极大的关注.

编辑这会导致异常:

private void IncreaseFileSize()
{
    int theNewMax = this.currentMax + INCREMENT_SIZE;
    this.currentMax = theNewMax;

    this.mmf.Dispose();

    this.mmf = MemoryMappedFile.CreateFromFile(this.FileName, FileMode.Create, "MyMMF", theNewMax);
    this.view = mmf.CreateViewAccessor(0, theNewMax);            
}
Run Code Online (Sandbox Code Playgroud)

抛出此异常:进程无法访问文件'C:\ Users\moberg\Documents\data.bin',因为它正由另一个进程使用.

c# .net-4.0 memory-mapped-files

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

了解Thread/BeginInvoke?[初学者]

考虑一下代码:

class Work
{
    public void DoStuff(string s)
    {
        Console.WriteLine(s);
        // .. whatever
    }
}
class Master
{
    private readonly Work work = new Work();

    public void Execute()
    {
        string hello = "hello";

        // (1) is this an ugly hack ?
        var thread1 = new Thread(new ParameterizedThreadStart(o => this.work.DoStuff((string)o)));           
        thread1.Start(hello);
        thread1.Join();

        // (2) is this similar to the one above?
        new Action<string>(s => this.work.DoStuff(s)).BeginInvoke(hello, null, null);
    }
}
Run Code Online (Sandbox Code Playgroud)

(1)是一种在单独的线程中轻松启动某些工作的可接受方式吗?如果不是更好的选择将非常感激.

(2)做同样的事吗?我猜我要问的是是否启动了一个新线程,或者..

希望你能帮助初学者更好地理解:)

/莫伯格

c# multithreading delegates begininvoke

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

LINQ扩展SelectMany在3.5对4.0?

当我在这里看到达林斯的建议时......

IEnumerable<Process> processes = 
    new[] { "process1", "process2" } 
    .SelectMany(Process.GetProcessesByName);
Run Code Online (Sandbox Code Playgroud)

(process.getprocessesbyname())

..我有点好奇,我在VS2008中尝试使用.NET 3.5 - 它没有编译,除非我将其更改为..

IEnumerable<Process> res = 
  new string[] { "notepad", "firefox", "outlook" }
    .SelectMany(s => Process.GetProcessesByName(s));
Run Code Online (Sandbox Code Playgroud)

在我怀疑是我的问题之前阅读了一些Darins的答案,当我后来在.NET 4.0上使用.NET 4.0时 - 正如预期的那样 - 最初的建议很有效.

我的问题是:从3.5到4.0发生了什么使得这种(新语法)成为可能?它是扩展(hmm)扩展方法还是lambda语法的新规则?

c# linq

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

通用可变参数

从dahlbyk回答这个问题:将两个列表映射到C#中的字典中,Leppie写了这样的评论:

遗憾的是需要Zip方法.如果只有更多静态类型的语言支持通用的可变参数,Select会处理这个(比如Scheme中的map). - leppie

那是什么意思?(我不知道Scheme):)

c# linq

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

理解扩展ElementAt(索引)

考虑以下代码:

int size = 100 * 1000 * 1000;
var emu = Enumerable.Range(0, size);
var arr = Enumerable.Range(0, size).ToArray();
Run Code Online (Sandbox Code Playgroud)

当我调用emu.ElementAt(size-10)和arr.ElementAt(size-10)并测量arr更快的时间时(与IEnumerable 0.59s相比,数组为0.0002s).

据我了解,扩展方法ElementAt()具有签名

public static TSource ElementAt<TSource>(this IEnumerable<TSource> source, int index)
Run Code Online (Sandbox Code Playgroud)

并且因为'source'是IEnumerable,所执行的逻辑将是类似的 - 与我看到的直接访问数组的情况相反.

有人可以解释一下:)

c# linq extension-methods

5
推荐指数
2
解决办法
379
查看次数

异步模式 - 在从方法返回某些值之前等待事件

[免责声明 - 此代码已简化(很多)以易于阅读,我知道它不符合正常的代码标准]

我的问题可以在下面的代码中看到。基本上我有一个解析对象的调用者。我必须等到子组件完成(由事件发出信号),然后才能从基于子组件上某个值的值返回值。

问题是:这种情况的首选模式是什么(当然,最欢迎实际的解决方案)。

我已经围绕 TaskCompletionSource 等尝试了不同的东西,但恐怕我的理解远远落后于找到(最好)优雅的解决方案。希望你能帮忙。

public class AsyncEventTest
{
    // This is performed one a single (UI) thread. The exception to this is
    // a.B that might - at the calling time - get a asycronious update from the backend.
    // The update is syncronized into the calling context so Task.Wait etc. will effectivly
    // deadlock the flow.
    public static string CallMe(A a)
    {
        if (a.B.State != State.Ready)
        {
            // wait for a.B.State == State.Ready ... but …
Run Code Online (Sandbox Code Playgroud)

c# asynchronous async-await

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

C#/ Lambda:对象参数说明请求(noob)

这里有一个问题,这篇文章完全是新的:带有lambda表达式和匿名方法的ThreadPool.QueueUserWorkItem

具体如下:

ThreadPool.QueueUserWorkItem(
    o => test.DoWork(s1, s2)
    );
Run Code Online (Sandbox Code Playgroud)

有人可以解释'o'是什么?我可以看到(在VS2008中)它是一个对象参数,但我基本上不明白为什么以及如何.

c# lambda

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

C#中的链式构造函数 - 使用中间逻辑

从这个帖子:http://geekswithblogs.net/kaju/archive/2005/12/05/62266.aspx有人问(在评论中)这个问题:

有没有办法做这样的事情:

public FooBar(string fooBar)
{
string[] s = fooBar.split(new char[] { ':' });
this(s[0], s[1]);
}

public Foo(string foo, string bar)
{
...
} 
Run Code Online (Sandbox Code Playgroud)

好吧,我遇到了需要同样事情的情况.它有可能吗?提前致谢.

编辑

我的意思是这个

public Foo(string fooBar)
{
string[] s = fooBar.split(new char[] { ':' });
this(s[0], s[1]);
}

public Foo(string foo, string bar)
{
...
} 
Run Code Online (Sandbox Code Playgroud)

Foo是一个构造函数.

我的问题是我必须在调用其他构造函数之前做很多逻辑 - 包括一些IO的东西.

c# constructor

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

IEnumerable <IEnumerable <int >> - 没有重复的IEnumerable <int> s

我正在尝试找到解决此问题的方法:

给定一个IEnumerable <IEnumerable <int >>我需要一个返回输入的方法/算法,但是如果有几个具有相同元素的IEnmerable <int>,则每个巧合/组只返回一个.

恩.

    IEnumerable<IEnumerable<int>> seqs = new[]
    { 
        new[]{2,3,4}, // #0 
        new[]{1,2,4}, // #1 - equals #3
        new[]{3,1,4}, // #2
        new[]{4,1,2}  // #3 - equals #1
    };
Run Code Online (Sandbox Code Playgroud)

"foreq seq in seqs"..产生{#0,#1,#2}或{#0,#2,#3}

我可以去..

..一些聪明的IEqualityComparer

..一些聪明的LINQ组合我还没弄明白 - groupby,sequenceequal ..?

..一些seq-> HashSet的东西

什么不是.一切都会有所帮助

我将能够通过良好的编程解决它,但灵感总是受到赞赏.

c# linq ienumerable

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

C#LINQ/Lambda范围问题

我收到错误:

名为's'的局部变量不能在此范围内声明,因为它会给's'赋予不同的含义,'s'已在'子'范围内用于表示其他内容.

static void Main(string[] args)
{
    string s = "hello"; // Line 1
    var test = new[] { "abd", "def" }.Select(s => s.StartsWith("a")); // Line 2
}
Run Code Online (Sandbox Code Playgroud)

为什么?

我猜想第1行的's'会被'.Select(s => ..'第2行中的'decleration)阴影但是 - 据我所知 - 事实并非如此......

PS我不确定是否用正确的术语表示 - 如果有更好的单词/短语,请纠正我.

c# linq lambda

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

PLINQ聚合异常我不明白

我正在看Igor Ostrovsky的PLINQ PCD09演示文稿,想要试着看看我能从CULV笔记本电脑中得到什么.

在某一点上,我得到了一个奇怪的例外,我不确定它是什么意思.我已经浓缩了代码以获得更好的概述.它是导致异常的最后一个primes.Sum(),如果我使范围变小 - 8000 - 则不会抛出异常.有任何想法吗?

Func<int, bool> isprime = n => // ignore input checks for now
    {
        int sqr = Convert.ToInt32(Math.Ceiling(Math.Sqrt(n)));
        for (int i = 2; i < sqr; i++) if (n % i == 0) return false;
        return true;
    };

var numbers = Enumerable.Range(1, 8*1000*1000);
long counter = 0;
ParallelQuery<int> primes = numbers.AsParallel().Where(x => isprime(x));
counter = primes.Sum();
Run Code Online (Sandbox Code Playgroud)

例外(很长)

System.AggregateException未处理Message =发生一个或多个错误.Source = System.Core
StackTrace:System.Linq.Parallel.QueryTaskGroupState.QueryEnd(Boolean userInitiatedDispose)at System.Linq.Parallel.SpoolingTask.SpoolStopAndGo [TInputOutput,TIgnoreKey](QueryTaskGroupState groupState,PartitionedStream 2 partitions, SynchronousChannel1 [] channels,TaskScheduler taskScheduler)at …

c# exception plinq

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