小编svi*_*ick的帖子

任务ContinuewWith等待返回错误

我有以下代码使用 System.Threading.Tasks

private async void UploadDocument(System.IO.FileInfo fileInfo)
{
    var someTask = await Task.Run<bool>(() =>
    {
        // open input stream
        using (System.IO.FileStream stream = new System.IO.FileStream(FileTextBox.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read))
        {
            using (StreamWithProgress uploadStreamWithProgress = new StreamWithProgress(stream))
            {
                uploadStreamWithProgress.ProgressChanged += uploadStreamWithProgress_ProgressChanged;

                // start service client
                FileTransferWCF.FileTransferServiceClient client = new FileTransferWCF.FileTransferServiceClient();
                //FileTransferClient.FileTransferServiceClient client = new FileTransferClient.FileTransferServiceClient();

                // upload file
                client.UploadFile(fileInfo.Name, fileInfo.Length, uploadStreamWithProgress);

                // close service client
                client.Close();
            }
        }

        return true;

    }).ContinueWith(r =>{
        if(r.Result) LogText("Done!!");
    });

}
Run Code Online (Sandbox Code Playgroud)

如果我离开这个并尝试编译我得到:

"无法将void分配给隐式类型的局部变量"** var someTask

所以我var someTask …

c# task-parallel-library async-await

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

为什么IEnumerable中没有赋值?

我解析一些数据

var result = xml.Descendants("record").Select(x => new F130Account
    {
        Account = x.Descendants("Account").First().Value,
    });
Run Code Online (Sandbox Code Playgroud)

然后我尝试更新一些

foreach (var item in result)
    item.Quantity = 1;
Run Code Online (Sandbox Code Playgroud)

在此之后,我result.Sum(a => a.Quantity)有零...为什么?

c# linq-to-xml

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

套接字服务器 - 从客户端向客户端发送消息

我有一个使用Adobe AIR设置的套接字服务器,我试图允许两个客户端使用服务器在彼此之间发送消息(这是一个基于Android的项目,因此服务器必须充当PC上的中间人).出于某种原因,我发送的消息只被发送回它来自的同一客户端,而不是发送给另一个客户端.我已经建立了一个系统来识别每条消息的来源,以及如何在另一方面处理它.

变量'connectionNum'int基本上表示客户端是否为数字0或1,并且发送到服务器和从服务器发送的数据在其前面有0或1.

理想情况下,我想要一种方法将数据一次定向到一个特定的客户端,而不是尝试在每个消息的开头用int发送它.

目前,只有连接第二个客户端的消息实际上是通过服务器发送的,第一个发送空白消息,不知道为什么.

sockets air client actionscript-3

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

在C#中你可以从main返回一个默认的int值吗?

我想从main返回一个默认的int值.

考虑以下:

using System;
class Class1
{
    static int Main(string[] args)
    {
        int intReturnCode = 1;
        int intRandNumber;

        Random myRandom = new Random();
        intRandNumber = myRandom.Next(0,2);
        if(intRandNumber ==1)
        {
            throw new Exception("ErrorError");      
        }
        return intReturnCode;
    }
}
Run Code Online (Sandbox Code Playgroud)

达到异常时,我无法设置返回码.

是否有可能在main中有一个默认的返回码?

澄清:我有一个程序正在抛出未处理的异常.我在try catch中有应用程序,但是一些错误(可能是内存不足,stackoverflow等)仍在冒泡并导致我的应用程序在生产中失败.

为了解决这个问题,我添加了代码来捕获未处理的异常.

这被添加到main:

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);
Run Code Online (Sandbox Code Playgroud)

现在我有一个未处理的异常发生时达到的方法.

public static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)   
{ 
    //here's how you get the exception  

    Exception exception = (Exception)e.ExceptionObject;                 

    //bail out in a tidy way and perform your logging
}
Run Code Online (Sandbox Code Playgroud)

问题是我不再在Main中,我想退出非零退出代码.

c#

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

提取字段值

如何使用Regex从下面的字符串中提取用户名值:

Acc sfjlsf56 1 0 User-Name = User_1,Group-Name = DCN_VPN_Support,

必须是User_1

Acc t3we89ab 1 0 User-Name = John,Group-Name = DCN_VPN_Support,

一定是约翰

Acc y5g769bo 1 0 User-Name =,Group-Name = DCN_VPN_Support,

必须为null

c# regex

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

如何创建委托数组并使用该数组?

我必须编写 Eve 类来编写以下代码:

