Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a function :

C#
static void fun1(string req)
      {
          while (true)
          {
              if (req == "read")
                  Console.WriteLine(req);
              else
                  Console.WriteLine(req);

          }
      }


to this function i want to pass an argument on runtime. This function will be mapped with a thread.

- This means that i will be passing data to the thread.
- I want to try out a scenario... on user input i want to pass an argument to the thread during runtime.
- How to achieve this? I tried out something like this:


Thread t = new Thread(() => fun1("hello"));

- I have used a lambda expression to pass data.
- Now i will start the thread:
t.Start();
- This thread now runs in the background. Now in the main function i will ask the user to enter some data lets say "Hey".
- So now the thread in the background should print "Hey" on the console on user input. So how to achieve this?
- Say for example in the main i say something like this :


console.writeline("Enter some thing");
string data = console.readline();

/////please note that the thread is running in the background///////
/////Now how to pass the data to the thread????

- I created an empty constructor of the class and started the thread.
- In the main i instantiate the constructor.
- Now the program hangs.


Thanks
Please guide.
Posted
Comments
Aydin Homay 21-Dec-13 6:39am    
Why you don`t use TPL library? Task.Factory can solve your problem easily

1 solution

That gets a lot harder than you think.
Why?
Partly because the code you show for your thread is really nasty: it will sit there outputting new lines to the COnsole as fast as it can, and causing your app to spend most of it's time allocating memory and scrolling the display...hence why your program hangs!

But mostly, because getting data between threads is fraught with problems, because therad are independent processes and you don't know what they are doing at any time from another thread: so if you are busy loading something into a variable in one thread, you don't know that the other is not half way through removing it. Just a simple thing like counting numbers up in one thread and reducing them in another becomes a source of horrible, horrible bugs.

If you really want to do this, then you need to start reading up first on designing for thread safe data transfer, but the simplest way is to involve the lock statement[^].
 
Share this answer
 
Comments
Rahul VB 21-Dec-13 12:39pm    
Thanks sir, actually i am developing a plc product for my client. I have 250 plc devices communicating with the server, now what i need to do is :
1) Ping all the PLCs on the network, now put only the pinged PLCs in a file.
2) Read the file and create threads to connect to those PLCs. Which means if 10 are pinged i will create 10 threads.
3)Now using those threads create sockets to connect to the PLC devices. Send request to independent devices to read their memory registers.
4) Now i will check the memory registers to see if any sensor event has been detected or not.
5) If so then send that data to the server unit to notify the guard that an event has occured and take care of the situation.

These steps have to repeated again and again. and this i have to achieve using threads. I have absolutely no knowledge of threads. So if you could give me some suggestions i would be grateful.

Thanks a lot.
Rahul VB 21-Dec-13 12:50pm    
sir,
I have also tried this in a form based application. I tried the above code which works fine, just instead of console.writeline i used message boxes. But i am sure that my application will collapse if i want to perform tasks which i just wrote above.Can you please refer me some books about the same.
Thanks a lot

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