Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Can i create asynchronous functions in windows forms application ?
Think like i have one windows application, there i have one button on click of that button it will do some migration of data from one excel to web site(just think, this is not the actual requirement) so first i need to select the excel then click on upload button. So the application will start to migrate the data in to site.But at the same time i want to select another excel then do the same.User should be able to select n number of excels and see the status of each request(button click) he made. User should be able to select other actions of application even though excel migration is running. Excel migration should happen in background.
How to do this ?
Posted
Updated 18-Apr-15 0:08am
v2

1 solution

Yes, and there are many ways of doing it. The phrase you're looking for is Threading.

The easiest way to get started is to user a BackgroundWorker object:

C#
string filesToUpload_etc = "files";

BackgroundWorker worker = new BackgroundWorker();
worker.DoWork+= delegate(object sender, DoWorkEventArgs args)
{
    foreach (string s in filesToUpload_etc.Split(''))
    {
        //do your thing
    }
};
worker.RunWorkerCompleted+= delegate(object sender, RunWorkerCompletedEventArgs args)
{
    //make something go "ping - done"
};

worker.RunWorkerAsync();



This is dead easy to implement.

There are more complications if you want a progress report from another thread to the main thread that runs your forms. Dangers lie in have multiple threads trying to access the same variable, for example.

There are ways to get around almost all of these issues (I'm looking at you, Thread.Abort >_<<). I won't go into detail as I don't know how detailed you need. Just post a new question to address any of these complexities

Good luck :)
 
Share this answer
 
Comments
Am Gayathri 17-Apr-15 13:29pm    
Thanks
Andy Lanng 17-Apr-15 13:45pm    
don't forget to accept the solution ^_^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900