如何实现多线程以使我的winform应用程序更快? [英] How to implement multithreading in order to make my winform app faster ?

查看:390
本文介绍了如何实现多线程以使我的winform应用程序更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有winform visual studio 2012.

i希望实现多线程,以便让我的应用程序更快

这就是我想要做的:

i有2个方法sr_created和sr_solved。我想在仪表板表单出现后同时在线程中执行这些方法。

和sr_created以及sr_solved方法需要在启动之前执行两个方法global和d_child(sr_created和sr_solved)方法需要输出global和d_child方法来执行它们。

i想要让我的应用程序在打开时更快,所以我不希望这些方法全局和d_child成为在我的应用程序加载时执行。



i已经尝试过,没有错误但是它没有工作



< b>我尝试了什么:



hi guys,i have winform visual studio 2012.
i want to implement multithreading in order to make my app faster
this is what i want to do:
i have 2 methods sr_created and sr_solved. i want these methods to be executed in threads simultaneously after the dashboard form appears.
and sr_created and sr_solved methods require both 2 methods "global" and "d_child" to be executed before they start (sr_created and sr_solved methods need output of "global" and "d_child" methods for their execution)
i want to make my app faster when it opens so i don't want these methods "global" and "d_child"to be executed while my app loads.

i have tried, there is no error but it's not working

What I have tried:

private void overview_Load(object sender, EventArgs e)
        {
           //threading
            Thread obj_srcr = new Thread(new ThreadStart(SrCreated));//for sr_created
            Thread obj_srsv = new Thread(new ThreadStart(SrSolved));//for sr_solved
            obj_srcr.Start();
            obj_srsv.Start();

           
        }

//method Sr_created
 void SrCreated()
        {
            Global(); //call Global method
            ChildAccess();//call d_child method
          //all the code of Sr_created()
}

//method sr_solved
  void SrSolved()
        {
            Global(); //call Global method
            ChildAccess();//call d_child method
           //all the code of Sr_solved()
       }

//method Global
void Global()
{
  //all the code of Global()
}

//method  d_child

void ChildAccess()
{
  //all the code of d_child
}

推荐答案

使用任务查看更现代的方式:

任务类(System.Threading.Tasks) [ ^ ]



这里有一些关于CodeProject上的任务的有趣文章:

通过C#实现任务并行的基础知识 [ ^ ]

任务并行库:n中的1个 [ ^ ]
Take a look at the more modern way using Tasks:
Task Class (System.Threading.Tasks)[^]

Here are some interesting articles about Tasks on CodeProject:
The Basics of Task Parallelism via C#[^]
Task Parallel Library: 1 of n[^]


首先,如果你想要延迟任何这个计算,直到表单显示后,你应该触发显示事件的处理程序中的计算,而不是加载事件。

(请参阅: Form.Shown Event( System.Windows.Forms) [ ^ ])



接下来,你没有回答我之前关于是否或不是全局 ChildAccess 方法的结果由 sr_created sr_solved 方法,或者如果它们需要由 sr_created 独立调用sr_solved

我将假设结果是共享的,并且 sr_created sr_solved 全球 ChildAccess 完成之前不应执行。



Als o,你还没有说过在所有这些计算都在运行时UI是否应该阻塞(我会假设没有)以及在计算 sr_created sr_solved 已经完成(我假设是)。



所以尝试这样的事情:

(注意:未经测试!编码脱离我的头脑(通过一些研究)。)

First, if you want to delay any of this computation until after the form is displayed, you should trigger that computation in the handler for the Shown event instead of the Load event.
(See: Form.Shown Event (System.Windows.Forms)[^])

Next, you didn't answer my earlier question about whether or not the results of the Global and ChildAccess methods are shared by the sr_created and sr_solved methods, or if they need to be called independently by sr_created and sr_solved.
I'm going to assume the results are shared and that sr_created and sr_solved should not be executed until Global and ChildAccess have completed.

Also, you haven't said if the UI should block while all of this computation is running (I'll assume no) and if there is something that should happen when the computation of sr_created and sr_solved have completed (I'll assume yes).

So try something like this:
(Note: untested! Coded "off the top of my head" (with some research).)
private void overview_Shown(object sender, EventArgs e)
{
  ComputationChain().ContinueWith(DoAfterComputationCompleted);
}

private Task ComputationChain()
{
  return PreworkComputation().ContinueWith(_ =>
      Task.WaitAll(
        Task.Run((Action)SrCreated),
        Task.Run((Action)SrSolved)));
}

// if Global and ChildAccess can be parallelized:
private Task PreworkComputation()
{
  return Task.WhenAll(
    Task.Run((Action)Global),
    Task.Run((Action)ChildAccess));
}
// if Global and ChildAccess must be run in sequence:
private Task PreworkComputation()
{
  return Task.Run(() => {
      Global();
      ChildAccess();
    });
}
 
//method Sr_created
void SrCreated()
{
  // Global(); //call Global method
  // ChildAccess();//call d_child method
  //all the code of Sr_created()
}
 
//method sr_solved
void SrSolved()
{
  // Global(); //call Global method
  // ChildAccess();//call d_child method
  //all the code of Sr_solved()
}
 
//method Global
void Global()
{
  //all the code of Global()
}
 
//method d_child

void ChildAccess()
{
  //all the code of d_child
}

private void DoAfterComputationCompleted(Task _)
{
  // ignore the Task parameter _
  // "this" should be the instance of the Form
  this.Invoke(() => {
      // the .Invoke(...) gets this to be done on the UI thread
      // here you perform the UI work after the computation has completed
    });
}


这篇关于如何实现多线程以使我的winform应用程序更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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