Tag: 回调

如何与许多APM BeginXXX呼叫共享AsyncCallback?

我想用许多APM风格的异步方法重用我的代码。 每种方法都有一个BeginXXX和一个EndXXX签名对。 我想在每个函数中重用相同的回调。 我一直使用下面的匿名方法,但我不知道如何将其提取为可重用的forms。 我知道这应该很简单,但我无法弄清楚如何使用委托来实现这一目标。 (这就是我自学成才) var result = tableSymmetricKeys.BeginExecuteQuerySegmented(query, token, opt, ctx, (o) => { var response = (o.AsyncState as CloudTable).EndExecuteQuerySegmented(o); token = response.ContinuationToken; int recordsRetrieved = response.Results.Count; totalEntitiesRetrieved += recordsRetrieved; Console.WriteLine(“Records retrieved in this attempt = ” + recordsRetrieved + ” | Total records retrieved = ” + totalEntitiesRetrieved); evt.Set(); }, tableSymmetricKeys); 如何使用(o) […]

使用对EnumFontFamiliesEx的DLL调用在C#中枚举活动字体让我知道了

我的最终目标是在Windows计算机上列出所有活动字体。 例如,如果您启动写字板或Word,您将看到它们全部。 其中一些字体来自windows \ fonts文件夹,但不是全部。 有些是使用gdi32.dll调用AddFontResource(…)动态注册的。 现在,你会认为C#支持检索整个列表,如果有,请告诉我! 但是,在花了好几个小时后,常规的C#调用获取安装的系统字体只检索实际上活动的所有字体中的一些。 例如,InstalledFontCollection.Families没有找到AddFontResource添加的任何字体(它找到了wordpad中列出的大约25%的字体!) 虽然我想在C#中执行此操作,但似乎我必须使用更多gdi32.dll调用。 这样就开始了我的EnumFontFamiliesEx问题。 我已经设法让以下代码工作到我正在获得一堆字体回调。 但是,结构ENUMLOGFONTEX似乎已损坏,例如lpelfe.elfFullName通常包含几个方形字符,而不是真实姓名。 也许我在编组或结构定义中犯了一个错误,不太确定。 我尝试了很多迭代和无休止的谷歌搜索,但无济于事。 任何帮助将非常感谢。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Windows.Media; using System.Runtime.InteropServices; using Microsoft.Win32; namespace FontRetriever { public partial class TestForm : Form { [DllImport(“gdi32.dll”)] static extern int EnumFontFamiliesEx(IntPtr hdc, [In] […]

C ++回调到C#函数期间的NullReferenceException

开发商! 我有一个非常奇怪的问题。 我的项目有用C ++编写的DLL和用C#编写的GUI。 我已经实现了一些互操作性的回调。 我计划在某些情况下C ++ DLL会调用C#代码。 它有效…但不长,我不明白为什么。 在C#部分的注释中标出了问题 这里是简化示例的完整代码: C ++ DLL: #include #define WIN32_LEAN_AND_MEAN #include BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } extern “C” { typedef void (*WriteSymbolCallback) (char Symbol); WriteSymbolCallback Test; […]

两个服务之间的WCF回调

