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
}









share|improve this question
























  • 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












  • @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












  • 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















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
}









share|improve this question
























  • 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












  • @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












  • 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













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
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 14:30

























asked Nov 8 at 13:43









joe

232114




232114












  • 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












  • @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












  • 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


















  • 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












  • @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












  • 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
















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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings