Posts

Showing posts from November, 2007

Passing parameters to Threads

Often when you start a thread, you want to give it some parameters - usually some data it has to process, a queue to wait on, etc. The ThreadStart delegate doesn't take any parameters, so the information has to be stored somewhere else if you are going to create a new thread yourself. Typically, this means creating a new instance of a class, and using that instance to store the information. Often the class itself can contain the delegate used for starting the thread. For example, we might have a program which needs to fetch the contents of various URLs, and wants to do it in the background. You could write code like this:
publicclass UrlFetcher
{
string url ;

public UrlFetcher (string url)
{
this.url = url;
}

publicvoid Fetch()
{
// use url here
}
}

[... in a different class ...]

UrlFetcher fetcher = new UrlFetcher (myUrl);
new Thread (new ThreadStart (fetcher.Fetch)).Start();


In some cases, you actually just wish to call a method in some class (possibly the currently executing class) with a spec…

ASP.net Passing Parameters in Button Click EventHandler

The Parameters can be passed by ASP.Net buttons by using CommandName and CommandArgument. Below is the code that demonstrates that how to pass Arguments using Asp.Net button in its event Handler. Just Specify CommandName and CommandArgument in the button and set the OnClick Event to your specified function.

publicvoid ButtonEventHandler(Object sender, EventArgs e)
{
//Cast sender to Button
Button oButton = (Button)sender;
//Check for the appropriate arguments

switch (oButton.CommandName)
{
case "SelectData":
//Below would be your Logic to SelectData
SelectData(oButton.CommandArgument.ToString());
break;
case "SaveData":
//Below would be your Logic to SaveData
SaveData(oButton.CommandArgument.ToString());
break;
case "DeleteData":
//Below would be your Logic to DeleteData
DeleteData(oButton.CommandArgument.ToString());
break;
}
}

Its very Simple by this way you can Pass parameters using button an…