?
Solved

How to use Threading in C# to execute three Functions at the same time

Posted on 2005-04-18
7
Medium Priority
?
758 Views
Last Modified: 2008-02-07
I have the following problem:

I have three functions that all return DataRows that by using WebClient download information from three different web servers, I would like the three functions to run at the same time and then when I have all the data back to carry on as normal, In a nutshell this is what I have :

public class book_search_results : System.Web.UI.Page
{


   private void Page_Load(object sender, System.EventArgs e)
     {

// do various other things

         Search mySearch = new Search();  //this calls a class called Search that contains function for going off an grabbing content from various websites

                DataRow drb = ds.Tables["results"].NewRow();
      drb = mySearch.searchBlackwells(txtSearch,drb);
      
      DataRow drbf = ds.Tables["results"].NewRow();
      drbf = mySearch.searchBookFellas(txtSearch,drbf);
      
      DataRow drcm = ds.Tables["results"].NewRow();
      drcm = mySearch.searchCompman(txtSearch,drcm);

// These are the functions that go off and grab content
// I need them to all run at the same time and return the data as they get it,
// at the moment they run concurrently

         
     }

}

That is about it, i've looked in to threading but can't seem to get hold of a decent example when you deal with functions that return information

All help greatly appreciated.

Al

0
Comment
Question by:alaistair
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 9

Expert Comment

by:cyberdevil67
ID: 13807305
Hi alaistair,

 Can't browse the page for the exact link

 but try this www.codeproject.com/csharp/ and select threads or threading there is great examples on this website

Cheers!
0
 
LVL 8

Expert Comment

by:vinhnl
ID: 13807433
That good ideal. You can redure exe time in using thread when search.

public class book_search_results : System.Web.UI.Page
{

   private  Search mySearch = new Search();

   private void searchBlackwells()
   {
             DataRow drb = ds.Tables["results"].NewRow();
             drb = mySearch.searchBlackwells(txtSearch,drb);
   }

   private void searchBookFellas()
   {
             DataRow drb = ds.Tables["results"].NewRow();
             drb = mySearch.searchBookFellas(txtSearch,drb);
   }

   private void searchCompman()
   {
             DataRow drb = ds.Tables["results"].NewRow();
             drb = mySearch.searchCompman(txtSearch,drb);
   }

 
   private void Page_Load(object sender, System.EventArgs e)
  {
//
      System.Threading.Thread thread1 = new System.Threading.Thread(searchBlackwells);
      thread1.Priority = System.Threading.ThreadPriority.Lowest;
      thread1.Start();

      System.Threading.Thread thread2 = new System.Threading.Thread(searchBookFellas);
      thread2.Priority = System.Threading.ThreadPriority.Lowest;
      thread2.Start();

      System.Threading.Thread thread3 = new System.Threading.Thread(searchCompman);
      thread3.Priority = System.Threading.ThreadPriority.Lowest;
      thread3.Start();

      while(!thread1.Join(10) || !thread2.Join(10) || !thread3.Join(10));
//
   }
}

Good luck
VINHNL
0
 
LVL 21

Expert Comment

by:Yurich
ID: 13810622
syntax for creating a thread is a bit different to my understanding:

using System.Threading;
...

// you can't directly spcify a calling function, should be withing ThreadStart
Thread thread1 = new Thread( new ThreadStart( searchBlackwells ));

i don't really think you want to set lowest priority for your trheads either

and it will work just fine without while block

regards
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:alaistair
ID: 13819591
Thanks vinhnl, i'll have a go at that and see what happens,

I think the while statement is there to make sure all threads have finished before we carry on?

Al
0
 
LVL 8

Accepted Solution

by:
vinhnl earned 1500 total points
ID: 13820568
That right! I think you can terminate the thread before timeout:

DateTime starttime = DateTime.Now;
TimeSpan during;
 while(!thread1.Join(10) || !thread2.Join(10) || !thread3.Join(10))
{
    during = DateTime.Now - starttime;
    if(during.TotalSeconds > 60)
    {
        if(thread1.ThreadState != System.Threading.ThreadState.Stopped)
            thread1.Interrupt();

        if(thread2.ThreadState != System.Threading.ThreadState.Stopped)
            thread2.Interrupt();

        if(thread3.ThreadState != System.Threading.ThreadState.Stopped)
            thread3.Interrupt();

        break;
    }
}
0
 
LVL 1

Author Comment

by:alaistair
ID: 13865975
Thanks guys,  Main answer from vinhnl was good and assisted by Yurich (if someone can tell me how to set part of the answer as 'Assisted' please let me know as I want to try to give Yurich some points to)

Thanks,

Al
0
 
LVL 21

Expert Comment

by:Yurich
ID: 13866147
You could click "Split Points" right above the text box you're typing comments in, then you'll have an option to select which one is accepted and put some fractions into another answers :)

regards
0

Featured Post

Technology Partners: 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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

862 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