[C#]Parallel Looping

07/01/2012 23:01 Naworia#1
I want a function that gives me looping parallelize.

Example :

Code:
While(a == 1)
{
  Send("{ENTER}");
}

While(b == 3)
{
  Send("{TAB}");
}
I want these 2 loops working for parallel. How can i do? If you know other solve, please say.
07/01/2012 23:02 xNopex#2
Code:
Loop 1
  Send("{ENTER}");
  Send("{TAB}");
EndLoop
???
07/01/2012 23:07 Naworia#3
I edited for you get understand more ...
07/01/2012 23:13 qkuh#4
Quote:
while(a == 1 && b == 3)
{
Send("{ENTER}");
Send("{TAB}");
}
?
07/01/2012 23:15 k1u1337#5
Hey,

Ist eigentlich ganz einfach du musst beide While Schleifen verschmelzen
07/01/2012 23:15 xNopex#6
Code:
while(a==1 || b==3)
{
    if(a==1)
    {
        Send("{ENTER}");
    }
    if(b==3)
    {
        Send("{TAB}");
    }
}
???
07/01/2012 23:19 Naworia#7
I don't want that in same loop. I want parallel.
Example:


Code:
While(a == 1)
{
  Send("{ENTER}");
  Thread.Sleep(1000);
}

While(b == 3)
{
  Send("{TAB}");
  Thread.Sleep(15000);
}
As you saw, first loop must wait 1 sec, but second loop must wait 15 sec.
07/01/2012 23:58 MrSm!th#8
#moved

@Topic: Use threads.
07/02/2012 00:00 Jeoni#9
/Edit: MrSmith said it first :D
Use multithreading.
Code:
Void InitializeLoops()
{
Thread Loop1Thread = new Thread(new ThreadStart(LoopFunc1));
Thread Loop2Thread = new Thread(new ThreadStart(LoopFunc2));

Loop1Thread.Start();
Loop2Thread.Start();
}

void LoopFunc1()
{
While(a == 1)
{
  Send("{ENTER}");
  Thread.Sleep(1000);
}
}

void LoopFunc2()
{
While(b == 3)
{
  Send("{TAB}");
  Thread.Sleep(15000);
}
}
I don't know if it's working this way, but you can give it a try. Maybe there are mistakes, because I made this out of my head.
07/02/2012 00:23 Naworia#10
Thank you for your both replies. I used them but i can't create a "Thread" for timer1_Tick.
It says an error about "No overload".
I sued this code :
Code:
Thread Loop1Thread = new Thread(new ThreadStart(timer1_Tick));
07/02/2012 02:36 Mashkin#11
Quote:
Originally Posted by Naworia View Post
Thank you for your both replies. I used them but i can't create a "Thread" for timer1_Tick.
It says an error about "No overload".
I sued this code :
Code:
Thread Loop1Thread = new Thread(new ThreadStart(timer1_Tick));
Please give more details on errors next time.

I guess the error occurs because the Tick event handlers commonly take two arguments:
Code:
void Timer1_Tick(Object sender, EventArgs e);
There is no candidate taking no arguments.

You will have to pass all parameters for the threaded method in one object (when calling the Start() method). However, you can also overload the Timer1_Tick method to handle no arguments (or one object which could include an array of arguments).

Easiest way would be a method simply calling the Timer1_Tick method.

Code:
public void Timer1_Tick(Object sender, EventArgs e)
{
 ... your original Tick method
}

public void Call_Timer1_Tick()
{
    this.Timer1_Tick(null, null);
}
Watch out: untested. :D
07/02/2012 03:39 Naworia#12
Thank you man. i solved <3