Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
1.75/5 (4 votes)
See more:
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:

C#
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
}
Posted
Updated 2-Jun-16 14:34pm
v3
Comments
Richard Deeming 2-Jun-16 13:04pm    
"It's not working" is not enough information for anyone to help you.

Click "Improve question" and update your question with a proper description of the problem. Include the full details of any errors you're getting, and remember to indicate which line of code they relate to.
Sergey Alexandrovich Kryukov 2-Jun-16 13:33pm    
Perhaps the whole question is based on some misconception. Multithreading, especially in UI, is not used to make something "faster". It is for keeping things parallel, where required, and for keeping UI responsive at all times. Can you see that these are totally different things? Maybe you understand it all correctly but did not just formulate the problems properly.

I would advice to focus on the formulation of the problem first. Try not to contaminate your description of goals/requirements with you ideas on how things are implemented.

—SA
Matt T Heffron 2-Jun-16 13:40pm    
In addition to giving more information as Richard suggested,
consider:
Are the SrCreated and SrSolved totally independent?
Are they compute-bound or IO-bound?
Are there any resources that they must share in a manner that allows for updates?
Do they each need their own results of Global() and ChildAccess()? or do they share the results (which is kind of implied by part of your question about start-up)?
Are they large enough (memory-wise) that switching between the threads will affect caching?
Just putting things in multiple threads is not necessarily going to make it faster?
Armel_Djient 2-Jun-16 14:59pm    
i figure out the term used is not appropriated. i want to execute thre methods i described in parallel using threading. i request u to do not focus on the tittle given(it seems to be not correct) but on what i want to do. yes SrCreated and srSolved are independent. both only require output of golbal and dchild methods. How can i execute these methods in parell after the dashboard form appears?

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[^]
 
Share this answer
 
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).)
C#
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
    });
}
 
Share this answer
 

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