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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Simple Threading (VS 2005) Example

Simple Threading (VS 2005) Example

Scheduled Pinned Locked Moved C#
csharpvisual-studiohelptutorial
8 Posts 6 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.
  • W Offline
    W Offline
    WinSolution
    wrote on last edited by
    #1

    Dear All I want a simple threading example in VS 2005 C#. When i run the project it should display a label with yellow background and increases it width 1px witn 1000ms interval. I have googled it out but they are using delegates & some are using timer but i dont want to use both. Any kind of help will be appreciated. Thanks.

    J P L 3 Replies Last reply
    0
    • W WinSolution

      Dear All I want a simple threading example in VS 2005 C#. When i run the project it should display a label with yellow background and increases it width 1px witn 1000ms interval. I have googled it out but they are using delegates & some are using timer but i dont want to use both. Any kind of help will be appreciated. Thanks.

      J Offline
      J Offline
      J a a n s
      wrote on last edited by
      #2

      I don't think that you can do this without the help of delegates. You wont be able to modify a control created in one thread from another. Check the following links: Invoke Required[^] http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx[^]

      WinSolution wrote:

      I want a simple threading example in VS 2005 C#

      You have to that do it yourself. If you are finding any issues post it here and we can help with it.

      W 1 Reply Last reply
      0
      • W WinSolution

        Dear All I want a simple threading example in VS 2005 C#. When i run the project it should display a label with yellow background and increases it width 1px witn 1000ms interval. I have googled it out but they are using delegates & some are using timer but i dont want to use both. Any kind of help will be appreciated. Thanks.

        P Offline
        P Offline
        prasadbuddhika
        wrote on last edited by
        #3

        this can be done without using a delegate if you are doing the label updating through the thread where the label is created(means in the main form thread). if you use another thread to increase the value and the label is in the main thread you have to use the delegate. because cross thread operations are not allowed .

        W 1 Reply Last reply
        0
        • J J a a n s

          I don't think that you can do this without the help of delegates. You wont be able to modify a control created in one thread from another. Check the following links: Invoke Required[^] http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx[^]

          WinSolution wrote:

          I want a simple threading example in VS 2005 C#

          You have to that do it yourself. If you are finding any issues post it here and we can help with it.

          W Offline
          W Offline
          WinSolution
          wrote on last edited by
          #4

          Yes i have tried and i found Cross Thread Error thats why i tried here if there is any way. Basically i have shifted from Java to CSharp as i am working with C# last 8 months but i haven't learnt some new concept of c#. I was good in Java's Threading as wished if there is another way. Thanks.

          1 Reply Last reply
          0
          • P prasadbuddhika

            this can be done without using a delegate if you are doing the label updating through the thread where the label is created(means in the main form thread). if you use another thread to increase the value and the label is in the main thread you have to use the delegate. because cross thread operations are not allowed .

            W Offline
            W Offline
            WinSolution
            wrote on last edited by
            #5

            Following is the code which i tried but got error of Cross Thread. public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { ThreadStart ts = new ThreadStart(ProgressBar); Thread th = new Thread(ts); th.Start(); } public void ProgressBar() { label1.Width += 1; Thread.Sleep(1000); } } Please share if you can figure out the problem

            A 1 Reply Last reply
            0
            • W WinSolution

              Following is the code which i tried but got error of Cross Thread. public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { ThreadStart ts = new ThreadStart(ProgressBar); Thread th = new Thread(ts); th.Start(); } public void ProgressBar() { label1.Width += 1; Thread.Sleep(1000); } } Please share if you can figure out the problem

              A Offline
              A Offline
              ABitSmart
              wrote on last edited by
              #6

              Reiterating what everyone has already mentioned. You cannot change UI state from a *non-UI* thread. ProgressBar is running on a non-UI thread. Use, BeginInvoke. Read the links already shared to you or see here[^]

              C 1 Reply Last reply
              0
              • A ABitSmart

                Reiterating what everyone has already mentioned. You cannot change UI state from a *non-UI* thread. ProgressBar is running on a non-UI thread. Use, BeginInvoke. Read the links already shared to you or see here[^]

                C Offline
                C Offline
                Cracked Down
                wrote on last edited by
                #7

                Use Control.Invoke method to update controls on main thread.........

                1 Reply Last reply
                0
                • W WinSolution

                  Dear All I want a simple threading example in VS 2005 C#. When i run the project it should display a label with yellow background and increases it width 1px witn 1000ms interval. I have googled it out but they are using delegates & some are using timer but i dont want to use both. Any kind of help will be appreciated. Thanks.

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

                  Hi, if all you want the thread to do is move a Label, then you don't need a thread at all. Just use a Windows.Forms.Timer, that is a timer that ticks on the GUI thread, hence you can have the Tick handler manipulate Controls without any InvokeRequired/Invoke (as long as it is fast, otherwise your GUI would freeze). :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                  modified on Sunday, June 12, 2011 8:37 AM

                  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