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
java c# android xamarin xamarin.android
add a comment |
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
java c# android xamarin xamarin.android
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 thisTask.Run(async () => { await StartGain(); }).Wait();
– CGPA6.4
2 days ago
add a comment |
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
java c# android xamarin xamarin.android
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
java c# android xamarin xamarin.android
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 thisTask.Run(async () => { await StartGain(); }).Wait();
– CGPA6.4
2 days ago
add a comment |
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 thisTask.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
add a comment |
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.
if I remove the delayStartCapital()
function sometime ends afterStartGain()
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
add a comment |
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.
if I remove the delayStartCapital()
function sometime ends afterStartGain()
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
add a comment |
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.
if I remove the delayStartCapital()
function sometime ends afterStartGain()
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
add a comment |
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.
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.
answered Nov 17 at 19:39
war_Hero
5,14822944
5,14822944
if I remove the delayStartCapital()
function sometime ends afterStartGain()
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
add a comment |
if I remove the delayStartCapital()
function sometime ends afterStartGain()
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
add a comment |
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%2f53354775%2fhow-to-wait-for-handler-thread-to-finish-in-xamarin-android%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
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