Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 C# - Threading Example
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
octopi




PostPosted: Sat Mar 17, 2007 3:54 pm   Post subject: C# - Threading Example

In gui applications it is important to make sure you do processor extensive calculations(work) in a different thread, this allows you to still let the user interact with your program while doing the work(otherwise the gui would be unresponsive/slow).

The attached file contains a project that shows how to do threading.

code:

        Thread thread_clock = new Thread(new ThreadStart(Thread_Clock));
        thread_clock.IsBackground = true;
        thread_clock.Start();


Thread_Clock is the name of the function (I guess they're called methods really?) to call.
IsBackground = true; tells the program to automatically end this thread when the last gui thread is ended.(when the main window is closed)
Start(); starts the thread.

code:

        void Thread_Clock() {
                while(true) {
                        clock_seconds +=1;
                        Update_Label_Seconds(clock_seconds.ToString());
                        Thread.Sleep(1000);
                }
        }

Notice the same name "Thread_Clock"
Notice the while loop, if you were to do this in a regular program it would slow it down alot.
Thread.Sleep(); tells the thread to goto sleep for the specified number of milliseconds.


code:

        delegate void StringParameterDelegate (string value);
               
        public void Update_Label_Seconds(string value) {
                if (InvokeRequired) {
                        BeginInvoke(new StringParameterDelegate(Update_Label_Seconds), new object[]{value});
                        return;
                }
                label_seconds.Text= value + " seconds";
        }


The above allows us to access stuff in the gui thread. If you try to access stuff in the gui thread without doing it this way, it won't work, and probally won't even give you an error message....or one you'll understand.
Basically what happens is, it checks if its in the gui thread, if it isn't (InvokeRequired=true), then it tells the program to switch to the gui thread, and run this again. The next time we are now in the gui thread, and we can access stuff in it. (labels, buttons, etc..)

The StringParameterDelegate line just allows us to send variables to the Update_Label_Seconds function.
If I wanted to pass an int I would make one that looked like this:
code:

delegate void IntParameterDelegate (int value);

and change the Update_Label_Seconds function like this:

code:

        public void Update_Label_Seconds(int value) {
                if (InvokeRequired) {
                        BeginInvoke(new IntParameterDelegate(Update_Label_Seconds), new object[]{value});
                        return;
                }
                label_seconds.Text= value.toString() + " seconds";
        }



If anyone has any questions feel free to ask, I know I don't explain things too well...



ExThread.zip
 Description:
Project that shows how to make threads that can access things running in another thread(GUI thread)

Download
 Filename:  ExThread.zip
 Filesize:  30.02 KB
 Downloaded:  1822 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: