?
Solved

Make application complete code in try block before proceeding?

Posted on 2008-06-17
6
Medium Priority
?
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 6

Accepted Solution

by:
Bruce_1975 earned 750 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 750 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

770 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