Solved

Make application complete code in try block before proceeding?

Posted on 2008-06-17
6
197 Views
Last Modified: 2013-12-16
I have been working on this for a little bit, and I have a button click that goes through a series of processes.  My application seems to be running through the try blocks and processing the code concurrently, which isn't good in my instance.  I tried liberal uses of Application.DoEvents();, but this did not resolve the problem.  Is there a way that I can force my application to process the code in a try block before it runs the code in the next try block?  Applications that process updates or other series type instructions should do something similar to what I'm trying to achieve here...  If anyone needs anything from me, please let me know.  I just really want to get this working.  I'm open and appreciate ideas.

Thank you again.
0
Comment
Question by:M_Lyons
6 Comments
 
LVL 6

Accepted Solution

by:
Bruce_1975 earned 250 total points
ID: 21802007
Have a look at the lock() statement.
http://msdn.microsoft.com/en-us/library/c5kehkcz(VS.80).aspx
With a lock block you can prevent other threads from accessing data or sections while the locking thread is processing.

Regards,
Bruce
0
 
LVL 7

Expert Comment

by:Zippit
ID: 21802183
Are you explicitly creating threads and/or new processes in your button click event?  If so, if you want them to run one after another then you should get rid of the threading.  If you're not using threading then it should already be running sequentially.

DoEvents are typically used to simulate concurrency, so if you don't want concurrency you may want to remove them (this will make your interface non-responsive during the processing, but that is easily fixed once you get your code processing correctly).
0
 
LVL 1

Assisted Solution

by:CartoonHead
CartoonHead earned 250 total points
ID: 21806742
I have had a similar strange experience when not explicitly using multiple threads.
The lock() solution worked well for me. However, rather than lock(this) it is better practice to create an instance of an object to lock. Eg:

Object o = new Object();
lock(o)
{
  try {........}
}



0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:M_Lyons
ID: 21809572
Thank you very much.  I just tried using the lock idea as described, and it is still not waiting for the zip file to be created before trying to copy it and perform the cleanup...  It still isn't waiting for everything done in the try block to finish before moving on to the next thing...  I put each code snippet in its own lock block and kept everything separate...  I also tried putting the code in the backgroundWorkers in a lock block, but that didn't change anything either...  This is becoming a bit frustrating.  I don't understand why I can't get this to work...  I'm open to any ideas.  It sounds like using lock is the way to go, but once it hits the backgroundWorker items I again have the concurrency issues.  I need the backgroundWorkers, because otherwise the UI locks up as these are demanding processes...

Thanks Again for your help.  I hope this is able to get resolved.  If you have any other ideas, or something that I might be doing wrong, please let me know.

Thanks Again,
0
 

Author Closing Comment

by:M_Lyons
ID: 31467916
Thanks for your help.
0
 
LVL 7

Expert Comment

by:Zippit
ID: 21831375
Ok..you are using multi-threading (the backgroundWorker hides it, but that's what it is doing).

1 question, is your zip code running in a background worker too?

The locks are not going to help in this situation.  The code enters the lock, fires up the background worker, which in turn fires up a new thread to do the work then your code exits the lock, but the background worker is still running on the other thread.

Your best bet is to write a function that does everything you need sequentially.  So in your helper function have it create the zip file, then copy the file, then delete, cleanup etc.  No background workers.  Then in your main function (I presume it's a button click or a timer event) use a background worker to run your single helper function.  Or, just create your own thread and have it run the helper function.

Basically the problem is you are using different background workers for different actions.  You need each individual action to occur on the same thread/background worker.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now