PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, December 11, 2022

[FIXED] How to call Start() method of multiple thread class instances in one statement?

 December 11, 2022     c#, delegates, multithreading, syntax, tuples     No comments   

Issue

Need syntax help, is there any way of calling the same method of multiple instances in one statement, like the way we assign values to multiple variables?

var t1 = new Thread(SayHello);
var t2 = new Thread(SayBye);
var t3 = new Thread(SayGoodbye);

(t1.Name, t2.Name, t3.Name) = ("Thread1", "Thread2", "Thread3");

// any way to call t1,t2, and t3's Start Method in one statement like above
t1.Start();
t2.Start();
t3.Start();

Solution

You can't call the Start() method of multiple objects in one statement as you would assign the same value to multiple variables in one statement: The Start() method is an element within each object, not the same piece of code attached to each one. The closest you're going to be able to come is in IamK's comment

(new List<Thread> { t1, t2, t3 }).ForEach(_=> _.Start());

which is a valid construct, as opposed to the merely conceptual one I thought up

(t1, t2, t3).Start();


Answered By - FKEinternet
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing