Solved

Make application complete code in try block before proceeding?

Posted on 2008-06-17
6
200 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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 our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

861 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