I have, what probably is, a simple problem. I'm using interop to call an asynchronous function in CompactFramework. After I get result of the execution, I would like to raise an event, that will be caught by the form, and based on the results, I would render some data on the screen. The problem, however, is that when interop function returns a result, it returns it on a worker thread, and if I raise an event, it will stay on the worker thread and I can't render any data in the form, unless I use Invoke.
Can somebody suggest a way to merge worker thread onto the main thread? And raise an event from the main thread? I found a few examples that loop through the delegates subscribed to the event and use BeginInvoke to raise an event in main thread, however, they all use ISynchronizeInvoke, which is not available in Compact Framework.
My code is below:
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);
}
}