class MainClass
  {    int sum = 5;
    public  void add (int x)
    {
      sum += x;
    }
    public static void write (int x)
    {
      Console.WriteLine("x = " + x);
    }
    public static void Main (string[] args)
    {
      Eve p = new Eve();
      MainClass m = new MainClass(); 
      p.registrate(m.add);
      p.registrate(write);
      p.registrate (delegate (int x) { 
                      System.Console.WriteLine(" I have {0} ", x);
                    }); 
      p.registrate (x => System.Console.WriteLine (" Square : {0} ", x * x));
      p.run(10);
      p.run(5);
                  Console.WriteLine(" Sum …
Run Code Online (Sandbox Code Playgroud)

c# delegates

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

Wikipedia API不返回特定文章的链接

我有一个查询,获取维基百科文章中的所有链接.问题是对于某些页面,它不返回任何链接.

这篇维基百科文章有很多链接.但是,我的查询:

http://en.wikipedia.org/w/api.php?titles=Maramures_County&prop=links&plnamespace=0&pllimit=250&format=xml&action=query

仅返回:

<?xml version="1.0"?>
    <api>
        <query>
            <normalized>
                <n to="Maramures County" from="Maramures_County"/>
            </normalized>
            <pages>
                <page title="Maramures County" ns="0" pageid="3625444">
                    <links>
                        <pl title="Maramure? County" ns="0"/>
                    </links>
            </page>
        </pages>
    </query>
</api>
Run Code Online (Sandbox Code Playgroud)

如果我对另一篇文章运行相同的查询,喜欢"月亮",我会得到很多结果.

http://en.wikipedia.org/w/api.php?titles=Moon&prop=links&plnamespace=0&pllimit=250&format=xml&action=query

返回

<?xml version="1.0"?>
    <api>
        <query-continue>
            <links plcontinue="19331|0|JSTOR"/>
        </query-continue>
        <query>
            <pages>
                <page title="Moon" ns="0" pageid="19331">
                <links>
                    <pl title="3753 Cruithne" ns="0"/>
                    <pl title="51st state" ns="0"/><pl title="Ablation" ns="0"/>

                    [ ... etc, etc  ...]
Run Code Online (Sandbox Code Playgroud)

我是在做API的错误还是这个错误?

wikipedia wikipedia-api

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

按随机数顺序排序列表

我有一个列表,我想每次排序成随机顺序.

我遇到过几种方法:

  1. list = list.OrderBy(x => Guid.NewGuid()).ToList();
    
    Run Code Online (Sandbox Code Playgroud)
  2. var rnd = new Random();
    myList = myList.OrderBy(x => rnd.Next()).ToList();
    
    Run Code Online (Sandbox Code Playgroud)
  3. static Random random = new Random();
    
    public static IEnumerable<T> RandomPermutation<T>(IEnumerable<T> sequence)
    {
        T[] retArray = sequence.ToArray();
    
        for (int i = 0; i < retArray.Length - 1; i += 1)
        {
            int swapIndex = random.Next(i + 1, retArray.Length);
            T temp = retArray[i];
            retArray[i] = retArray[swapIndex];
            retArray[swapIndex] = temp;
        }
    
        return retArray;
    }
    
    Run Code Online (Sandbox Code Playgroud)

显然,1到3之间的代码量有很大差异,但是有什么好处吗?

c# asp.net random

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

LINQ替换嵌套数组迭代和更新?

我需要一些语法糖的帮助.我有一个ThisClass [3]和ThatClass [3].

public class ThisClass
{
    public string Thing1;
    public string Thing2;
    public string Thing3;
    public string Thing4;
}

public class ThatClass
{
    public string Thing1;
    public string Thing2;
}
Run Code Online (Sandbox Code Playgroud)

ThatClass数组中的每个实例都是基于数组ThisClass的相同位置的实例创建的.所以ThatClass [0]的字段与ThisClass [0]具有相同的值,除了它只有2个字段而不是4个字段.

我现在想更新ThisClass数组中的每个实例,其中包含来自ThatClass数组中对象的匹配索引位置的字段.我可以做嵌套for循环,但我需要帮助思考LINQ选项.

 ThisClass[0].Thing1 = ThatClass[0].Thing1; 
 ThisClass[0].Thing2 =  ThatClass[0].Thing2;
Run Code Online (Sandbox Code Playgroud)

有效,但我相信可以做得更好.使用C#,.NET 4.5.

c# linq arrays

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

CSharpCodeProvider在System.Collections.Generic下没有看到Stack <T>类

我正在构建一个伪代码转换器和编译器.它通过执行几个字符串操作将伪代码转换为代码,然后它使用CSharpCodeProvider类来编译它,最后它尝试运行它.

经过几次测试后,如果翻译/输出代码如下:

using System;
using System.Collections.Generic;

public class TranslatedProgram
{
    public static void ReadLine(ref int destiny)
    {
        destiny = int.Parse(Console.ReadLine());
    }
    public static void InsertInStack(ref Stack<int> originalStack, int value)
    {
        Console.WriteLine("Inserting the value " + value + " to the stack.");
        originalStack.Push(value);
        foreach (int current in originalStack)
        {
            Console.WriteLine("|" + current);
        }
        Console.WriteLine("_______");
    }
    public static void Main()
    {
        int value = new int();
        Stack<int> data = new Stack<int>();
        ReadLine(ref value);
        InsertInStack(ref data, value);
    }
}
Run Code Online (Sandbox Code Playgroud)

当应用程序将此代码发送到CSharpCodeProvider时,它不会编译.在compilerResults中,我收到以下错误:"无法找到类型或命名空间名称'Stack'(您是否缺少using指令或程序集引用?)" (CS0246) …

.net c# compiler-errors pseudocode csharpcodeprovider

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