How to Wait for Handler Thread to finish in Xamarin Android?











up vote
1
down vote

favorite












How to wait for the handler.PostDelayed to finish before the function continues?



    int num = null;
public int DoSomething()
{

var handler = new Handler();
handler.PostDelayed(() => StartCapital(), 10);

handler.PostDelayed(() =>
{
num = StartGain();
}, 300);

return num;
}


how to Wait for the StartGain() Method thread to finish? the method takes sometime to finish and the Function it is in returns null










share|improve this question
























  • why do you call the method from another thread if you want to wait the command has been finished?
    – AKSW
    Nov 17 at 19:39












  • it takes time and I don't want the UI to be hanged
    – VINNUSAURUS
    Nov 17 at 19:45










  • you will have to create a thread using new runnable and pass it to the handler to make it work in the background, right now you are just adding a delay on that piece of code thats all
    – war_Hero
    Nov 17 at 19:47










  • delay is not the problem, problem is the function finishes before returning a integer value, which makes return value null
    – VINNUSAURUS
    Nov 17 at 19:52










  • @VINNUSAURUS - You can try this Task.Run(async () => { await StartGain(); }).Wait();
    – CGPA6.4
    2 days ago

















up vote
1
down vote

favorite












How to wait for the handler.PostDelayed to finish before the function continues?



    int num = null;
public int DoSomething()
{

var handler = new Handler();
handler.PostDelayed(() => StartCapital(), 10);

handler.PostDelayed(() =>
{
num = StartGain();
}, 300);

return num;
}


how to Wait for the StartGain() Method thread to finish? the method takes sometime to finish and the Function it is in returns null










share|improve this question
























  • why do you call the method from another thread if you want to wait the command has been finished?
    – AKSW
    Nov 17 at 19:39












  • it takes time and I don't want the UI to be hanged
    – VINNUSAURUS
    Nov 17 at 19:45










  • you will have to create a thread using new runnable and pass it to the handler to make it work in the background, right now you are just adding a delay on that piece of code thats all
    – war_Hero
    Nov 17 at 19:47










  • delay is not the problem, problem is the function finishes before returning a integer value, which makes return value null
    – VINNUSAURUS
    Nov 17 at 19:52










  • @VINNUSAURUS - You can try this Task.Run(async () => { await StartGain(); }).Wait();
    – CGPA6.4
    2 days ago















up vote
1
down vote

favorite









up vote
1
down vote

favorite











How to wait for the handler.PostDelayed to finish before the function continues?



    int num = null;
public int DoSomething()
{

var handler = new Handler();
handler.PostDelayed(() => StartCapital(), 10);

handler.PostDelayed(() =>
{
num = StartGain();
}, 300);

return num;
}


how to Wait for the StartGain() Method thread to finish? the method takes sometime to finish and the Function it is in returns null










share|improve this question















How to wait for the handler.PostDelayed to finish before the function continues?



    int num = null;
public int DoSomething()
{

var handler = new Handler();
handler.PostDelayed(() => StartCapital(), 10);

handler.PostDelayed(() =>
{
num = StartGain();
}, 300);

return num;
}


how to Wait for the StartGain() Method thread to finish? the method takes sometime to finish and the Function it is in returns null







java c# android xamarin xamarin.android






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 at 20:43

























asked Nov 17 at 19:29









VINNUSAURUS

1781314




1781314












  • why do you call the method from another thread if you want to wait the command has been finished?
    – AKSW
    Nov 17 at 19:39












  • it takes time and I don't want the UI to be hanged
    – VINNUSAURUS
    Nov 17 at 19:45










  • you will have to create a thread using new runnable and pass it to the handler to make it work in the background, right now you are just adding a delay on that piece of code thats all
    – war_Hero
    Nov 17 at 19:47










  • delay is not the problem, problem is the function finishes before returning a integer value, which makes return value null
    – VINNUSAURUS
    Nov 17 at 19:52










  • @VINNUSAURUS - You can try this Task.Run(async () => { await StartGain(); }).Wait();
    – CGPA6.4
    2 days ago




















  • why do you call the method from another thread if you want to wait the command has been finished?
    – AKSW
    Nov 17 at 19:39












  • it takes time and I don't want the UI to be hanged
    – VINNUSAURUS
    Nov 17 at 19:45










  • you will have to create a thread using new runnable and pass it to the handler to make it work in the background, right now you are just adding a delay on that piece of code thats all
    – war_Hero
    Nov 17 at 19:47










  • delay is not the problem, problem is the function finishes before returning a integer value, which makes return value null
    – VINNUSAURUS
    Nov 17 at 19:52










  • @VINNUSAURUS - You can try this Task.Run(async () => { await StartGain(); }).Wait();
    – CGPA6.4
    2 days ago


















why do you call the method from another thread if you want to wait the command has been finished?
– AKSW
Nov 17 at 19:39






why do you call the method from another thread if you want to wait the command has been finished?
– AKSW
Nov 17 at 19:39














it takes time and I don't want the UI to be hanged
– VINNUSAURUS
Nov 17 at 19:45




it takes time and I don't want the UI to be hanged
– VINNUSAURUS
Nov 17 at 19:45












you will have to create a thread using new runnable and pass it to the handler to make it work in the background, right now you are just adding a delay on that piece of code thats all
– war_Hero
Nov 17 at 19:47




