How can I sync a pinvoke callback in a method?
up vote
0
down vote
favorite
I have a native dll written in C. One function is to search for Gateway and return after timeout.
In C codes,it's a synchronous process
search -> callback -> return bool.
So by the time find_gw finished, the gw_found has already been called thus we already have the results. Is there a proper way to sync this so that I can get gwaddr gwname right after find_gw? And I numberred the order it run. gw_found is called inside find_gw synchronously
var gw_found = new FOUND_GW_CALLBACK(callback) //1.new delegate
bool pinvokereturn=GwPInvokes.find_gw(broadcastaddr, timeout, gw_found);//2.pinvoke
//4.How to get gwaddr/gwname here
where
public static bool callback(IntPtr ptr,string gwddr,string gwname)
{
return true; //3.callback
}
c# callback pinvoke
|
show 9 more comments
up vote
0
down vote
favorite
I have a native dll written in C. One function is to search for Gateway and return after timeout.
In C codes,it's a synchronous process
search -> callback -> return bool.
So by the time find_gw finished, the gw_found has already been called thus we already have the results. Is there a proper way to sync this so that I can get gwaddr gwname right after find_gw? And I numberred the order it run. gw_found is called inside find_gw synchronously
var gw_found = new FOUND_GW_CALLBACK(callback) //1.new delegate
bool pinvokereturn=GwPInvokes.find_gw(broadcastaddr, timeout, gw_found);//2.pinvoke
//4.How to get gwaddr/gwname here
where
public static bool callback(IntPtr ptr,string gwddr,string gwname)
{
return true; //3.callback
}
c# callback pinvoke
ManualResetEventSlim ...var mres = new ManualResetEventSlim(false);in callbackmres.Set()... afterGwPInvokes.find_gwmres.Wait()the code aftermres.Wait()would be called after callback callsmres.Set()
– Selvin
Nov 8 at 13:49
orTaskCompletionSource<Tuple<string, string>>var tsc = new TaskCompletionSource<Tuple<string, string>>();in callbacktsc.SetResult(new Tuple<string,string>(gwddr, gwname);after GwPInvokes.find_gwvar result = await tsc.Task;in result.Item1 would begwddrand in Item2gwname
– Selvin
Nov 8 at 13:57
@Selvin This should work. But I was just wondering if we can do this without using a waithandle or building a task. Because thecallbcakwould be called beforefind_gwreturn. And those public fields are not thread-safe.
– joe
Nov 8 at 14:12
Because the callbcak would be called before find_gw return and ???
– Selvin
Nov 8 at 14:14
So by the time you hit the comment,gwddrandgwnameare already assigned by native code, you don't need to wait.
– joe
Nov 8 at 14:16
|
show 9 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a native dll written in C. One function is to search for Gateway and return after timeout.
In C codes,it's a synchronous process
search -> callback -> return bool.
So by the time find_gw finished, the gw_found has already been called thus we already have the results. Is there a proper way to sync this so that I can get gwaddr gwname right after find_gw? And I numberred the order it run. gw_found is called inside find_gw synchronously
var gw_found = new FOUND_GW_CALLBACK(callback) //1.new delegate
bool pinvokereturn=GwPInvokes.find_gw(broadcastaddr, timeout, gw_found);//2.pinvoke
//4.How to get gwaddr/gwname here
where
public static bool callback(IntPtr ptr,string gwddr,string gwname)
{
return true; //3.callback
}
c# callback pinvoke
I have a native dll written in C. One function is to search for Gateway and return after timeout.
In C codes,it's a synchronous process
search -> callback -> return bool.
So by the time find_gw finished, the gw_found has already been called thus we already have the results. Is there a proper way to sync this so that I can get gwaddr gwname right after find_gw? And I numberred the order it run. gw_found is called inside find_gw synchronously
var gw_found = new FOUND_GW_CALLBACK(callback) //1.new delegate
bool pinvokereturn=GwPInvokes.find_gw(broadcastaddr, timeout, gw_found);//2.pinvoke
//4.How to get gwaddr/gwname here
where
public static bool callback(IntPtr ptr,string gwddr,string gwname)
{
return true; //3.callback
}
c# callback pinvoke
c# callback pinvoke
edited Nov 8 at 14:30
asked Nov 8 at 13:43
joe
232114
232114
ManualResetEventSlim ...var mres = new ManualResetEventSlim(false);in callbackmres.Set()... afterGwPInvokes.find_gwmres.Wait()the code aftermres.Wait()would be called after callback callsmres.Set()
– Selvin
Nov 8 at 13:49
orTaskCompletionSource<Tuple<string, string>>var tsc = new TaskCompletionSource<Tuple<string, string>>();in callbacktsc.SetResult(new Tuple<string,string>(gwddr, gwname);after GwPInvokes.find_gwvar result = await tsc.Task;in result.Item1 would begwddrand in Item2gwname
– Selvin
Nov 8 at 13:57
@Selvin This should work. But I was just wondering if we can do this without using a waithandle or building a task. Because thecallbcakwould be called beforefind_gwreturn. And those public fields are not thread-safe.
– joe
Nov 8 at 14:12
Because the callbcak would be called before find_gw return and ???
– Selvin
Nov 8 at 14:14
So by the time you hit the comment,gwddrandgwnameare already assigned by native code, you don't need to wait.
– joe
Nov 8 at 14:16
|
show 9 more comments
ManualResetEventSlim ...var mres = new ManualResetEventSlim(false);in callbackmres.Set()... afterGwPInvokes.find_gwmres.Wait()the code aftermres.Wait()would be called after callback callsmres.Set()
– Selvin
Nov 8 at 13:49
orTaskCompletionSource<Tuple<string, string>>var tsc = new TaskCompletionSource<Tuple<string, string>>();in callbacktsc.SetResult(new Tuple<string,string>(gwddr, gwname);after GwPInvokes.find_gwvar result = await tsc.Task;in result.Item1 would begwddrand in Item2gwname
– Selvin
Nov 8 at 13:57
@Selvin This should work. But I was just wondering if we can do this without using a waithandle or building a task. Because thecallbcakwould be called beforefind_gwreturn. And those public fields are not thread-safe.
– joe
Nov 8 at 14:12
Because the callbcak would be called before find_gw return and ???
– Selvin
Nov 8 at 14:14
So by the time you hit the comment,gwddrandgwnameare already assigned by native code, you don't need to wait.
– joe
Nov 8 at 14:16
ManualResetEventSlim ...
var mres = new ManualResetEventSlim(false); in callback mres.Set() ... after GwPInvokes.find_gw mres.Wait() the code after mres.Wait() would be called after callback calls mres.Set()– Selvin
Nov 8 at 13:49
ManualResetEventSlim ...
var mres = new ManualResetEventSlim(false); in callback mres.Set() ... after GwPInvokes.find_gw mres.Wait() the code after mres.Wait() would be called after callback calls mres.Set()– Selvin
Nov 8 at 13:49
or
TaskCompletionSource<Tuple<string, string>> var tsc = new TaskCompletionSource<Tuple<string, string>>(); in callback tsc.SetResult(new Tuple<string,string>(gwddr, gwname); after GwPInvokes.find_gw var result = await tsc.Task; in result.Item1 would be gwddr and in Item2 gwname– Selvin
Nov 8 at 13:57
or
TaskCompletionSource<Tuple<string, string>> var tsc = new TaskCompletionSource<Tuple<string, string>>(); in callback tsc.SetResult(new Tuple<string,string>(gwddr, gwname); after GwPInvokes.find_gw var result = await tsc.Task; in result.Item1 would be gwddr and in Item2 gwname– Selvin
Nov 8 at 13:57
@Selvin This should work. But I was just wondering if we can do this without using a waithandle or building a task. Because the
callbcak would be called before find_gw return. And those public fields are not thread-safe.– joe
Nov 8 at 14:12
@Selvin This should work. But I was just wondering if we can do this without using a waithandle or building a task. Because the
callbcak would be called before find_gw return. And those public fields are not thread-safe.– joe
Nov 8 at 14:12
Because the callbcak would be called before find_gw return and ???
– Selvin
Nov 8 at 14:14
Because the callbcak would be called before find_gw return and ???
– Selvin
Nov 8 at 14:14
So by the time you hit the comment,
gwddr and gwname are already assigned by native code, you don't need to wait.– joe
Nov 8 at 14:16
So by the time you hit the comment,
gwddr and gwname are already assigned by native code, you don't need to wait.– joe
Nov 8 at 14:16
|
show 9 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208996%2fhow-can-i-sync-a-pinvoke-callback-in-a-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
ManualResetEventSlim ...
var mres = new ManualResetEventSlim(false);in callbackmres.Set()... afterGwPInvokes.find_gwmres.Wait()the code aftermres.Wait()would be called after callback callsmres.Set()– Selvin
Nov 8 at 13:49
or
TaskCompletionSource<Tuple<string, string>>var tsc = new TaskCompletionSource<Tuple<string, string>>();in callbacktsc.SetResult(new Tuple<string,string>(gwddr, gwname);after GwPInvokes.find_gwvar result = await tsc.Task;in result.Item1 would begwddrand in Item2gwname– Selvin
Nov 8 at 13:57
@Selvin This should work. But I was just wondering if we can do this without using a waithandle or building a task. Because the
callbcakwould be called beforefind_gwreturn. And those public fields are not thread-safe.– joe
Nov 8 at 14:12
Because the callbcak would be called before find_gw return and ???
– Selvin
Nov 8 at 14:14
So by the time you hit the comment,
gwddrandgwnameare already assigned by native code, you don't need to wait.– joe
Nov 8 at 14:16