-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
124 lines (99 loc) · 3.83 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
using System.Net.Http;
using Newtonsoft.Json.Linq;
using Nito.AsyncEx;
using System.Runtime.InteropServices;
using System.Net;
namespace BulkIpGeoLocator
{
class Program
{
const int CONCURRENT_TASKS = 5000;
const string workbookPath = "E:\\src\\BulkIpGeoLocator\\20170925.xlsx";
private static Excel.Application excelApp;
private static Excel.Workbook excelWorkbook;
private static Excel.Sheets excelSheets;
static async Task<int> MainAsync(string[] args)
{
GeoLocator geo = new GeoLocator();
string currentSheet = "Sheet4";
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
//int x = 26720;
int x = 0;
try
{
while (x < excelWorksheet.Rows.Count)
{
var tasks = new List<Task>();
// tasks.Add(geo.RunGeoLocator(excelWorksheet, x, "B"));
for (int y = 0; y < CONCURRENT_TASKS; y++)
{
try
{
tasks.Add(geo.RunGeoLocator(excelWorksheet, x + y, "B"));
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
await Task.WhenAll(tasks);
excelWorkbook.Save();
//Parallel.Invoke(tasks);
x += CONCURRENT_TASKS;
}
return x;
}
catch (Exception ex)
{
return x;
}
finally
{
if (excelWorkbook != null) excelWorkbook.Close(0);
excelApp.Quit();
}
}
static void Main(string[] args)
{
excelApp = new Excel.Application();
excelWorkbook = excelApp.Workbooks.Open(workbookPath,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
excelSheets = excelWorkbook.Worksheets;
int rowsProcessed = 0;
var sheet = excelWorkbook.Worksheets["20170925"];
int rowCount = sheet.Cells.Find("*", System.Reflection.Missing.Value,
System.Reflection.Missing.Value, System.Reflection.Missing.Value,
Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlPrevious,
false, System.Reflection.Missing.Value, System.Reflection.Missing.Value).Row;
try
{
System.Net.ServicePointManager.DefaultConnectionLimit = 10;
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
rowsProcessed = AsyncContext.Run(() => MainAsync(args));
}
catch (Exception ex)
{
Console.Error.WriteLine(ex);
//return -1;
}
finally
{
//if (excelWorkbook != null) excelWorkbook.Close();
excelApp.Quit();
//excelWorkbook = null;
//excelApp = null;
Marshal.ReleaseComObject(excelSheets);
Marshal.ReleaseComObject(excelWorkbook);
Marshal.ReleaseComObject(excelApp);
}
Console.ReadKey();
}
}
}