Multithreading with task in .net 4. Strange things.
-
Hi guys, First of all - I am not very experienced. I have the following strage situation: Task t = null; t = Task.Factory.StartNew(() => { while (true) { // do some calculations LogToUi("some result"); Thread.Sleep(15000); } }); LogToUi is a function that updates a textbox in the UI. It does it pretty straightforward tbUpdateMe.Text = passedtoLogToUiText; This works just fine, it also works if I use a listbox instead of textbox, but If I would like to change LogToUi to update a datagridview - it does not work. I was told that in general what I have written should not work.... But it does... So why? A friend of mine told me that even with textbox it should not work... But it does... So what is the situation and can I expect that it will work with TextBox but not very stable or what?
-
Hi guys, First of all - I am not very experienced. I have the following strage situation: Task t = null; t = Task.Factory.StartNew(() => { while (true) { // do some calculations LogToUi("some result"); Thread.Sleep(15000); } }); LogToUi is a function that updates a textbox in the UI. It does it pretty straightforward tbUpdateMe.Text = passedtoLogToUiText; This works just fine, it also works if I use a listbox instead of textbox, but If I would like to change LogToUi to update a datagridview - it does not work. I was told that in general what I have written should not work.... But it does... So why? A friend of mine told me that even with textbox it should not work... But it does... So what is the situation and can I expect that it will work with TextBox but not very stable or what?
So long as your
LogToUi
method is invoking to the UI thread then any control can be updated successfully. If not, you will end up with illegal cross thread exceptions, or your app may crash (eventually!) if you have these turned off.Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Hi guys, First of all - I am not very experienced. I have the following strage situation: Task t = null; t = Task.Factory.StartNew(() => { while (true) { // do some calculations LogToUi("some result"); Thread.Sleep(15000); } }); LogToUi is a function that updates a textbox in the UI. It does it pretty straightforward tbUpdateMe.Text = passedtoLogToUiText; This works just fine, it also works if I use a listbox instead of textbox, but If I would like to change LogToUi to update a datagridview - it does not work. I was told that in general what I have written should not work.... But it does... So why? A friend of mine told me that even with textbox it should not work... But it does... So what is the situation and can I expect that it will work with TextBox but not very stable or what?
Invoke is required, here[^] is more about it. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
modified on Tuesday, May 10, 2011 5:56 PM
-
Hi guys, First of all - I am not very experienced. I have the following strage situation: Task t = null; t = Task.Factory.StartNew(() => { while (true) { // do some calculations LogToUi("some result"); Thread.Sleep(15000); } }); LogToUi is a function that updates a textbox in the UI. It does it pretty straightforward tbUpdateMe.Text = passedtoLogToUiText; This works just fine, it also works if I use a listbox instead of textbox, but If I would like to change LogToUi to update a datagridview - it does not work. I was told that in general what I have written should not work.... But it does... So why? A friend of mine told me that even with textbox it should not work... But it does... So what is the situation and can I expect that it will work with TextBox but not very stable or what?
Yeah, I'm surprised it ever works like that. Maybe you get lucky every once in a while and the task is scheduled on the UI thread...
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Invoke is required, here[^] is more about it. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
modified on Tuesday, May 10, 2011 5:56 PM
-
Correct Link[^] :)
Thanks. I accidentally provided the link to my local copy... Fixed it. :thumbsup:
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Invoke is required, here[^] is more about it. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
modified on Tuesday, May 10, 2011 5:56 PM
Good article!
-
Good article!
Thanks Alan. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Hi guys, First of all - I am not very experienced. I have the following strage situation: Task t = null; t = Task.Factory.StartNew(() => { while (true) { // do some calculations LogToUi("some result"); Thread.Sleep(15000); } }); LogToUi is a function that updates a textbox in the UI. It does it pretty straightforward tbUpdateMe.Text = passedtoLogToUiText; This works just fine, it also works if I use a listbox instead of textbox, but If I would like to change LogToUi to update a datagridview - it does not work. I was told that in general what I have written should not work.... But it does... So why? A friend of mine told me that even with textbox it should not work... But it does... So what is the situation and can I expect that it will work with TextBox but not very stable or what?
-
Yeah, I'm surprised it ever works like that. Maybe you get lucky every once in a while and the task is scheduled on the UI thread...
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Hi guys, First of all - I am not very experienced. I have the following strage situation: Task t = null; t = Task.Factory.StartNew(() => { while (true) { // do some calculations LogToUi("some result"); Thread.Sleep(15000); } }); LogToUi is a function that updates a textbox in the UI. It does it pretty straightforward tbUpdateMe.Text = passedtoLogToUiText; This works just fine, it also works if I use a listbox instead of textbox, but If I would like to change LogToUi to update a datagridview - it does not work. I was told that in general what I have written should not work.... But it does... So why? A friend of mine told me that even with textbox it should not work... But it does... So what is the situation and can I expect that it will work with TextBox but not very stable or what?
you can try delegate