1 0 Archive | Programming RSS feed for this section
post icon

Spawned process locks folder (and how to fix)

I recently had a problem where I was starting a process in another folder to update the calling program and the folder was locked. Here is what I was trying to do:

  1. Main program extracts update files to temp directory.
  2. Main program starts update program and closes.
  3. Update program deletes main program directory.
  4. Update program extracts updated main program directory.
  5. Update program starts updated main program and closes.

My problem was that when trying to delete the main program directory the folder was locked. Using Unlocker, it showed my update program having the lock. If I started the update program manually the lock wasn’t there. So I figured the lock was some side effect of me starting the update program from the main program.

Here is how I was starting the update program:

Dim p As New ProcessStartInfo
p.FileName = Other.UserTempPath & "\IBSi Updater\IBSi Updater.exe"
p.Arguments = """" & Application.StartupPath & """"
Process.Start(p)

And here is the simple solution on how to fix it:

Dim p As New ProcessStartInfo
p.FileName = Other.UserTempPath & "\IBSi Updater\IBSi Updater.exe"
p.Arguments = """" & Application.StartupPath & """"
p.WorkingDirectory = Other.UserTempPath & "\IBSi Updater"
Process.Start(p)

By making sure to set the WorkingDirectory, we tell the thread where it is and it locks it automatically so you don’t delete the executable that is running the thread. By not setting it, it takes the location of the thread that called it which in this case was not what I wanted.

Read full story »
post icon

Project Euler – Problem 1

This will be the first of my analysis of the Project Euler problems. It will contain spoilers and code. If you don’t wish to spoil the fun then don’t read.

Introduction

“Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.” – Project Euler website

I’m attempting to go through these one at a time and offer my reasoning behind my answers as well as my code. I haven’t gone through all of them so this is a continuing process.

Problem

“If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.” – Project Euler website

Solution

This sounds remarkably like the FizzBuzz problem. And accordingly the solution is fairly easy.

Basically, we are going to use the modulus operator to determine if the number is divisable by 3 or 5. If it is we add it to a list of numbers. Once we’re done we add up all the numbers in our list. This gives the answer of 233,168. It takes my machine about 15 milliseconds to run the program.

Code (VB 2008)

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim stopwatch As New Stopwatch

        stopwatch.Start()

        Dim numbers As New List(Of Int64)

        For counter As Int64 = 1 To 999

            If counter Mod 3 = 0 Or counter Mod 5 = 0 Then

                numbers.Add(counter)

            End If

        Next

        Dim sum As Int64

        For Each number As Int64 In numbers

            sum += number

        Next

        Debug.WriteLine(sum)

        stopwatch.Stop()

        Debug.WriteLine("Took " & stopwatch.ElapsedMilliseconds & " milliseconds.")

        stopwatch.Reset()

    End Sub

End Class

Download

You can download the “Project Euler – Problem 1″ code here. You can use VB 2008 Express to open it.

Read full story »