run oncomplete event in async

539 views Asked by At

Is there someway of letting an oncomplete event run in Async. The oncomplete event is called from an external dll. The external dll uses Async and I am creating a wrapper for it that hides multithreading.

I Have tried using AutoResetEvents but these get stuck on the WaitOne as the oncomplete event which resets the AutoResetEvent only runs after the function is completed.

From what I can pickup from the external Dll, the object tree needs to be synced to the main form in order to function. This forces everything on a single thread. GetTag is called as GetTag(this).

protected EventWaitHandle SyncEvent;
public string GetTag(object syncObject)
    {
        string lResult = "";
        SyncEvent = new AutoResetEvent(false);
        XiServer lOPCServer = new XiServer(OPCServer.ServerID, OPCServer.ApplicationName, syncObject); //create local instance of server synced to mainform
        lOPCServer.Initiate(OnInitiated2, null); //Connect server
        SyncEvent.WaitOne(); //Wait here for for connect to finish, Freezes here

        if  (ServerTree == null)
            ServerTree = new ObjectTree(lOPCServer, lOPCServer.ServerInfo.ServerName);
        DialogResult dlr = ServerTree.ShowDialog();
        if (dlr == DialogResult.OK)
        {
            lResult =  ServerTree.SelectedObject.InstanceId.FullyQualifiedId;
        }
        lOPCServer.Dispose();
        return lResult;
    }
private void OnInitiated2(Exception aError, object asyncState)
    {
        if (aError != null)
        {
            GlobalException = aError;
        }
        SyncEvent.Set();            
    }

So in short, is there someway I can let the code wait till after the Initiate was finsihed? I dont have access to the initiate code.

1

There are 1 answers

3
blackwolfsa On BEST ANSWER

Okay I found away around this. I keep checking a field that should be set last on on the initiate function. On the server object I am using the isConnected field

while ((lOPCServer.IsConnected == false) && (lTimeout < 60000)) //1 min timeout
{
    Thread.Sleep(10);
    lTimeout += 10;
}

if (lTimeout >= 60000)
{
    return ""; //Server initaite timed out
}