0

I have three threads in my program, one function and 5 .txt files

The function randomly select a file and writes something in it

now my problem is to detect that which thread is use which file

I want show in a rich textbox something like this:

thread one add in 2.txt

thread three add in 1.txt

thread three add in 1.txt

thread three add in 1.txt

thread three add in 5.txt

thread two add in 3.txt

....

Edit

I use Thread.CurrentThread.ManagedThreadId.ToString() like this:

case 4:
                        if (c4 >= 5)
                        {
                            c44 = true;
                            test();
                        }
                        else
                        {
                            this.Invoke(new MethodInvoker(delegate()
                            {
                                richTextBox1.Text += "\n\n4--->" + Thread.CurrentThread.ManagedThreadId.ToString() + "\n\n";
                            }));
                            writefile(t4);
                        }
                        c4++;
                        test();
                        break;

case 4 means my thread is writting in 4.txt

1 Answers1

2

using Thread.currentThread you can tell which thread is running the function

string temp = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString();
ZoomVirus
  • 615
  • 7
  • 22