Solved

Make application complete code in try block before proceeding?

Posted on 2008-06-17
6
198 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Authentication of Web Services 3 53
Get id from json Data with NewtonSoft 3 34
Two different visual studio versions 3 23
Where to download and how to install sqldmo.dll 5 34
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

13 Experts available now in Live!

Get 1:1 Help Now