Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

thread callback advice needed 1

Status
Not open for further replies.

misterstick

Programmer
Apr 7, 2000
633
GB
how can i let the parent process of a thead created using a delegate that the thread has finished?

i have a set of web services which, on startup, must be compiled by the server. this takes 20-odd seconds to do, and i'd like this to be done by a splash screen which gives some feedback to the user.

my thought is to have a constructor on the splash screen which takes an array of delegates, each of which is called on a background thread.

when all the background threads have finished, the splash screen should unload itself.

all the above works fine, except that using new Thread(ThreadStart(passedDelegate)) i can't see a way of attaching code to a (currently non-existent) OnEnded() event.

Code:
[COLOR=blue]public delegate void[/color] [COLOR=#2B919F]VoidDelegate[/color]();

[COLOR=green]// test strap form[/color]
[COLOR=blue]public class[/color] [COLOR=#2B919F]Form1[/color] : [COLOR=#2B919F]Form[/color]
{
 [COLOR=green]// one for each WebService, currently 11[/color]
 [COLOR=blue]private void[/color] Method1()
 {
  [COLOR=green]// wakes up service for this process,[/color]
  [COLOR=green]// takes twenty seconds the first time, [/color]
  [COLOR=green]// less than a tenth of a second thereafter[/color]
  [COLOR=blue]using[/color] ( [COLOR=#2B919F]WebProxy[/color] proxy = [COLOR=blue]new[/color] [COLOR=#2B919F]WebProxy[/color]() )
  {
  }
 }
 [COLOR=blue]private void[/color] button1_Click()
 {
  [COLOR=#2B919F]VoidDelegate[/color][] delegates = { Method1, ... };
  [COLOR=blue]using[/color] ( [COLOR=#2B919F]Form2[/color] form2 = [COLOR=blue]new[/color] [COLOR=#2B919F]Form2[/color](delegates) )
  {
    form2.ShowDialog(this);
  }
 }
}

[COLOR=blue]public class[/color] [COLOR=#2B919F]Form2[/color] : [COLOR=#2B919F]Form[/color]
{
 [COLOR=blue]public[/color] [COLOR=#2B919F]Form2[/color]() : [COLOR=blue]this[/color]([COLOR=blue]null[/color])
 {
 }
 [COLOR=blue]public[/color] [COLOR=#2B919F]Form2[/color]([COLOR=#2B919F]VoidDelegate[/color][] passedDelegates)
 {
  [COLOR=blue]if[/color] ( passedDelegates != [COLOR=blue]null[/color] )
  {
   progressBar1.Maximum = passedDelegates.Length;
   [COLOR=blue]foreach[/color] ( [COLOR=#2B919F]VoidDelegate[/color] item [COLOR=blue]in[/color] passedDelegates )
   {
    [COLOR=blue]new[/color] [COLOR=#2B919F]Thread[/color]([COLOR=blue]new[/color] [COLOR=#2B919F]ThreadStart[/color](item)).Start();
    progressBar1.Value++;
   }
  }
 }
 [COLOR=green]// i'd like the progress bar to count down again[/color]
 [COLOR=green]// as the threads finish, and close the form when[/color]
 [COLOR=green]// there's none left... any ideas?[/color]
}

mr s. <;)

 
Calling a method in a separate class is probably the way to go here. This is a quick example I wrote off the top of my head...


public class SplashScreenLoader
{
public event EventHandler Finished;

public SplashScreenLoader()
{
}

public void DoWork()
{
//do Work here

this.Finished(this, EventArgs.Empty);
}
}




btnLaunch_Click()
{
SplashScreenLoader ssl = new SplashScreenLoader();

ssl.Finished += new EventHandler(ssl_Finished);

Thread t = new Thread(new ThreadStart(ssl.DoWork));

t.Name = "SplashScreenThread";

t.Start();
}

private void ssl_Finished(object sender, EventArgs e)
{
if (this.InvokeRequired)
{
//reinvoke this method using this.Invoke(...)
}
else
{
progressbar1.Value += 5;
}
}
 
why thank you very much.
just couldn't see it.
star for you.


mr s. <;)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top