Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Thread Example

Posted on 2006-11-25
10
Medium Priority
?
285 Views
Last Modified: 2010-04-16
Hi,

How to use thread in the C#,

Can someone give me small example on threads

Kind Regards,
0
Comment
Question by:dinesh_bali
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
10 Comments
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 18011260
public System.Threading.Thread Thread1 = new Thread(new ThreadStart(AlertIt_One));
public System.Threading.Thread Thread2 = new Thread(new ThreadStart(AlertIt_Two));

public void Button1_Click(object sender, EventArgs e)
{
Thread1.Start();
Thread2.Start();
}

public void AlertIt_One()
{
MessageBox("Fired 1");
Thread1.Abort();
}

public void AlertIt_Two()
{
MessageBox("Fired 2");
Thread2.Abort();
}
0
 

Author Comment

by:dinesh_bali
ID: 18011283
Hi Thanks for the example.

Can we commumicate between two threads

say thread 1 and thread 2, when the process of the thread 1 completes

Kind Regards
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 29

Expert Comment

by:Nightman
ID: 18011316
Yes - you can raise events between the threads.

Or you can simply have a timer in the main thread checking the state of the thread:
i.e.
if (
Thread1.ThreadState == ThreadState.Unstarted ||
Thread1.ThreadState == ThreadState.Stopped ||
Thread1.ThreadState == ThreadState.Aborted ||
Thread1.ThreadState == ThreadState.Suspended )
{
  This thread is dead.
}

Have a look at
http://www.developerfusion.co.uk/show/5184/3/ for some more examples
0
 
LVL 29

Expert Comment

by:Nightman
ID: 18011399
Sorry, I am working in too many languages at the same time - they are all starting to merge into one ;). That was a VB.NET example

have a look at 'Using events for thread synchronization'
http://www.codeproject.com/csharp/eventsthreadsync.asp
0
 
LVL 29

Accepted Solution

by:
Nightman earned 2000 total points
ID: 18011411
Another example:
http://www.codeproject.com/csharp/workerthread.asp

Both this one and my previous post have downloadable, working code samples
0
 
LVL 29

Expert Comment

by:Nightman
ID: 18198051
Hi dinesh_bali

Did our comments assist you? If not, please can you provide some feedback so that we can assist you further. Alternatively, have a look at http://www.experts-exchange.com/help.jsp#hs5 for information on closing questions.

Regards
Nightman
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question