Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an argument to the EventHandler

Tags:

c#

No it's not that kind of basic question. I am doing a application and got a scenerio like, the file will be downloaded then it will be uploaded to the FTP server, then the local copy will be deleted, then one entry will be placed in a dictionary for that filename. So, the code is below

public void download_This_WebPage(string url, string cookies_String, string local_Saving_File_Name_With_Path)
{
    WebClient wb = new WebClient();
    wb.Headers.Add(HttpRequestHeader.Cookie, cookies_String);
    // Below I want to pass this local_File _Path to the event handler
    wb.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(wb,);
    wb.DownloadFileAsync(new Uri(url), local_Saving_File_Name_With_Path + ".html");
}

public void data_Download_Completed(Object sender, System.ComponentModel.AsyncCompletedEventArgs args)
{
      //use the file name to upload the file to FTP

}

public FTP_Completed
{
    // Delete the file
}

But, I dont know how to pass that filename to the event handler of download_Completed. Can anybody guide me in this

EDIT: Thank you for the answers from "Darin" and "Frederic". Is there any general way to pass the custom data to the (already defined)event handler like below

void main_Fn()
{
    string my_Data = "Data";
    some_object a = new some_object();
    some_Object.click_event += new eventHandler(click_Happened);
    (Assume that the event passes two ints, I also want to pass the string "my_Data"
      to "click_Happened")
    some_object.start();
}

void click_Happened(int a, int b)
{
   // I want to get the string "my_Data" here. 
}

In short how to trick the signature?

like image 691
prabhakaran Avatar asked Jul 02 '12 09:07

prabhakaran


People also ask

How does EventHandler work?

Each key node has a handler that receives key events when the key has focus. The handler responds to the key-pressed and key-released events for the Enter key by changing the color of the key on the screen. The event is then consumed so that the keyboard node, which is the parent node, does not receive the event.

How can we pass an event handler to the child component?

To pass an onChange event handler to a child component in React: Define the event handler function in the parent component. Pass it as a prop to the child component, e.g. <Child handleChange={handleChange} /> . Set it to the onChange prop on the input field in the child.

Can the arguments be passed on to the Reactjs event handlers directly or indirectly?

We cannot directly pass the argument to the event handler because, in that scenario, the event handler function would call automatically before you even pressed the button.

How do you pass arguments to an event handler?

If you want to pass a parameter to the click event handler you need to make use of the arrow function or bind the function. If you pass the argument directly the onClick function would be called automatically even before pressing the button.


2 Answers

You can pass the file name in the userToken argument to DownloadFileAsync(). When the operation finishes, it will be available in the UserState property of the AsyncCompletedEventArgs argument passed to data_Download_Completed():

string filename = local_Saving_File_Name_With_Path + ".html";
wb.DownloadFileAsync(new Uri(url), filename, filename);

Then:

public void data_Download_Completed(Object sender,
    System.ComponentModel.AsyncCompletedEventArgs args)
{
    string filename = (string) args.UserState;
    // Now do something with 'filename'...
}
like image 172
Frédéric Hamidi Avatar answered Oct 13 '22 23:10

Frédéric Hamidi


You could use the 3-rd argument of the DownloadFileAsync method which allows you to pass an UserState to the completed handler:

// subscribe to the completed event
wb.DownloadFileCompleted += data_Download_Completed;
string file = local_Saving_File_Name_With_Path + ".html";
wb.DownloadFileAsync(new Uri("url"), file, file);

and inside the handler:

public void data_Download_Completed(Object sender, AsyncCompletedEventArgs args)
{
    // extract the filename from the UserState of the args
    string file = args.UserState as string;
    ...

}
like image 20
Darin Dimitrov Avatar answered Oct 13 '22 21:10

Darin Dimitrov