我可以在Windows窗体应用程序中创建异步函数吗? [英] Can I Create Asynchronous Functions In Windows Forms Application ?

查看:108
本文介绍了我可以在Windows窗体应用程序中创建异步函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在Windows窗体应用程序中创建异步函数吗?

想想我有一个Windows应用程序,点击该按钮就有一个按钮,它会从一个excel到一个excel进行一些数据迁移网站(只是想想,这不是实际要求)所以首先我需要选择excel然后点击上传按钮。所以应用程序将开始将数据迁移到site.But同时我想选择另一个excel然后执行相同的操作。用户应该能够选择n个excel并查看每个请求的状态(按钮单击)他做了。即使正在运行excel迁移,用户也应该能够选择其他应用程序操作。 Excel迁移应该在后台进行。

怎么做?

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 ?

推荐答案

是的,有很多方法可以做到这一点。您正在寻找的短语是线程。



最简单的入门方法是使用BackgroundWorker对象:



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:

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();







这很容易实现。



如果您希望从另一个线程到运行表单的主线程的进度报告,则会出现更多复杂情况。例如,有多个线程试图访问相同的变量,危险在于。



有几种方法可以解决几乎所有这些问题(我在看着你,Thread.Abort> _<<)。我不会详细说明,因为我不知道你需要多详细。只需发布一个新问题来解决这些复杂问题



祝你好运:)




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 :)


这篇关于我可以在Windows窗体应用程序中创建异步函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