void bw_DoWork(object sender, DoWorkEventArgs e)
 {
     while (points.Count > 0)
     {
         currentpoint++;
         int delta=-1*(points.Count-initialcount)+1;
         this.threadstatus=" - (1/3) Building point to point distance matrix from Mapquest ("+currentpoint+" of "+initialcount+")";
         PointD tofind=points.Dequeue();
         Map.Directions directions = new Map.Directions(GlobalVar.sqlsettings.mapquestkey);
         List <double> latitude=new List<double>();
         List <double> longitude=new List<double>();
         latitude.Add(tofind.RX);
         longitude.Add(tofind.RY);
         latitude.Add(tofind.CX);
         longitude.Add(tofind.CY);
         DirectionData result=directions.GetDirections(latitude,longitude,false);
         this.distancematrix[tofind.RZ][tofind.CZ] = result.time[0];
     }
 }
        void bw3_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                Mapquest mapquest = new Mapquest(GlobalVar.sqlsettings.mapquestkey);
                Map.Directions directions = new Map.Directions(GlobalVar.sqlsettings.mapquestkey);

                DirectionArgs args = (DirectionArgs)e.Argument;
                for (int i = 0; i < args.latitude.Count; i++)
                {
                    if (args.latitude[i] == 0 && args.longitude[i] == 0)
                    {
                        GeoData geodata = mapquest.GetLocation(args.address[i]);
                        if (geodata == null)
                        {
                            args.counter = -1;
                            break;
                        }
                        args.latitude[i] = geodata.latitude[0];
                        args.longitude[i] = geodata.longitude[0];
                    }
                }
                /*for (int i = 1; i<args.latitude.Count-1; i++)
                {
                    for(int j=1;j<args.latitude.Count-1;j++)
                    {
                        if (args.longitude[j] == args.longitude[i] && args.latitude[j] == args.latitude[i]&&i!=j)
                        {
                            args.longitude.RemoveAt(j);
                            args.latitude.RemoveAt(j);
                            args.address.RemoveAt(j);
                            j = 1;
                        }
                    }
                }*/
                    if (args.counter != -1)
                    {
                        DirectionData directiondata = directions.GetDirections(args.latitude, args.longitude, args.optimize);
                        args.directiondata = directiondata;
                    }
                e.Result = args;
            }
            catch { }
        }
        void bw2_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                Mapquest mapquest = new Mapquest(GlobalVar.sqlsettings.mapquestkey);
                Map.Directions directions = new Map.Directions(GlobalVar.sqlsettings.mapquestkey);

                DirectionArgs args = (DirectionArgs)e.Argument;
                for (int i = 0; i < args.latitude.Count; i++)
                {
                    for (int j = 0; j < args.latitude[i].Count; j++)
                    {
                        if (args.latitude[i][j] == 0 && args.longitude[i][j] == 0)
                        {
                            GeoData geodata = mapquest.GetLocation(args.address[i][j]);
                            if (geodata == null)
                            {
                                args.counter = -1;
                                break;
                            }
                            args.latitude[i][j] = geodata.latitude[0];
                            args.longitude[i][j] = geodata.longitude[0];

                                viewinvoices[i].latitude = (double)geodata.latitude[0];
                                viewinvoices[i].longitude = (double)geodata.longitude[0];
                                mysql_invoices.UpdateInvoice(viewinvoices[i], viewinvoices[i]);

                        }
                    }
                }
                if (args.counter != -1)
                {
                    for (int i = 0; i < args.latitude.Count;i++ )
                    {
                        DirectionData directiondata = directions.GetDirections(args.latitude[i], args.longitude[i], args.optimize);
                        args.directiondata.Add(directiondata);
                    }
                }
                e.Result = args;
            }
            catch { }
        }
        void bw3_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = e.Argument;
            distances = new List<int>();
            for (int i = 0; i < optimizedroutes.Count; i++)
            {
                currentstatus = " - (3/3) Querying Mapquest for optimal delivery route order ("+(i+1)+" of "+(optimizedroutes.Count)+")";
                List<double> latitude = new List<Double>();
                List<double> longitude = new List<Double>();
                latitude.Add(GlobalVar.sqlsettings.RRSlatitude);
                longitude.Add(GlobalVar.sqlsettings.RRSlongitude);
                for (int j = 0; j < optimizedroutes[i].Count; j++)
                {
                    for (int k = 0; k < viewinvoices.Count; k++)
                    {
                        if (optimizedroutes[i][j].invoicenumber == viewinvoices[k].number)
                        {
                            latitude.Add(viewinvoices[k].latitude);
                            longitude.Add(viewinvoices[k].longitude);
                        }
                    }

                }
                latitude.Add(GlobalVar.sqlsettings.RRSlatitude);
                longitude.Add(GlobalVar.sqlsettings.RRSlongitude);
                Map.Directions directions = new Map.Directions(GlobalVar.sqlsettings.mapquestkey);

                DirectionData directiondata = directions.GetDirections(latitude, longitude, true);
                distances.Add((int)directiondata.distance.Sum());

                for (int j = 0; j < optimizedroutes[i].Count; j++)
                {
                    for (int k = 1; k < directiondata.locationSequence.Count - 1; k++)
                    {
                        if (directiondata.locationSequence[k] == j + 1)
                        {
                            optimizedroutes[i][j].stop = k - 1;
                        }
                    }
                }

            }
        }