c# winforms my ui still freeze with async in event load
In my event load of my form , I call a method loadDg
:
private void form_Load(object sender, EventArgs e)
loadDg();
and
private async Task loadDg()
pictureLoading.Visible = true;
await Task.Run(() => string datas = db.row("select * from products");
string datas2 = db.row("select * from users");
double one = Convert.ToInt32(datas[0]);
label1.Text = one.toString();
//....
);
pictureLoading.Visible = false; //hide gif animation
in my code , db.row This method always returns only 1 row ( string array) , but my ui freezes still , i try update UI continuously with async without freeze at startup
c# .net winforms asynchronous async-await
add a comment |
In my event load of my form , I call a method loadDg
:
private void form_Load(object sender, EventArgs e)
loadDg();
and
private async Task loadDg()
pictureLoading.Visible = true;
await Task.Run(() => string datas = db.row("select * from products");
string datas2 = db.row("select * from users");
double one = Convert.ToInt32(datas[0]);
label1.Text = one.toString();
//....
);
pictureLoading.Visible = false; //hide gif animation
in my code , db.row This method always returns only 1 row ( string array) , but my ui freezes still , i try update UI continuously with async without freeze at startup
c# .net winforms asynchronous async-await
3
label1.Text = ...
in non-ui thread is bad.
– Sinatr
Nov 15 '18 at 15:27
@Sinatr what's is the best way to fix it ?
– kevin73
Nov 15 '18 at 15:32
@Flydog57loadDg
is not "called synchronously". Given what's shown, it's an asynchronous method, and nothing is explicitly blocking until its completion in the code shown, so it will run asynchronously. Markingform_Load
asasync
would only be useful if you need to do something in response to the completion of the asynchronous method. In this case, there is nothing being done when it finishes.
– Servy
Nov 15 '18 at 16:25
1
To fix thelabel1.Text =...
issue, create a small function that sets the label and useControl.Invoke
to invoke that function on the thread that owns the control (remember that the Form class is a sub-class of the control). There are lots of examples on how to do that.
– Flydog57
Nov 15 '18 at 17:08
add a comment |
In my event load of my form , I call a method loadDg
:
private void form_Load(object sender, EventArgs e)
loadDg();
and
private async Task loadDg()
pictureLoading.Visible = true;
await Task.Run(() => string datas = db.row("select * from products");
string datas2 = db.row("select * from users");
double one = Convert.ToInt32(datas[0]);
label1.Text = one.toString();
//....
);
pictureLoading.Visible = false; //hide gif animation
in my code , db.row This method always returns only 1 row ( string array) , but my ui freezes still , i try update UI continuously with async without freeze at startup
c# .net winforms asynchronous async-await
In my event load of my form , I call a method loadDg
:
private void form_Load(object sender, EventArgs e)
loadDg();
and
private async Task loadDg()
pictureLoading.Visible = true;
await Task.Run(() => string datas = db.row("select * from products");
string datas2 = db.row("select * from users");
double one = Convert.ToInt32(datas[0]);
label1.Text = one.toString();
//....
);
pictureLoading.Visible = false; //hide gif animation
in my code , db.row This method always returns only 1 row ( string array) , but my ui freezes still , i try update UI continuously with async without freeze at startup
c# .net winforms asynchronous async-await
c# .net winforms asynchronous async-await
asked Nov 15 '18 at 15:21
kevin73kevin73
93
93
3
label1.Text = ...
in non-ui thread is bad.
– Sinatr
Nov 15 '18 at 15:27
@Sinatr what's is the best way to fix it ?
– kevin73
Nov 15 '18 at 15:32
@Flydog57loadDg
is not "called synchronously". Given what's shown, it's an asynchronous method, and nothing is explicitly blocking until its completion in the code shown, so it will run asynchronously. Markingform_Load
asasync
would only be useful if you need to do something in response to the completion of the asynchronous method. In this case, there is nothing being done when it finishes.
– Servy
Nov 15 '18 at 16:25
1
To fix thelabel1.Text =...
issue, create a small function that sets the label and useControl.Invoke
to invoke that function on the thread that owns the control (remember that the Form class is a sub-class of the control). There are lots of examples on how to do that.
– Flydog57
Nov 15 '18 at 17:08
add a comment |
3
label1.Text = ...
in non-ui thread is bad.
– Sinatr
Nov 15 '18 at 15:27
@Sinatr what's is the best way to fix it ?
– kevin73
Nov 15 '18 at 15:32
@Flydog57loadDg
is not "called synchronously". Given what's shown, it's an asynchronous method, and nothing is explicitly blocking until its completion in the code shown, so it will run asynchronously. Markingform_Load
asasync
would only be useful if you need to do something in response to the completion of the asynchronous method. In this case, there is nothing being done when it finishes.
– Servy
Nov 15 '18 at 16:25
1
To fix thelabel1.Text =...
issue, create a small function that sets the label and useControl.Invoke
to invoke that function on the thread that owns the control (remember that the Form class is a sub-class of the control). There are lots of examples on how to do that.
– Flydog57
Nov 15 '18 at 17:08
3
3
label1.Text = ...
in non-ui thread is bad.– Sinatr
Nov 15 '18 at 15:27
label1.Text = ...
in non-ui thread is bad.– Sinatr
Nov 15 '18 at 15:27
@Sinatr what's is the best way to fix it ?
– kevin73
Nov 15 '18 at 15:32
@Sinatr what's is the best way to fix it ?
– kevin73
Nov 15 '18 at 15:32
@Flydog57
loadDg
is not "called synchronously". Given what's shown, it's an asynchronous method, and nothing is explicitly blocking until its completion in the code shown, so it will run asynchronously. Marking form_Load
as async
would only be useful if you need to do something in response to the completion of the asynchronous method. In this case, there is nothing being done when it finishes.– Servy
Nov 15 '18 at 16:25
@Flydog57
loadDg
is not "called synchronously". Given what's shown, it's an asynchronous method, and nothing is explicitly blocking until its completion in the code shown, so it will run asynchronously. Marking form_Load
as async
would only be useful if you need to do something in response to the completion of the asynchronous method. In this case, there is nothing being done when it finishes.– Servy
Nov 15 '18 at 16:25
1
1
To fix the
label1.Text =...
issue, create a small function that sets the label and use Control.Invoke
to invoke that function on the thread that owns the control (remember that the Form class is a sub-class of the control). There are lots of examples on how to do that.– Flydog57
Nov 15 '18 at 17:08
To fix the
label1.Text =...
issue, create a small function that sets the label and use Control.Invoke
to invoke that function on the thread that owns the control (remember that the Form class is a sub-class of the control). There are lots of examples on how to do that.– Flydog57
Nov 15 '18 at 17:08
add a comment |
3 Answers
3
active
oldest
votes
There is nothing to prevent your code run asynchronously. pictureLoading
will be invisible even before task is completed. You should fix cross-thread problem and logic of the UI as this:
private void form_Load(object sender, EventArgs e)
pictureLoading.Visible = true;
loadDg();
private async Task loadDg()
await Task.Run(() =>
string datas = db.row("select * from products");
string datas2 = db.row("select * from users");
double one = Convert.ToInt32(datas[0]);
label1.BeginInvoke((Action)delegate ()
label1.Text = one.toString();
//hide gif animation
pictureLoading.Visible = false;
);
//....
);
1
What's the benefit of invoking the UI thread from a thread pool thread and immediately get back to the UI thread?
– Paulo Morgado
Nov 16 '18 at 0:47
@PauloMorgado Well for that simple case, your solution would be better. However, if the task is a loop and you want to update the UI multiple times, then the above code would be fine (but in that later case, sometime aBackgroundWorker
might also works).
– Phil1970
Nov 16 '18 at 3:32
OrIProgress
. But, in that case, you wouldn't be hidingpictureLoading
. Would you?
– Paulo Morgado
Nov 16 '18 at 7:12
@PauloMorgado 1.I tried to solve the problem with minimal changes in code. 2.The question had multiple UI updates in different lines. So, it may be easier this way. 3.Also, the method runs once in FormLoad which doesn't make me worried about resource usage.
– aliassce
Nov 16 '18 at 7:27
add a comment |
Unnecessarily jumping between threads/context should be avoided.
This is an with better resource usage:
private async void form_Load(object sender, EventArgs e)
pictureLoading.Visible = true;
try
label1.Text = await LoadDgAsync();
catch
// error handling
finally
pictureLoading.Visible = false;
private Task<string> LoadDgAsync()
return Task.Run(() =>
string datas = db.row("select * from products");
string datas2 = db.row("select * from users");
double one = Convert.ToInt32(datas[0]);
//....
return one.toString();
);
add a comment |
You are calling the loadDg()
function synchronously.
Unless you await the loadDg()
function call (since its return type is Task) and make the form_Load
function asynchronous the function call will be synchronous.
The correct way to fix it is...
private async void form_Load(object sender, EventArgs e)
await loadDg();
1
This is false. Calling an asynchronous method doesn't run it synchronously. The method would only run synchronously if it was a synchronous method from the start, and if that were true, then this code would behave no differently and would also run it synchronously.await
is a tool for more conveniently doing something after an asynchronous method completes. As this code has nothing to do when the asynchronous method completes, it's accomplishing nothing.
– Servy
Nov 15 '18 at 16:27
@Servy maybe you can recommand another best approach to resolve this problem ?
– kevin73
Nov 15 '18 at 16:34
@kevin73 You need to provide enough code to reproduce the problem for anyone to tell you how to fix it.
– Servy
Nov 15 '18 at 16:55
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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%2f53322607%2fc-sharp-winforms-my-ui-still-freeze-with-async-in-event-load%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is nothing to prevent your code run asynchronously. pictureLoading
will be invisible even before task is completed. You should fix cross-thread problem and logic of the UI as this:
private void form_Load(object sender, EventArgs e)
pictureLoading.Visible = true;
loadDg();
private async Task loadDg()
await Task.Run(() =>
string datas = db.row("select * from products");
string datas2 = db.row("select * from users");
double one = Convert.ToInt32(datas[0]);
label1.BeginInvoke((Action)delegate ()
label1.Text = one.toString();
//hide gif animation
pictureLoading.Visible = false;
);
//....
);
1
What's the benefit of invoking the UI thread from a thread pool thread and immediately get back to the UI thread?
– Paulo Morgado
Nov 16 '18 at 0:47
@PauloMorgado Well for that simple case, your solution would be better. However, if the task is a loop and you want to update the UI multiple times, then the above code would be fine (but in that later case, sometime aBackgroundWorker
might also works).
– Phil1970
Nov 16 '18 at 3:32
OrIProgress
. But, in that case, you wouldn't be hidingpictureLoading
. Would you?
– Paulo Morgado
Nov 16 '18 at 7:12
@PauloMorgado 1.I tried to solve the problem with minimal changes in code. 2.The question had multiple UI updates in different lines. So, it may be easier this way. 3.Also, the method runs once in FormLoad which doesn't make me worried about resource usage.
– aliassce
Nov 16 '18 at 7:27
add a comment |
There is nothing to prevent your code run asynchronously. pictureLoading
will be invisible even before task is completed. You should fix cross-thread problem and logic of the UI as this:
private void form_Load(object sender, EventArgs e)
pictureLoading.Visible = true;
loadDg();
private async Task loadDg()
await Task.Run(() =>
string datas = db.row("select * from products");
string datas2 = db.row("select * from users");
double one = Convert.ToInt32(datas[0]);
label1.BeginInvoke((Action)delegate ()
label1.Text = one.toString();
//hide gif animation
pictureLoading.Visible = false;
);
//....
);
1
What's the benefit of invoking the UI thread from a thread pool thread and immediately get back to the UI thread?
– Paulo Morgado
Nov 16 '18 at 0:47
@PauloMorgado Well for that simple case, your solution would be better. However, if the task is a loop and you want to update the UI multiple times, then the above code would be fine (but in that later case, sometime aBackgroundWorker
might also works).
– Phil1970
Nov 16 '18 at 3:32
OrIProgress
. But, in that case, you wouldn't be hidingpictureLoading
. Would you?
– Paulo Morgado
Nov 16 '18 at 7:12
@PauloMorgado 1.I tried to solve the problem with minimal changes in code. 2.The question had multiple UI updates in different lines. So, it may be easier this way. 3.Also, the method runs once in FormLoad which doesn't make me worried about resource usage.
– aliassce
Nov 16 '18 at 7:27
add a comment |
There is nothing to prevent your code run asynchronously. pictureLoading
will be invisible even before task is completed. You should fix cross-thread problem and logic of the UI as this:
private void form_Load(object sender, EventArgs e)
pictureLoading.Visible = true;
loadDg();
private async Task loadDg()
await Task.Run(() =>
string datas = db.row("select * from products");
string datas2 = db.row("select * from users");
double one = Convert.ToInt32(datas[0]);
label1.BeginInvoke((Action)delegate ()
label1.Text = one.toString();
//hide gif animation
pictureLoading.Visible = false;
);
//....
);
There is nothing to prevent your code run asynchronously. pictureLoading
will be invisible even before task is completed. You should fix cross-thread problem and logic of the UI as this:
private void form_Load(object sender, EventArgs e)
pictureLoading.Visible = true;
loadDg();
private async Task loadDg()
await Task.Run(() =>
string datas = db.row("select * from products");
string datas2 = db.row("select * from users");
double one = Convert.ToInt32(datas[0]);
label1.BeginInvoke((Action)delegate ()
label1.Text = one.toString();
//hide gif animation
pictureLoading.Visible = false;
);
//....
);
answered Nov 15 '18 at 17:40
aliasscealiassce
931414
931414
1
What's the benefit of invoking the UI thread from a thread pool thread and immediately get back to the UI thread?
– Paulo Morgado
Nov 16 '18 at 0:47
@PauloMorgado Well for that simple case, your solution would be better. However, if the task is a loop and you want to update the UI multiple times, then the above code would be fine (but in that later case, sometime aBackgroundWorker
might also works).
– Phil1970
Nov 16 '18 at 3:32
OrIProgress
. But, in that case, you wouldn't be hidingpictureLoading
. Would you?
– Paulo Morgado
Nov 16 '18 at 7:12
@PauloMorgado 1.I tried to solve the problem with minimal changes in code. 2.The question had multiple UI updates in different lines. So, it may be easier this way. 3.Also, the method runs once in FormLoad which doesn't make me worried about resource usage.
– aliassce
Nov 16 '18 at 7:27
add a comment |
1
What's the benefit of invoking the UI thread from a thread pool thread and immediately get back to the UI thread?
– Paulo Morgado
Nov 16 '18 at 0:47
@PauloMorgado Well for that simple case, your solution would be better. However, if the task is a loop and you want to update the UI multiple times, then the above code would be fine (but in that later case, sometime aBackgroundWorker
might also works).
– Phil1970
Nov 16 '18 at 3:32
OrIProgress
. But, in that case, you wouldn't be hidingpictureLoading
. Would you?
– Paulo Morgado
Nov 16 '18 at 7:12
@PauloMorgado 1.I tried to solve the problem with minimal changes in code. 2.The question had multiple UI updates in different lines. So, it may be easier this way. 3.Also, the method runs once in FormLoad which doesn't make me worried about resource usage.
– aliassce
Nov 16 '18 at 7:27
1
1
What's the benefit of invoking the UI thread from a thread pool thread and immediately get back to the UI thread?
– Paulo Morgado
Nov 16 '18 at 0:47
What's the benefit of invoking the UI thread from a thread pool thread and immediately get back to the UI thread?
– Paulo Morgado
Nov 16 '18 at 0:47
@PauloMorgado Well for that simple case, your solution would be better. However, if the task is a loop and you want to update the UI multiple times, then the above code would be fine (but in that later case, sometime a
BackgroundWorker
might also works).– Phil1970
Nov 16 '18 at 3:32
@PauloMorgado Well for that simple case, your solution would be better. However, if the task is a loop and you want to update the UI multiple times, then the above code would be fine (but in that later case, sometime a
BackgroundWorker
might also works).– Phil1970
Nov 16 '18 at 3:32
Or
IProgress
. But, in that case, you wouldn't be hiding pictureLoading
. Would you?– Paulo Morgado
Nov 16 '18 at 7:12
Or
IProgress
. But, in that case, you wouldn't be hiding pictureLoading
. Would you?– Paulo Morgado
Nov 16 '18 at 7:12
@PauloMorgado 1.I tried to solve the problem with minimal changes in code. 2.The question had multiple UI updates in different lines. So, it may be easier this way. 3.Also, the method runs once in FormLoad which doesn't make me worried about resource usage.
– aliassce
Nov 16 '18 at 7:27
@PauloMorgado 1.I tried to solve the problem with minimal changes in code. 2.The question had multiple UI updates in different lines. So, it may be easier this way. 3.Also, the method runs once in FormLoad which doesn't make me worried about resource usage.
– aliassce
Nov 16 '18 at 7:27
add a comment |
Unnecessarily jumping between threads/context should be avoided.
This is an with better resource usage:
private async void form_Load(object sender, EventArgs e)
pictureLoading.Visible = true;
try
label1.Text = await LoadDgAsync();
catch
// error handling
finally
pictureLoading.Visible = false;
private Task<string> LoadDgAsync()
return Task.Run(() =>
string datas = db.row("select * from products");
string datas2 = db.row("select * from users");
double one = Convert.ToInt32(datas[0]);
//....
return one.toString();
);
add a comment |
Unnecessarily jumping between threads/context should be avoided.
This is an with better resource usage:
private async void form_Load(object sender, EventArgs e)
pictureLoading.Visible = true;
try
label1.Text = await LoadDgAsync();
catch
// error handling
finally
pictureLoading.Visible = false;
private Task<string> LoadDgAsync()
return Task.Run(() =>
string datas = db.row("select * from products");
string datas2 = db.row("select * from users");
double one = Convert.ToInt32(datas[0]);
//....
return one.toString();
);
add a comment |
Unnecessarily jumping between threads/context should be avoided.
This is an with better resource usage:
private async void form_Load(object sender, EventArgs e)
pictureLoading.Visible = true;
try
label1.Text = await LoadDgAsync();
catch
// error handling
finally
pictureLoading.Visible = false;
private Task<string> LoadDgAsync()
return Task.Run(() =>
string datas = db.row("select * from products");
string datas2 = db.row("select * from users");
double one = Convert.ToInt32(datas[0]);
//....
return one.toString();
);
Unnecessarily jumping between threads/context should be avoided.
This is an with better resource usage:
private async void form_Load(object sender, EventArgs e)
pictureLoading.Visible = true;
try
label1.Text = await LoadDgAsync();
catch
// error handling
finally
pictureLoading.Visible = false;
private Task<string> LoadDgAsync()
return Task.Run(() =>
string datas = db.row("select * from products");
string datas2 = db.row("select * from users");
double one = Convert.ToInt32(datas[0]);
//....
return one.toString();
);
answered Nov 16 '18 at 0:54
Paulo MorgadoPaulo Morgado
6,11811533
6,11811533
add a comment |
add a comment |
You are calling the loadDg()
function synchronously.
Unless you await the loadDg()
function call (since its return type is Task) and make the form_Load
function asynchronous the function call will be synchronous.
The correct way to fix it is...
private async void form_Load(object sender, EventArgs e)
await loadDg();
1
This is false. Calling an asynchronous method doesn't run it synchronously. The method would only run synchronously if it was a synchronous method from the start, and if that were true, then this code would behave no differently and would also run it synchronously.await
is a tool for more conveniently doing something after an asynchronous method completes. As this code has nothing to do when the asynchronous method completes, it's accomplishing nothing.
– Servy
Nov 15 '18 at 16:27
@Servy maybe you can recommand another best approach to resolve this problem ?
– kevin73
Nov 15 '18 at 16:34
@kevin73 You need to provide enough code to reproduce the problem for anyone to tell you how to fix it.
– Servy
Nov 15 '18 at 16:55
add a comment |
You are calling the loadDg()
function synchronously.
Unless you await the loadDg()
function call (since its return type is Task) and make the form_Load
function asynchronous the function call will be synchronous.
The correct way to fix it is...
private async void form_Load(object sender, EventArgs e)
await loadDg();
1
This is false. Calling an asynchronous method doesn't run it synchronously. The method would only run synchronously if it was a synchronous method from the start, and if that were true, then this code would behave no differently and would also run it synchronously.await
is a tool for more conveniently doing something after an asynchronous method completes. As this code has nothing to do when the asynchronous method completes, it's accomplishing nothing.
– Servy
Nov 15 '18 at 16:27
@Servy maybe you can recommand another best approach to resolve this problem ?
– kevin73
Nov 15 '18 at 16:34
@kevin73 You need to provide enough code to reproduce the problem for anyone to tell you how to fix it.
– Servy
Nov 15 '18 at 16:55
add a comment |
You are calling the loadDg()
function synchronously.
Unless you await the loadDg()
function call (since its return type is Task) and make the form_Load
function asynchronous the function call will be synchronous.
The correct way to fix it is...
private async void form_Load(object sender, EventArgs e)
await loadDg();
You are calling the loadDg()
function synchronously.
Unless you await the loadDg()
function call (since its return type is Task) and make the form_Load
function asynchronous the function call will be synchronous.
The correct way to fix it is...
private async void form_Load(object sender, EventArgs e)
await loadDg();
edited Nov 15 '18 at 17:01
gravitymixes
1,65211525
1,65211525
answered Nov 15 '18 at 16:17
kiran sai bandreddykiran sai bandreddy
11
11
1
This is false. Calling an asynchronous method doesn't run it synchronously. The method would only run synchronously if it was a synchronous method from the start, and if that were true, then this code would behave no differently and would also run it synchronously.await
is a tool for more conveniently doing something after an asynchronous method completes. As this code has nothing to do when the asynchronous method completes, it's accomplishing nothing.
– Servy
Nov 15 '18 at 16:27
@Servy maybe you can recommand another best approach to resolve this problem ?
– kevin73
Nov 15 '18 at 16:34
@kevin73 You need to provide enough code to reproduce the problem for anyone to tell you how to fix it.
– Servy
Nov 15 '18 at 16:55
add a comment |
1
This is false. Calling an asynchronous method doesn't run it synchronously. The method would only run synchronously if it was a synchronous method from the start, and if that were true, then this code would behave no differently and would also run it synchronously.await
is a tool for more conveniently doing something after an asynchronous method completes. As this code has nothing to do when the asynchronous method completes, it's accomplishing nothing.
– Servy
Nov 15 '18 at 16:27
@Servy maybe you can recommand another best approach to resolve this problem ?
– kevin73
Nov 15 '18 at 16:34
@kevin73 You need to provide enough code to reproduce the problem for anyone to tell you how to fix it.
– Servy
Nov 15 '18 at 16:55
1
1
This is false. Calling an asynchronous method doesn't run it synchronously. The method would only run synchronously if it was a synchronous method from the start, and if that were true, then this code would behave no differently and would also run it synchronously.
await
is a tool for more conveniently doing something after an asynchronous method completes. As this code has nothing to do when the asynchronous method completes, it's accomplishing nothing.– Servy
Nov 15 '18 at 16:27
This is false. Calling an asynchronous method doesn't run it synchronously. The method would only run synchronously if it was a synchronous method from the start, and if that were true, then this code would behave no differently and would also run it synchronously.
await
is a tool for more conveniently doing something after an asynchronous method completes. As this code has nothing to do when the asynchronous method completes, it's accomplishing nothing.– Servy
Nov 15 '18 at 16:27
@Servy maybe you can recommand another best approach to resolve this problem ?
– kevin73
Nov 15 '18 at 16:34
@Servy maybe you can recommand another best approach to resolve this problem ?
– kevin73
Nov 15 '18 at 16:34
@kevin73 You need to provide enough code to reproduce the problem for anyone to tell you how to fix it.
– Servy
Nov 15 '18 at 16:55
@kevin73 You need to provide enough code to reproduce the problem for anyone to tell you how to fix it.
– Servy
Nov 15 '18 at 16:55
add a comment |
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.
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%2f53322607%2fc-sharp-winforms-my-ui-still-freeze-with-async-in-event-load%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
3
label1.Text = ...
in non-ui thread is bad.– Sinatr
Nov 15 '18 at 15:27
@Sinatr what's is the best way to fix it ?
– kevin73
Nov 15 '18 at 15:32
@Flydog57
loadDg
is not "called synchronously". Given what's shown, it's an asynchronous method, and nothing is explicitly blocking until its completion in the code shown, so it will run asynchronously. Markingform_Load
asasync
would only be useful if you need to do something in response to the completion of the asynchronous method. In this case, there is nothing being done when it finishes.– Servy
Nov 15 '18 at 16:25
1
To fix the
label1.Text =...
issue, create a small function that sets the label and useControl.Invoke
to invoke that function on the thread that owns the control (remember that the Form class is a sub-class of the control). There are lots of examples on how to do that.– Flydog57
Nov 15 '18 at 17:08