我有,可能是,一个简单的问题。我正在使用互操作来调用 CompactFramework 中的异步函数。获得执行结果后,我想引发一个事件,该事件将被表单捕获,并根据结果,我将在屏幕上呈现一些数据。然而,问题是,当互操作函数返回结果时,它会在工作线程上返回它,如果我引发一个事件,它将保留在工作线程上,并且我无法呈现表单中的任何数据,除非我使用调用。
有人可以建议一种将工作线程合并到主线程的方法吗?并从主线程引发一个事件?我发现了一些循环订阅事件的委托并使用 BeginInvoke 在主线程中引发事件的示例,但是,它们都使用 ISynchronizeInvoke,这在 Compact Framework 中不可用。
我的代码如下:
public delegate void CellTowerFoundEventHandler(CellTower towerInfo);
public class CellTowerProvider
{
public delegate void RILRESULTCALLBACK(uint dwCode, IntPtr hrCmdID, IntPtr lpData, uint cbData, uint dwParam);
public delegate void RILNOTIFYCALLBACK(uint dwCode, IntPtr lpData, uint cbData, uint dwParam);
private RILCELLTOWERINFO towerDetails;
private CellTower cellTowerInfo;
private IntPtr radioInterfaceLayerHandle;
public CellTowerProvider()
{
radioInterfaceLayerHandle = IntPtr.Zero;
IntPtr radioResponseHandle = IntPtr.Zero;
// Initialize the radio layer with a result callback parameter.
radioResponseHandle = RIL_Initialize(1, new RILRESULTCALLBACK(CellDataCallback), null, 0, 0, out radioInterfaceLayerHandle);
// The initialize API call will always return 0 if initialization is successful.
if (radioResponseHandle != IntPtr.Zero)
{
return;
}
// Query for the current tower data.
radioResponseHandle = RIL_GetCellTowerInfo(radioInterfaceLayerHandle);
}
public void CellDataCallback(uint dwCode, IntPtr hrCmdID, IntPtr lpData, uint cbData, uint dwParam)
{
// Refresh the current tower details
towerDetails = new RILCELLTOWERINFO();
// Copy result returned from RIL into structure
Marshal.PtrToStructure(lpData, towerDetails);
cellTowerInfo = new CellTower()
{
TowerId = Convert.ToInt32(towerDetails.dwCellID),
LocationAreaCode = Convert.ToInt32(towerDetails.dwLocationAreaCode),
MobileCountryCode = Convert.ToInt32(towerDetails.dwMobileCountryCode),
MobileNetworkCode = Convert.ToInt32(towerDetails.dwMobileNetworkCode),
};
if (CellTowerFound != null)
CellTowerFound(cellTowerInfo);
// Release the RIL handle
RIL_Deinitialize(radioInterfaceLayerHandle);
}
}