线程的按钮点击方法 [英] Threading for methods on button click

查看:163
本文介绍了线程的按钮点击方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我呼吁一个按钮点击几个方法。

I am calling few methods on a button click.

泛函()

functionB()

functionB()

functionC()

functionC()

这三个功能是相互独立的,他们需要很长时间来执行。我查了一下发现,通过线程我可以运行所有三个在一起,这将节省的执行时间。

All three functions are independent from each other and they take long time to execute. I checked and found that by threading I can run all three together which will save the execution time.

由于我是新线程的概念,任何人都可以请指导我,我可以做的方案或其他方式线程,这将是在这种情况下很有用最简单的方法。

As I am new to threading concept, could anyone please guide me the simplest way I can do threading in scenario or other way which will be useful in this scenario.

修改

在同一个函数还有一个问题:

One more problem in the same function:

我的三个功能执行后绑定5 GridView的。像这样

I am binding 5 gridviews after the three functions execution. Like this

            gv1.DataSource = GetData("Mill");
            gv1.DataBind();

            gv2.DataSource = GetData("Factory");
            gv2.DataBind();

            gv3.DataSource = GetData("Garage");
            gv3.DataBind();

            gv4.DataSource = GetData("Master");
            gv4.DataBind();

他们都正在使用得到的结果同样的方法,他们也需要时间来加载。有没有什么办法可以运行它们并行呢?我害怕,因为他们使用同样的方法来获取数据。是否有可能为他们做穿线。怎么样?

They all are using the same method for getting the result and they are also taking time to load. Is there any way I can run them parallel too? I afraid, because they are using same method to get the data. Is it possible to do threading for them. How ?

推荐答案

我不知道Parallel.Invoke()如何决定哪些并行执行,但如果你想保证,他们将在并行执行,使用线程:

I am not sure how Parallel.Invoke() decides what to execute in parallel, but if you want an assurance that they will execute in parallel, use threads:

var t1 = new Thread(MySlowFunction);
t1.IsBackground = true;
t1.Start();

var t2 = new Thread(MySlowFunction);
t2.IsBackground = true;
t2.Start();

# To resync after completion:
t1.Join();
t2.Join();

甚至更好,使用线程池:

Or even better, use the ThreadPool:

  ThreadPool.QueueUserWorkItem(MyWork);

记住要处理你的线程异常。

Remember to handle your thread exceptions.

这篇关于线程的按钮点击方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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