Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Multithreading with task in .net 4. Strange things.

Multithreading with task in .net 4. Strange things.

Scheduled Pinned Locked Moved C#
questioncsharpdesignannouncementlounge
11 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F fory_cpp

    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?

    D Offline
    D Offline
    DaveyM69
    wrote on last edited by
    #2

    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)

    1 Reply Last reply
    0
    • F fory_cpp

      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?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #3

      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

      L A 2 Replies Last reply
      0
      • F fory_cpp

        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?

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #4

        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:

        B 1 Reply Last reply
        0
        • L Luc Pattyn

          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

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #5

          Correct Link[^] :)

          L 1 Reply Last reply
          0
          • L Lost User

            Correct Link[^] :)

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #6

            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.

            1 Reply Last reply
            0
            • L Luc Pattyn

              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

              A Offline
              A Offline
              Alan Balkany
              wrote on last edited by
              #7

              Good article!

              L 1 Reply Last reply
              0
              • A Alan Balkany

                Good article!

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #8

                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.

                1 Reply Last reply
                0
                • F fory_cpp

                  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?

                  D Offline
                  D Offline
                  darkelv
                  wrote on last edited by
                  #9

                  I have experienced that it may work without invoke on one machine but it doesn't work on other machines (and took a long time trying to find that out with the assumption that part was not the problem). So it's better to use invoke regardless.

                  1 Reply Last reply
                  0
                  • M Mark Salsbery

                    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:

                    B Offline
                    B Offline
                    BobJanova
                    wrote on last edited by
                    #10

                    For some controls it will work correctly ... most of the time. This is why .Net now catches them and throws and exception, because it is inconsistent and very difficult to catch otherwise.

                    1 Reply Last reply
                    0
                    • F fory_cpp

                      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?

                      W Offline
                      W Offline
                      wanghao2979
                      wrote on last edited by
                      #11

                      you can try delegate

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups