Click here to Skip to main content
16,015,756 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to use thread to press more than one key at same time (inner loop) and after one second press repeat this task.

I use this code but I found some problem if I use join the first thread not press at same time but if not use press at same time but all thread start.
C#
private void play()
        {
            
            for (int i = 0; i < 2; i++)
            {
                Thread th=null;
                for (int j = 0; j < 2;j++ )
                {
                    
                   SubNote sub= ArrayPlayNote[i].NotesSameTime[j];
             
                   ThreadStart starter = delegate { MessageSub(sub); };
                  th = new Thread(starter);
                
                   th.Start();
                    
                }
                th.Join();
             
            }

        }
///////////////////////////////////////////////////////
Keyboard ky = new Keyboard();
        private void MessageSub( SubNote sub)
        {
            ky.pianoControl1.PressPianoKey(sub.KeyNum);
            Thread.Sleep(1000);
            ky.pianoControl1.ReleasePianoKey(sub.KeyNum);
          
        }


Thanks for help me.
Posted
Updated 24-Nov-11 23:54pm
v2
Comments
RaisKazi 25-Nov-11 5:55am    
Edited: 1) Formatting 2) Added "pre" tag.
Paulo Zemek 25-Nov-11 7:36am    
I didn't understand what you are really asking.
But you are starting two threads and you are only joining to the last one. And why use a thread to join it? Call the method directly... I really can't help because I can't understand what you are really trying to do.
shadiabusamra 25-Nov-11 8:19am    
i use join to prevent start next threads (outer loop) before previous threads finish

This is because the general idea is such a big misuse of threads. Everything looks wrong and beyond repair. Maybe if you explain the requirement, someone or myself would advise you a simple solution. The problem does not look very difficult.

[EDIT]
About passing parameters through thread: see my solutions:
How to pass ref parameter to the thread[^],
change paramters of thread (producer) after it started[^].

—SA
 
Share this answer
 
v2
Comments
shadiabusamra 25-Nov-11 6:06am    
In 2D array i need to start thread for all element first row in same time after (1 second) start thread for all element in second row
valza 25-Nov-11 6:13am    
Does this thread something return? There are 4 ways to use threads. Give more info about your issue.
shadiabusamra 25-Nov-11 6:24am    
no return in thread but pass parameter through thread
Sergey Alexandrovich Kryukov 25-Nov-11 11:08am    
You see, you say "I want to do this and that with threads". This is not your ultimate goal. Start with explaining what your application should do. Wait a minite...
--SA
Sergey Alexandrovich Kryukov 25-Nov-11 11:09am    
For now, please see my update, after [EDIT].
--SA
this is the basic
C#
static void Main(string[] args)
       {
           for (int i = 0; i < 100; i++)
           {
               Thread t = new Thread(new ParameterizedThreadStart(MyMethod));
               t.Start(i); //pass in object
           }
           Console.ReadLine();
       }

       public static void MyMethod(object input)
       {
           //Cast object
           Console.WriteLine("Hello from MyMethod " + input.ToString());
       }


So you can pass in an object to thread. if you need to print in console from 1 to 99, not like now, you need to use ReaderWriterLock.
Ok.
If I am wrong tell me please someone because i am learning .NET.
 
Share this answer
 
Comments
Paulo Zemek 25-Nov-11 7:34am    
How a ReaderWriterLock will make values from 0 to 99 be ordered?
ReaderWriterLock is for protected a resource that is not thread-safe (so, many readers at once or only a single writer). But it does not order the threads... if one thread is faster than the other for some reason, they will be unordered. And Console is already thread-safe.
valza 25-Nov-11 8:22am    
Yes, sorry for my bad comment. You are right.
Thank Paul.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900