我想在两个服务之间建立连接。 Service1将向service2发送事件通知。 我想在这两个服务之间使用回调。 service1将事件发送到service2,service2将事件传递给客户端。 是否可以在两个服务之间使用回调? 我已经查看了wcf回调的示例,但是无法在服务与服务通信之间找到任何内容。 任何帮助都是适用的。 请参阅下面的1个服务的代码。 我的问题:其他服务的代码如何与回调中的代码相似? 或者在这种情况下,其中一项服务就像客户一样? 那怎么样? Service1代码:(接口类) public interface IMyEvents { [OperationContract(IsOneWay = true)] void Event1(); } [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IMyEvents))] public interface SampleInterface { [OperationContract] void DoSomethingAndFireEvent(); [OperationContract] void SubscribeEvent(); } 这是服务类 public class SampleHandlerSimulator : SampleInterface { static Action m_Event1 = delegate { Console.WriteLine(“Catch”);}; public void […]

从WCF服务调用基于任务的异步单向回调方法

我有一个WCF服务,它使用回调合同来通知客户端,类似于此 public interface IClientCallback { [OperationContract(IsOneWay = true)] void NotifySomething(); } 和调用它的服务代码类似于此 void NotifySomething() { try { this.callback.NotifySomething(); } catch (Exception ex) { // Log the exception and eat it } } 请注意,根据设计,回调通知是可选的 ,即很好,但不是必需的。 这就是为什么它被标记为OneWay并且实现吃exception。 由于一些误解,我们认为这对于拥有一个非阻塞的火灾和忘记方法就足够了。 但当然这不是真的,所以在某些情况下它会阻塞一段时间,这会导致问题,因为它是从内部线程同步块调用的。 因此,我们决定通过更改定义使其异步,如下所示 public interface IClientCallback { [OperationContract(IsOneWay = true)] Task NotifySomething(); } 我对客户端实现没有问题,我的问题是如何从服务中调用它。 这是我正在考虑的事情 async void NotifySomething() { try […]

使用C#.NET在ASP.NET中强制使用Clickatell CallBack

我正在使用C#.NET在ASP.NET中构建一个Web应用程序,它使用clickatell发送一个SMS。 我试图通过创建一个名为ClickatellCallBack.aspx的CallBack页面来尝试从clickatell接收状态,这是以下代码的后台: public partial class ClickatellCallBack:System.Web.UI.Page { protected void Page_Load(object sender,EventArgs e) { //从QueryString中检索值。 string apiID = Request.QueryString [“api_id”]; string from = Request.QueryString [“from”]; string to = Request.QueryString [“to”]; string timestamp = Request.QueryString [“timestamp”]; string apiMsgId = Request.QueryString [“apiMsgId”]; string cliMsgId = Request.QueryString [“cliMsgId”]; string status = Request.QueryString [“status”]; string charge = Request.QueryString [“charge”]; // […]

自我更新MemoryCache

我已经实现了一个缓存,当它们到期时更新包含值。 我的代码是这样的: class MyCache { private static readonly MemoryCache Cache = MemoryCache.Default; private CacheItemPolicy _errorpolicy; private CacheItemPolicy _warnPolicy; private CacheEntryRemovedCallback _warnCacheEntryRemovedCallback; private CacheEntryRemovedCallback _errorCacheEntryRemovedCallback; public void AddErrors(string key, object errors) { _errorCacheEntryRemovedCallback = ErrorItemRemovedCallback; _errorpolicy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(7.00), RemovedCallback = _errorCacheEntryRemovedCallback }; Cache.Set(key, errors, _errorpolicy); } public void AddWarnings(string key, object […]

.NET DLL需要接收一个Clarion回调程序,然后通过三个整数?

我正在为Clarion(Clarion是C ++编程语言)程序编写C#.NET DLL。 我很好地调用了C#.NET DLL,一切都很好。 但是,我需要C#.NET DLL接收Clarion过程以进行回调,然后能够调用该过程传递三个int参数。 Clarion Procedure看起来像这样(Clarion long是C#int): MyCallBack procedure(long p1, long p2, long p3) … Data … code … Code … 如何将abvoe过程传递给C#.NET DLL以及C#.NET DLL如何调用该过程传递三个int参数? 提前致谢。

C#回调接收UTF8字符串

我有一个C#函数,一个回调,从用C ++编写的Win32 DLL调用。 调用者给了我一个UTF8字符串,但是我无法正确接收它,所有匈牙利语特殊字符都出错了。 [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate int func_writeLog(string s); 当我将参数类型更改为IntPtr并编写代码时,它会正确写入。 但我发现这是一个非常缓慢的解决方案: byte[] bb = new byte[1000]; int i = 0; while (true) { byte b = Marshal.ReadByte(pstr, i); bb[i] = b; if (b == 0) break; i++; } System.Text.UTF8Encoding encodin = new System.Text.UTF8Encoding(); var sd = encodin.GetString(bb, 0, i); 我试着给string参数写一些属性,比如: [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate int […]

C ++ dll的C#包装器; “运行时检查失败#0 – ESP的值未在函数调用中正确保存。”错误

这是C ++ DLL中的代码: extern “C” _declspec(dllexport) int testDelegate(int (*addFunction)(int, int), int a, int b) { int res = addFunction(a, b); return res; } 这是C#中的代码: public delegate int AddIntegersDelegate(int number1, int number2); public static int AddIntegers(int a, int b) { return a + b; } [DllImport(“tester.dll”, SetLastError = true)] public static extern int testDelegate(AddIntegersDelegate callBackProc, int […]