小编Ian*_*Ian的帖子

有太多事件吗?

public class Human
{
    public void Run(){}
    public void Jump(){}
    public void Eat(){}

    //Generalized approach
    public EventHandler<HumanActivityProgressChanged> ActivityProgressChanged;
    public EventHandler<HumanActivityCompleted> ActivityCompleted;

    //Per-method approach
    public EventHandler<HumanActivityProgressChanged> Running;
    public EventHandler<HumanActivityCompleted> Ran;

    public EventHandler<HumanActivityProgressChanged> Jumping;
    public EventHandler<HumanActivityCompleted> Jumped;

    public EventHandler<HumanActivityProgressChanged> Eating;
    public EventHandler<HumanActivityCompleted> Ate;
}
Run Code Online (Sandbox Code Playgroud)

我有不同的方法来实现基于事件的异步模式.这些方法触发ProgressChangedeventargs和Completedeventargs.它们都触发相同的事件标记(如上面的代码所示).

每个异步方法提供一个事件是否有意义?或者只为所有异步方法提供一般化事件?是否存在太多事件?

c# events

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

C#中的简单增强现实应用程序

我想开始一个个人项目,它将给我很多新的概念来学习和理解.

经过一番思考,我认为增强现实相关项目对我来说是最有益的,原因如下:

  • 我还没有尝试将程序与实时视频/相机输入连接起来
  • 我没有做过任何"图像处理"项目
  • 我没有做任何"图形渲染"

话虽如此,你可以假设我会完全吮吸AR.所以我在这里要求就最佳方式提出一些建议.

  1. 我正在考虑的st里程碑项目是使用廉价的网络摄像头并使用C#读取一些Data Matrix.

  2. 当与特定数据矩阵一起呈现时,nd里程碑项目将在Feed上呈现一些文本覆盖.

  3. rd里程碑项目实际上会呈现一些3D形状.

我全面搜索过,发现了一些不错的AR高级材料:

http://sites.google.com/site/augmentedrealitytestingsite/

http://soldeveloper.com/

http://www.mperfect.net/wpfAugReal/

所以我来这里问以下问题:

为了开始里程碑1,你能提供一些我可以学习的材料吗?我更喜欢在线资料.

谢谢!

编辑:删除了"主观"问题.

c# augmented-reality

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

我怎么知道"组装"是否确实改变了?

我在VS2005中创建了一个简单的"Hello World"应用程序.这是一个直接的控制台应用程序; 它只包含以下几行:

Console.WriteLine("Hello World");
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)

当我尝试重建相同的控制台应用程序而不执行任何更改(只需按下重建按钮)时,我得到一个微妙的不同的可执行文件.(我从第一个和第二个生成的可执行文件生成了一个SHA-1哈希,它是不同的!)

当没有代码更改时,为什么它会有所不同?实际改变了什么?我使用十六进制编辑器进行比较,只看到几个不同的字节.

我想我的最终问题是,我怎么知道"集会"是否确实改变了?(当然不看文件版本,文件大小等)

编辑

到目前为止,我们已经确定差异在于PE头(时间戳和一些调试数据).在我重新发明轮子之前,是否有一个忽略PE头的"装配比较"工具?

谢谢,伊恩

c# comparison file-comparison

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

DotNetZip ExtractProgress Bug?

ExtractProgressEventArgs.EntriesTotalExtractProgressEventArgs.EntriesExtracted始终为零.这是一个已知的错误?请参阅下面的代码:

public static void UnZip(string zipFile, string destination)
{
    using(ZipFile zip = ZipFile.Read(zipFile))
    {                
        zip.ExtractProgress += new EventHandler<ExtractProgressEventArgs>(zip_ExtractProgress);

        foreach (ZipEntry entry in zip)
        {
            entry.Extract(destination, ExtractExistingFileAction.OverwriteSilently);                    
        }
        Console.WriteLine("DONE");
    }
}

static void zip_ExtractProgress(object sender, ExtractProgressEventArgs e)
{
    if(e.EventType == ZipProgressEventType.Extracting_AfterExtractEntry)
        Console.WriteLine(String.Format("{0} : {1} / {2} = {3}%", e.CurrentEntry.FileName, e.EntriesTotal, e.EntriesExtracted, ((double)e.EntriesTotal / (double)e.EntriesExtracted) * 100.00));
}
Run Code Online (Sandbox Code Playgroud)

.net c# dotnetzip

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

WebClient.DownloadFileAsync - 一次下载一个文件

我使用下面的代码从TFS服务器下载多个附件:

foreach (Attachment a in wi.Attachments)
{    
    WebClient wc = new WebClient();
    wc.Credentials = (ICredentials)netCred;
    wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
    wc.DownloadFileAsync(a.Uri, "C:\\" + a.Name);
}
Run Code Online (Sandbox Code Playgroud)

我想使用DownloadFileAsync下载多个文件,但我希望逐个下载它们.

有人可能会问"你为什么不使用同步的DownloadFile方法?" 这是因为:

  1. 我想利用DownloadFileAsync提供的事件.
  2. 我不想创建Webclient的多个实例以避免泛滥服务器.

这是我想到的解决方案:

foreach (Attachment a in wi.Attachments)
{        
    WebClient wc = new WebClient();
    wc.Credentials = (ICredentials)netCred;
    wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
    wc.DownloadFileAsync(a.Uri, "C:\\" + a.Name);
    while (wc.IsBusy)
    {
        System.Threading.Thread.Sleep(1000);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,这种方法存在一些问题:

  1. Thread.Sleep()正在锁定我的表单.我仍然需要创建自己的Thread或使用BackgroundWorker.(我想尽可能避免这种情况)
  2. 下载所有文件后,将触发DownloadFileCompleted事件.我不知道这是否是使用System.Threading.Thread.Sleep(1000)的副作用;

有没有更好的方法使用WebClient.DownloadFileAsync一次下载一个文件?

谢谢!

c# asp.net multithreading webclient download

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

使用"new EventHandler <T>"并且不使用新的EventHandler <T>"的接线事件之间的区别?

这两者有什么区别?

object.ProgressChanged += new EventHandler<ProgressChangedEventArgs>(object_ProgressChanged)

object.ProgressChanged += object_ProgressChanged;

    void installableObject_InstallProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        EventHandler<ProgressChangedEventArgs> progress = ProgressChanged;
        if (installing != null)
            installing(this, e);
    }
Run Code Online (Sandbox Code Playgroud)

编辑:

如果没有差异,哪个是更好的选择?

谢谢!

c# events

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

为什么SafeHandle.DangerousGetHandle()"危险"?

这是我第一次使用它SafeHandle.

我需要调用这个需要UIntPtr的P/Invoke方法.

    [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
    public static extern int RegOpenKeyEx(
      UIntPtr hKey,
      string subKey,
      int ulOptions,
      int samDesired,
      out UIntPtr hkResult);
Run Code Online (Sandbox Code Playgroud)

这个UIntPtr将派生自.NET的RegistryKey类.我将使用上面的方法将RegistryKey类转换为IntPtr,以便我可以使用上面的P/Invoke:

        private static IntPtr GetRegistryKeyHandle(RegistryKey rKey)
        {
            //Get the type of the RegistryKey
            Type registryKeyType = typeof(RegistryKey);

            //Get the FieldInfo of the 'hkey' member of RegistryKey
            System.Reflection.FieldInfo fieldInfo =
                registryKeyType.GetField("hkey", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

            //Get the handle held by hkey
            if (fieldInfo != null)
            {
                SafeHandle handle = (SafeHandle)fieldInfo.GetValue(rKey);

                //Get the unsafe handle
                IntPtr dangerousHandle = handle.DangerousGetHandle(); …
Run Code Online (Sandbox Code Playgroud)

c# pinvoke handles .net-3.5

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

程序集加载版本不匹配:为什么要加载?

我有两个程序集:HelloWorld.exe 和 Hello.dll。exe 是主程序集,主程序集正在使用 dll。

我编译了 HelloWorld.exe (1.0.0) 和 Hello.dll (1.0.0)。我将程序集放在不同的文件夹中。

然后我将 Hello.dll 的版本更改为 2.0.0,并继续用 2.0.0 版本覆盖 Hello.dll 1.0.0。然后我启动 HelloWorld.exe 并且它运行良好。

我预计它会立即崩溃并烧毁,因为我编译 EXE 时引用的 Hello.dll 是 1.0.0。现在,1.0.0 DLL 已被 2.0.0 取代,但它仍然有效!

根据MSDN

默认情况下,程序集将仅使用与构建和测试时完全相同的程序集(名称和版本号)中的类型。也就是说,如果您的程序集使用另一个程序集 1.0.0.2 版中的类型,则它(默认情况下)不会使用另一个程序集 1.0.0.4 版中的相同类型。使用名称和版本来标识引用的程序集有助于避免升级到一个应用程序破坏其他应用程序的“DLL 地狱”问题。

问题:

  1. 为什么有效?
  2. 如何使它不起作用?
  3. 额外问题:在构建过程中会发生什么?外部依赖的版本不是硬编码到主要依赖中吗?

请注意,Hello.dll 不是强命名的。

这是 HelloWorld.exe 的清单:

// Metadata version: v2.0.50727
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 2:0:0:0
}
.assembly extern Hello
{
  .ver 2:0:0:0
} …
Run Code Online (Sandbox Code Playgroud)

.net dll assemblies

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

覆盖虚拟方法时,最好是调用基本方法吗?

我注意到,当使用Visual Studio在C#中覆盖虚拟方法时,IDE会自动添加base.Method()调用.另一方面,当重写抽象方法时,IDE会自动添加NotImplementedException().

为什么VS在覆盖虚拟方法时会自动添加base.Method()调用?调用基本方法是最佳做法吗?

c# polymorphism visual-studio-2010

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

如何将IntPtr转换为UIntPtr?

我尝试像这样投射它:

UIntPtr x = (UIntPtr)intPtr;
Run Code Online (Sandbox Code Playgroud)

...但编译器对它不满意并返回编译错误.

我需要进行转换,因为RegOpenKeyEx的P/Invoke签名需要一个UIntPtr:

  [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
  public static extern int RegOpenKeyEx(
    UIntPtr hKey,
    string subKey,
    int ulOptions,
    int samDesired,
    out UIntPtr hkResult);
Run Code Online (Sandbox Code Playgroud)

为了获得句柄,我使用SafeHandle.DangerousHandle()返回一个IntPtr:

   /// <summary>
    /// Get a pointer to a registry key.
    /// </summary>
    /// <param name="registryKey">Registry key to obtain the pointer of.</param>
    /// <returns>Pointer to the given registry key.</returns>
    IntPtr _getRegistryKeyHandle(RegistryKey registryKey)
    {
        //Get the type of the RegistryKey
        Type registryKeyType = typeof(RegistryKey);

        //Get the FieldInfo of the 'hkey' member of …
Run Code Online (Sandbox Code Playgroud)

c#

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