C
C#3y ago
Shunrai

Communication between variables in Multiple Threads

Hello, I am kinda new to this and I've never really worked with multi threads, decided to finally learn but I can't figure out how I can read the variables of one thread in the other.
C#
void Kekw1()
{
while (true)
{
string image1 = (@"C:\rumblered7.PNG");
string[] results1 = UseImageSearch(image1, "10");
if (results1 == null)
{
MessageBox.Show("null value bro, sad day");
}
else
{
int bananaX1 = Int32.Parse(results1[1]);
int bananaY1 = Int32.Parse(results1[2]);
MessageBox.Show("Banana Results:" + bananaX1 + ", " + bananaY1);
}
}
}
void Kekw2()
{
while (true)
{
string image2 = (@"C:\rumblered2.PNG");
string[] results2 = UseImageSearch(image2, "15");
if (results2 == null)
{
MessageBox.Show("null value bro, sad day");
}
else
{
int bananaX2 = Int32.Parse(results2[1]);
int bananaY2 = Int32.Parse(results2[2]);
MessageBox.Show("Banana Results 2:" + bananaX2 + ", " + bananaY2);
}
}
}
C#
void Kekw1()
{
while (true)
{
string image1 = (@"C:\rumblered7.PNG");
string[] results1 = UseImageSearch(image1, "10");
if (results1 == null)
{
MessageBox.Show("null value bro, sad day");
}
else
{
int bananaX1 = Int32.Parse(results1[1]);
int bananaY1 = Int32.Parse(results1[2]);
MessageBox.Show("Banana Results:" + bananaX1 + ", " + bananaY1);
}
}
}
void Kekw2()
{
while (true)
{
string image2 = (@"C:\rumblered2.PNG");
string[] results2 = UseImageSearch(image2, "15");
if (results2 == null)
{
MessageBox.Show("null value bro, sad day");
}
else
{
int bananaX2 = Int32.Parse(results2[1]);
int bananaY2 = Int32.Parse(results2[2]);
MessageBox.Show("Banana Results 2:" + bananaX2 + ", " + bananaY2);
}
}
}
4 Replies
Korbah
Korbah3y ago
declare the variable outside of the functions, in the enclosing class. You may have to worry about locking for thread safety, I honestly don't know much about that on single variables
Shunrai
ShunraiOP3y ago
Could you please provide me with an example
Korbah
Korbah3y ago
I'm on mobile now so my example is not great, but above void Kekw1, you could place string[] results1; Then in Kekw1, you would just say results1 = UseImageSearch(...) instead of declaring it each time unless I'm misunderstanding :) It's a little hard to see what's going on here with this limited portion - I'm also not sure what you exactly want from these 2
Shunrai
ShunraiOP3y ago
Testing right now to see if I understood DogeKek seem to work my G, though im prob doing smth wrong
Want results from more Discord servers?
Add your server