Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more: , +
there are three methods called in a backgroundworker_dowork() event in c# .net 4. I want to execute three methods synchronously. that is, 2nd method will execute after 1st method is fully executed and 3rd method will execute when 2nd method is fully executed. because 2nd method can exetue properly only when 1st method is fully executed. but in the background worker all three methods are executed asynchronously that i don't want.

How to solve it?

Thanks in advance.
Posted
Comments
Sergey Alexandrovich Kryukov 29-Feb-12 20:11pm    
Why? why?!
--SA

This isn't described very well.

But, you're saying that all three methods are executing at the same time?? Congratulations! You wrote your code to launch three different BackgroundWorkers or spawned your own threads to do it!

All you would have to do is create ONE BackgroundWorker and have it execute a method like this:
Private Sub StartWork()
    Method1()
    Method2()
    Method3()
End Sub

Each method will get executed in order and each will wait for the previous method to finish execution before the next one starts.
 
Share this answer
 
Comments
Espen Harlinn 29-Feb-12 17:28pm    
5'ed!
Sergey Alexandrovich Kryukov 29-Feb-12 20:10pm    
Right, my 5. I tried to explain the wrongfulness of OP's thinking using the notion of race condition, please see.
--SA
In addition to Solution 1 and Solution 3.

Of course, you can always change the priority of a thread:
http://msdn.microsoft.com/en-us/library/system.threading.thread.priority.aspx[^].

You can also change the priority of a process. The thing is: in almost all cases, you should not do it. You hardly can help anything but you can easily make things bad or very bad.

You are thinking in wrong direction: trying to affect functionality by the timing. You should do directly opposite: the functionality should never depend on time delays in any threads. In fact, there is a direction in testing which does exactly that: seeds different delays in threads in systematic ways and tests that it does not affect functionality. If such tolerance is not achieved, this is considered to be a bug (and a damn bad one), called race condition.

When I learn it many years ago, I knew it under more defining name: "incorrect dependency on the time of execution". Please see:
http://en.wikipedia.org/wiki/Race_condition[^].

—SA
 
Share this answer
 
Comments
Wonde Tadesse 29-Feb-12 21:27pm    
5+
Sergey Alexandrovich Kryukov 29-Feb-12 21:28pm    
Thank you, Wonde.
--SA
Put them all in the same thread.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Feb-12 20:11pm    
Simple, isn't it? my 5. I tried to explain the wrongfulness of OP's thinking using the notion of race condition, please see.
--SA
Wonde Tadesse 29-Feb-12 21:24pm    
Yap. No need to have multithreadinng! This pure sequential execution of methods.
Thank you your response.

Now I am trying to make the situation clear.

C#
private void bgwLoading_DoWork(object sender, DoWorkEventArgs e)
  {

      ArrayList a = (ArrayList)e.Argument;

      string[] fileNames = (string[])a[0];
      bool isLoad = (bool)a[1];

      this.loadMultiImages(fileNames, isLoad);
  }

 private void loadMultiImages(string[] fileNames, bool isLoad)

 {

  // I want to execute the following codes sequentially.

       Bitmap newBtmap = saveJpeg();
       this.SafeInvoke(d => d.imageList.Images.Add(newBtmap));

 }


here, safeInvoke() takes less time than saveJpeg() method and safeInvoke() starts executing before the saveJpeg() is fully executed. That is flow of the execution is switching between these two methods. That I don't want. I want to complete the execution of saveJpeg() before safeInvoke() start to execute.
 
Share this answer
 
I don't fully understand your question. If you - for example - dont want the third method to execute on a background thread, only call methods 1 and 2 in the DoWork event handler. Than return result indicating the success of the operation via the DoWorkEventArgs. In the WorkCompleted event handler check the success of the operation and if necessary invoke the third method.
 
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