A
Aidal
Hi NG.
I'm creating a little app. that has to use a 3rd party API.
The functions in this API has no return value but trigger events like OK
or NOT OK with event args.
Where I need to use this API I must have a synchrone modul so I need to
force a synchrone look to this asynchrone model of the API.
I was thinking this would call for some waiting for events and since I
don't wanna make a loop eat all the cpu power I was thinking of using a
few threads to accomplish this.
It's giving me some trouble though I'm aware it's a pretty common and
simple scenario.
I was hoping maybe somone here could lend me a hand.
What I need is as said, to make some code that uses the API but my code
has to be synchrone while the API is not.
So I can do somthing like:
--------------------------
public MyClass
{
private bool InProcess;
private bool ItWentWell;
public bool Function1()
{
InProcess = true;
APIClass apic = new APIClass();
apic.FunctionAPI1_Success += EventHandler(FunctionOK);
apic.FunctionAPI1_Failed += EventHandler(FunctionERROR);
apic.FunctionAPI1();
...
Wait for InProcess to change from TRUE to FALSE.
Once this happens, return ItWentWell.
}
public void FunctionOK()
{
Inprocess = false;
ItWentWell = true;
}
public void FunctionERROR()
{
InProcess = false;
ItWentWell = false;
}
}
//--- somwhere like the Main of a console app. ---
MyClass mc = new MyClass();
if(mc.Function1())
{
Consile.WriteLine("API call went well.");
}
else
{
Consile.WriteLine("API call went bad.")
}
--------------------------
I'm aware that the above doesn't work, it's only to give a picture of
what I'm trying to accomplish. Somwhere around the part with the waiting
for the event to occur should most likely be using a new thread not to
eat all the cpu power, but I can't get the things I've tried to work.
Any help would be much appreciated, thanks![Smile :) :)](/styles/default/custom/smilies/smile.gif)
/Aidal
I'm creating a little app. that has to use a 3rd party API.
The functions in this API has no return value but trigger events like OK
or NOT OK with event args.
Where I need to use this API I must have a synchrone modul so I need to
force a synchrone look to this asynchrone model of the API.
I was thinking this would call for some waiting for events and since I
don't wanna make a loop eat all the cpu power I was thinking of using a
few threads to accomplish this.
It's giving me some trouble though I'm aware it's a pretty common and
simple scenario.
I was hoping maybe somone here could lend me a hand.
What I need is as said, to make some code that uses the API but my code
has to be synchrone while the API is not.
So I can do somthing like:
--------------------------
public MyClass
{
private bool InProcess;
private bool ItWentWell;
public bool Function1()
{
InProcess = true;
APIClass apic = new APIClass();
apic.FunctionAPI1_Success += EventHandler(FunctionOK);
apic.FunctionAPI1_Failed += EventHandler(FunctionERROR);
apic.FunctionAPI1();
...
Wait for InProcess to change from TRUE to FALSE.
Once this happens, return ItWentWell.
}
public void FunctionOK()
{
Inprocess = false;
ItWentWell = true;
}
public void FunctionERROR()
{
InProcess = false;
ItWentWell = false;
}
}
//--- somwhere like the Main of a console app. ---
MyClass mc = new MyClass();
if(mc.Function1())
{
Consile.WriteLine("API call went well.");
}
else
{
Consile.WriteLine("API call went bad.")
}
--------------------------
I'm aware that the above doesn't work, it's only to give a picture of
what I'm trying to accomplish. Somwhere around the part with the waiting
for the event to occur should most likely be using a new thread not to
eat all the cpu power, but I can't get the things I've tried to work.
Any help would be much appreciated, thanks
![Smile :) :)](/styles/default/custom/smilies/smile.gif)
/Aidal