Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / June 2007

Tip: Looking for answers? Try searching our database.

Threading Design Question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Salim - 20 Jun 2007 19:01 GMT
Hello All,
I am working on introducing threading to my single threaded application to
take advantage of these multi-core processors.

I qurery for user data from file share and Active Directory with using two
methods

UserData data = GetFromFileShare(userName);
if(data == null)
{
    data = GetFromAD(userName);
}
I want to do these two opertions in parallel to reduce the time for overall
operation incase data is in AD (which is 85% most of the time but we have tp
consider File share since file share data gets priority)

1. Execute two threads for these two operations.
2. Wait for File share method to finish. If it returns result, do not wait
for AD thread to finish and return.
3. If FileShare returns null, wait for AD to finish and return AD result.
4. If AD also returns null, throw UserDataNotFoundException

Can you experts provide me a code snippet to do this in .Net 2.0?

Thanks a million for this,
Peter Duniho - 20 Jun 2007 19:15 GMT
> [...]
> I want to do these two opertions in parallel to reduce the time for  
[quoted text clipped - 11 lines]
>
> Can you experts provide me a code snippet to do this in .Net 2.0?

Maybe something like this (uncompiled, untested):

    UserData dataFileShare, dataAD, data;
    Thread threadFileShare, threadAD;

    threadFileShare = new Thread(new ThreadStart(delegate()  
{ dataFileShare = GetFromFileShare(userName); }));
    threadAD = new Thread(new ThreadStart(delegate() { dataAD =  
GetFromAD(userName); }));

    threadFileShare.Start();
    threadAD.Start();

    threadFileShare.Join();

    if (dataFileShare != null)
    {
        data = dataFileShare;
    }
    else
    {
        threadAD.Join();
        if (dataAD == null)
        {
            throw new UserDataNotFoundException();
        }
        data = dataAD;
    }

Note that I'm still getting used to the anonymous delegate syntax and rely  
on the compiler to tell me what exactly I need to do.  The above might not  
compile right off the bat, but if not hopefully the compiler error will  
give you good direction.  I think the basic idea is correct.

Pete
Salim - 20 Jun 2007 21:05 GMT
Thanks Peter. This is awesome. I wasn't sure how to wait for next thread but
you made my life easy. Thanks a milliton for this. I just added one small
line to abort AD thread if file share already returned.

threadFileShare.Join();

           if (dataFileShare != null)
           {
               data = dataFileShare;
               if (threadAD.IsAlive)
               {
                   threadAD.Abort();//Abort AD thread since we dont need
this data so why to spend resources on it

               }
           }            }

Do you think its a good enhancmeent and will not cuse any trouble?
I did limited testing and it works fine.

Salim

> > [...]
> > I want to do these two opertions in parallel to reduce the time for  
[quoted text clipped - 47 lines]
>
> Pete
Chris Mullins [MVP] - 20 Jun 2007 21:15 GMT
> I just added one small line to abort AD thread if file
> share already returned.
>                    threadAD.Abort();//Abort AD thread since we dont need
> this data so why to spend resources on it

Aborting the thread like that will cause all sorts of problems. These
problems may introduce major instability into your application.

You're better off just letting the thread finish and go away. Aborting it is
much worse than letting it suck up a few AD resources.

Signature

Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

Peter Duniho - 20 Jun 2007 21:34 GMT
> [...]
> Do you think its a good enhancmeent and will not cuse any trouble?
> I did limited testing and it works fine.

As Chris says, aborting the thread is a very bad idea.

If you had complete control over all of the code executed within the  
thread, then _maybe_ you could get away with aborting the thread, even  
though doing so would still be poor design and likely to create  
hard-to-track-down bugs.

But given that you are calling external code, you definitely should _not_  
abort the thread.  You have no way to ensure that the external code can  
deal with this gracefully, and it's really not going to hurt anything to  
allow the query to go on without you.  You clearly can afford the  
processing in the general case, and so you should easily be able to afford  
it in all cases.

Pete

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.