you will have to create a thread using new runnable and pass it to the handler to make it work in the background, right now you are just adding a delay on that piece of code thats all
– war_Hero
Nov 17 at 19:47












delay is not the problem, problem is the function finishes before returning a integer value, which makes return value null
– VINNUSAURUS
Nov 17 at 19:52




delay is not the problem, problem is the function finishes before returning a integer value, which makes return value null
– VINNUSAURUS
Nov 17 at 19:52












@VINNUSAURUS - You can try this Task.Run(async () => { await StartGain(); }).Wait();
– CGPA6.4
2 days ago






@VINNUSAURUS - You can try this Task.Run(async () => { await StartGain(); }).Wait();
– CGPA6.4
2 days ago














1 Answer
1






active

oldest

votes

















up vote
0
down vote













This looks like a problem in understanding the way Threads work, You create a background thread so that you won't have to block the main thread for some huge process to finish among many other uses,



Like in your case the method StartGain() will be executed on the main thread after a delay of 300ms so if you don't want to wait remove the delay and the handler and run it on the normal flow. If the value that method returns are needs for the further calculations then run it in a separate background thread before continuing.






share|improve this answer





















  • if I remove the delay StartCapital() function sometime ends after StartGain() which causes problem
    – VINNUSAURUS
    Nov 17 at 19:48










  • I understand so don't move it into a background thread , instead run it on your main thread as it's needed for the flow, it's justifiable delay, you can use a different thread to get that value ahead of time
    – war_Hero
    Nov 17 at 19:51











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%2f53354775%2fhow-to-wait-for-handler-thread-to-finish-in-xamarin-android%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













This looks like a problem in understanding the way Threads work, You create a background thread so that you won't have to block the main thread for some huge process to finish among many other uses,



Like in your case the method StartGain() will be executed on the main thread after a delay of 300ms so if you don't want to wait remove the delay and the handler and run it on the normal flow. If the value that method returns are needs for the further calculations then run it in a separate background thread before continuing.






share|improve this answer





















  • if I remove the delay StartCapital() function sometime ends after StartGain() which causes problem
    – VINNUSAURUS
    Nov 17 at 19:48










  • I understand so don't move it into a background thread , instead run it on your main thread as it's needed for the flow, it's justifiable delay, you can use a different thread to get that value ahead of time
    – war_Hero
    Nov 17 at 19:51















up vote
0
down vote













This looks like a problem in understanding the way Threads work, You create a background thread so that you won't have to block the main thread for some huge process to finish among many other uses,



Like in your case the method StartGain() will be executed on the main thread after a delay of 300ms so if you don't want to wait remove the delay and the handler and run it on the normal flow. If the value that method returns are needs for the further calculations then run it in a separate background thread before continuing.






share|improve this answer





















  • if I remove the delay StartCapital() function sometime ends after StartGain() which causes problem
    – VINNUSAURUS
    Nov 17 at 19:48










  • I understand so don't move it into a background thread , instead run it on your main thread as it's needed for the flow, it's justifiable delay, you can use a different thread to get that value ahead of time
    – war_Hero
    Nov 17 at 19:51













up vote
0
down vote










up vote
0
down vote









This looks like a problem in understanding the way Threads work, You create a background thread so that you won't have to block the main thread for some huge process to finish among many other uses,



Like in your case the method StartGain() will be executed on the main thread after a delay of 300ms so if you don't want to wait remove the delay and the handler and run it on the normal flow. If the value that method returns are needs for the further calculations then run it in a separate background thread before continuing.






share|improve this answer












This looks like a problem in understanding the way Threads work, You create a background thread so that you won't have to block the main thread for some huge process to finish among many other uses,



Like in your case the method StartGain() will be executed on the main thread after a delay of 300ms so if you don't want to wait remove the delay and the handler and run it on the normal flow. If the value that method returns are needs for the further calculations then run it in a separate background thread before continuing.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 17 at 19:39









war_Hero

5,14822944




5,14822944












  • if I remove the delay StartCapital() function sometime ends after StartGain() which causes problem
    – VINNUSAURUS
    Nov 17 at 19:48










  • I understand so don't move it into a background thread , instead run it on your main thread as it's needed for the flow, it's justifiable delay, you can use a different thread to get that value ahead of time
    – war_Hero
    Nov 17 at 19:51


















  • if I remove the delay StartCapital() function sometime ends after StartGain() which causes problem
    – VINNUSAURUS
    Nov 17 at 19:48










  • I understand so don't move it into a background thread , instead run it on your main thread as it's needed for the flow, it's justifiable delay, you can use a different thread to get that value ahead of time
    – war_Hero
    Nov 17 at 19:51
















if I remove the delay StartCapital() function sometime ends after StartGain() which causes problem
– VINNUSAURUS
Nov 17 at 19:48




if I remove the delay StartCapital() function sometime ends after StartGain() which causes problem
– VINNUSAURUS
Nov 17 at 19:48












I understand so don't move it into a background thread , instead run it on your main thread as it's needed for the flow, it's justifiable delay, you can use a different thread to get that value ahead of time
– war_Hero
Nov 17 at 19:51




I understand so don't move it into a background thread , instead run it on your main thread as it's needed for the flow, it's justifiable delay, you can use a different thread to get that value ahead of time
– war_Hero
Nov 17 at 19:51


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354775%2fhow-to-wait-for-handler-thread-to-finish-in-xamarin-android%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







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin