hilowcard游戏的代码片段 [英] code snippets for hilowcard game

查看:74
本文介绍了hilowcard游戏的代码片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我开始遇到问题的地方...

This is where I start having my problems...

public Card()
{
  int width = DefaultWidth;
  int height = DefaultHeight;
  mode = (int)CardMode.RankCollated;
  NativeMethods.cdtInit(ref width, ref height);
}
#region Implementation: IDisposable & Finaliser
/// <summary>
/// Instance finaliser for the class for unmanaged resources.
/// </summary>
~Card ()
{
  Dispose(false);
}
/// <summary>
/// Called by caller's when the instance is finished with.
/// </summary>
public void Dispose()
{
  Dispose(true);
  GC.SuppressFinalize(this);
}
/// <summary>
/// Internal dispose function which release managed and unmanaged resources.
/// </summary>
/// <param name="disposing">true iff managed and unmanaged are being disposed of else unmanaged only.</param>
private void Dispose( bool disposing )
{
  // Check to see if Dispose has already been called.
  if(!this.disposed)
  {
    // Call the appropriate methods to clean up
    // unmanaged resources here.
    // If disposing is false,  only the following code is executed.
    if( this.graphicsSurface != null && this.graphicsDC != IntPtr.Zero)
    {
      this.graphicsSurface.ReleaseHdc( this.graphicsDC );
      this.graphicsDC = IntPtr.Zero;
    }
    NativeMethods.cdtTerm();
    // If disposing equals true, dispose all managed
    // and unmanaged resources.
    if(disposing)
    {
      // Dispose managed resources.
      if( graphicsSurface != null )
      {
        graphicsSurface.Dispose();
      }
    }
  }
  this.disposed = true;
}
#endregion

推荐答案

您没有解释问题,但不应从析构函数中进行处理.破坏的时间不确定(由于GC).
就您而言,根本不要使用析构函数. (在.NET中,编写析构函数的方法要少得多.)

应该从窗体(或Windows)Dispose调用链中的所有子项来构建处置.

[在新版本中编辑]

请解释您的问题以获得更多建议.

感谢James的问题解答,我可以看到此建议与他的当前问题无关.这个建议仍然有效.

我将发布单独的答案以帮助解决"Cards.DLL"的问题,请参阅.

[在新版本中结束编辑]
You did not explain the problem, but you should not dispose from destructor. The moment of the destruction is uncertain (because of GC).
In your case, don''t use destructor at all. (In .NET writing destructors is much less typical.)

The disposal should be built from the Form (or Windows) Dispose calling all children in chain.



Please explain your problem to get further advice.

Thanks to Question clarification by James I can see this advice is unrelated to his immediate problem. Still this advice is valid.

I''ll post a separate Answer to help sorting out of the problem with "Cards.DLL", please see.

[END EDITED IN NEW VERSION]


感谢詹姆斯对问题的进一步解释,另一个问题是缺少"Cards.DLL".

詹姆斯,首先,您从来没有给我们提供过尝试加载"Cards.DLL"的代码.犯错很容易.

据我所知,"Cards.dll"是一个本机库,而不是.NET程序集.
由于您的Question带有标记"C#",因此您想在程序集中使用该库.

可以通过System.Runtime.InteropServices http://msdn.microsoft.com/进行加载和访问zh-cn/library/system.runtime.interopservices.aspx [
如果拥有所有这些,则需要应用属性System.Runtime.InteropServices.DllImportAttribute(
http: //msdn.microsoft.com/zh-CN/library/system.runtime.interopservices.dllimportattribute.aspx [ http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshalasattribute.aspx [构造函数 [http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.unmanagedtype .aspx [ ^ ]).

在更复杂的情况下,可能需要创建自定义编组.

为了获得进一步的建议,需要对功能进行准确的描述.可以很容易地导出它们(更典型的情况),非常困难(不是非常典型的情况)或介于两者之间(相当典型的情况).

我对使用Dispose的第一个建议与"Cards.DLL"的问题无关,但仍然有效-您应该更改处理方案.这就是为什么我要发布第二个答案.
Thanks to additional explanation of the problem by James, another problem is missing "Cards.DLL".

James, first of all, you never gave us a piece of code where "Cards.DLL" is attempted to load. It''s very easy to make a mistake in it.

As far as I can see, "Cards.dll" is a native library, not .NET assembly.
As your Question has a tag "C#", you want to use this library in your assembly.

It can be loaded and accessed via System.Runtime.InteropServices http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.aspx[^].

You need to know entry points of every function you want to export from "Cards.DLL", exact name (non-mangled, as it appears in DLL dump) complete profile of each one (translated into C#). You also need to have a correct path to the DLL, relative to your starting assembly location (or absolute).

If you have all that, you need to apply the attribute System.Runtime.InteropServices.DllImportAttribute (http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx[^]) to each function to be imported.

As the function may contain parameter or return types different from .NET types, each such parameter may need application of the attribute
System.Runtime.InteropServices.MarshalAsAttribute (http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshalasattribute.aspx[^]).

In some cases, default marshalling works (such as marshalling between System.String and null-terminated strings, either Unicode or ANSI), so such parameters require no attribute. In other cases, System.Runtime.InteropServices.MarshalAsAttribute is needed (see its constructor [^]) and the type System.Runtime.InteropServices.UnmanagedType, http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.unmanagedtype.aspx[^]).

In more complex cases, creation of custom Marshalling may be required.

For further advice, exact profiles of the functions is needed. It can be very easy to export them (more typical case), very difficult (not very typical case) or somewhere in between (fairly typical case).

My first advice about the use of Dispose is unrelated to the problem of "Cards.DLL", but still valid -- you should change the schema of disposal. That''s why I''m posting a second answer.


这篇关于hilowcard游戏的代码片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