• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

Make application complete code in try block before proceeding?

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
M_Lyons
Asked:
M_Lyons
2 Solutions
 
Bruce_1975Commented:
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
 
ZippitCommented:
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
 
CartoonHeadCommented:
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
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
M_LyonsAuthor Commented:
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
 
M_LyonsAuthor Commented:
Thanks for your help.
0
 
ZippitCommented:
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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now