Solved

Thread Example

Posted on 2006-11-25
10
279 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
  • 5
10 Comments
 
LVL 29

Expert Comment

by:Nightman
Comment Utility
0
 
LVL 20

Expert Comment

by:REA_ANDREW
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 29

Expert Comment

by:Nightman
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now