Click here to Skip to main content
11,925,101 members (55,515 online)
Click here to Skip to main content
Add your own
alternative version


58 bookmarked

A Multi-threading Example

, 26 Sep 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
An Example Showing How to Run Multiple Codes Concurrently and Prevent Cross-thread Operation Errors Using C#
Screenshot - project.jpg


You might have wanted to execute several codes concurrently on Windows Forms, but you may encountered the Cross-Thread Operation error or faced your application in a dead mood. There are several ways of preventing the application from hanging. Here, I just want to show a simple way to beginners of how it is easy to achieve such a goal in a very straightforward way.

A couple of months ago, I wanted to start ProgressBar while at the same time doing something else within my application. So, the idea of multi-threading came up. Surely you have to separate the tasks using thread philosophy and also get the application away from hanging. I found some useful articles on the net that allowed me to complete my task successfully. Now I just want to disclose this little experience with those who have the same problem.

Here are some helpful links that I used:

Into the Code

First of all, we define a thread variable in the body of the class. We want it to be global because of further changing on it.

using System.Threading; 
public Thread thread = null; //assign a default value Delete 


Here is the main part for performing our aim. This is contrived in the Delete button. As you see below, first we have defined the thread variable as a global one. For stopping and starting this thread later on, the pause/continue button is used. In our sample in the Delete button, we really don't delete files in the path browsed by the user. It is just a demo for depicting something like that (concurrent code execution is far more important).

private void Btn_Delete_Click(object sender,EventArgs e)
  if(textBox1.Text ==String.Empty)
     MessageBox.Show("Please specify a path");
     Btn_Pause.Enabled     = true;
     Btn_Stop.Enabled      = true;
     Btn_Delete.Enabled    = false;

     thread = new Thread(delegate()
       //Divide thread into several parts.You can execute multiple code 
       //within a thread back to back:

       //Part 1:

//for(int i=0;i<files.Count;Interlocked.Increment(ref i))-for 
//understanding atomic operation see <a href=""> </a>        
        for(int i = 0; i < files.Count; i++)
          //accessing any member out of this thread for changing it 
          //here causes Cross_Thread Opertaion Error.
          //this.Text ="something";//---->Cross_Thread Operation Error(Do 
          //not put here)

              this.Text = files[i].ToString().Substring(files[i].LastIndexOf(
                  '\\') + 1);
              //you can delete file by yourself.  
              this.label2.Text = Convert.ToString(i + 1);               
          //Part 2:
              this.Btn_Pause.Enabled = false;
              this.Btn_Stop.Enabled  = false;
          //Part 3:
              this.ShowMessageDelegate("100% (Process Compeletd)");
//Implement other codes here for performing them at the time the progressbar 
//is being processed .
 MessageBox.Show("Am being displayed while progressbar is being performed....");


Screenshot - pause.jpg

The Pause button also acts as a Continue button for continuing and pausing the thread.

private void Btn_Pause_Click(object sender,EventArgs e)
  if(thread ==null || !thread.IsAlive)
     MessageBox.Show("Process has not been started or just being finished");
  if(Btn_Pause.Text.ToUpper() == "pause".ToUpper())
     Btn_Pause.Text = "Continue";
     Btn_Pause.Text = "Pause";


For stopping a thread, you have to check whether this thread is being suspended or not. If so, make it resume and then abort it. Here, we also implement the rolling back of the progress bar in a new thread.

private void Btn_Stop_Click(object sender,EventArgs e)
     if(thread.ThreadState ==ThreadState.Suspended)


     new Thread(delegate()
         int val = this.progressBar1.Value;  //--->Don't make mistake,you 
                                             //can read but you can not change 
                                             //if you put any code that wants 
                                             //to modify an object other than 
                                             //this thread you'll see 
          for(int i = 1; i <= val; Interlocked.Increment(ref i))
     label2.Text = "PROCESS HAS BEEN ABORETD";

     Btn_Pause.Enabled      =false;
     Btn_Delete.Enabled     =false;
     Btn_Pause.Text         ="Pause";
     Btn_Stop.Enabled       =false;

Points of Interest

When I made the progress bar run, I noticed that I couldn't drag the form because it hung. So, I began multi-threading for that aim. I used this code for accessing the objects in the same Windows Form by a new thread. When I first created the new thread and then wanted to call a void function for starting some tasks associated with the objects in my form, I got error the error, "Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on."

Of course, even in this way if you use objects (write on/modify an object) other than the objects in the new thread — or you have created within the main thread of the application — you will again get this error. You have to use it under the this.BeginInvoke((ThreadStart)delegate(){//implement your logic here...}); part and make the whole new thread start.


All in all, as I mentioned in the introduction, there would be several ways to do this. I used this way because I think it is easy to understand the relevant conceptions and is also very straightforward. By the way, I'd like to ask you to be lenient with my article because it is my first article on the Code Project.


  • 26 September, 2007 -- Original version posted


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Pooya Musavi
Software Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionShowing Exception while I click on Stop button Pin
Rahul saraswat22-May-14 20:29
memberRahul saraswat22-May-14 20:29 
GeneralMy vote of 3 Pin
surinder55523-Nov-13 0:42
groupsurinder55523-Nov-13 0:42 
QuestionQuestion Pin
Mirko Jurcevic13-Nov-13 3:01
memberMirko Jurcevic13-Nov-13 3:01 
GeneralMy vote of 2 Pin
aGISer30-Apr-13 0:46
memberaGISer30-Apr-13 0:46 
BugCross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on Pin
aGISer25-Apr-13 4:33
memberaGISer25-Apr-13 4:33 
GeneralRe: Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on Pin
aGISer28-Apr-13 2:58
memberaGISer28-Apr-13 2:58 
Generalan small mistake Pin
wdnanji14-Apr-08 4:31
memberwdnanji14-Apr-08 4:31 
GeneralRe: an small mistake Pin
Pooya Musavi14-Apr-08 21:17
memberPooya Musavi14-Apr-08 21:17 
GeneralRe: an small mistake Pin
aGISer28-Apr-13 3:02
memberaGISer28-Apr-13 3:02 
GeneralRe: an small mistake Pin
aGISer28-Apr-13 3:24
memberaGISer28-Apr-13 3:24 
GeneralCoding for "Pause" and "Cancel" Button Pin
nuweima26-Oct-07 0:36
membernuweima26-Oct-07 0:36 
GeneralRe: Coding for "Pause" and "Cancel" Button Pin
Pooya Musavi26-Oct-07 4:28
memberPooya Musavi26-Oct-07 4:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151125.3 | Last Updated 26 Sep 2007
Article Copyright 2007 by Pooya Musavi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid