<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
public static string DbPath => DependencyService.Get<ISqLite>().GetDatabasePath("user.db");
#region License
// Разработано: Коротенко Владимиром Николаевичем (Vladimir N. Korotenko)
// email: koroten@ya.ru
// skype:vladimir-korotenko
// https://vkorotenko.ru
// Создано: 09.04.2019 22:38
#endregion
using System;
using System.IO;
using SpecialForcesDirectory.Code;
using SpecialForcesDirectory.Droid;
using Xamarin.Forms;
[assembly: Dependency(typeof(SQLite_Android))]
namespace SpecialForcesDirectory.Droid
{
public class SQLite_Android : ISqLite
{
public SQLite_Android() { }
public string GetDatabasePath(string sqliteFilename)
{
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var path = Path.Combine(documentsPath, sqliteFilename);
return path;
}
}
}
WebRequest request = WebRequest.Create("http://somesite.com/private/myfile.txt");
WebResponse response = request.GetResponse();
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
string line = "";
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
response.Close();
public partial class Form1 : Form
{
private LongTimeJob _ltj;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
_ltj = new LongTimeJob();
button1.Enabled = false;
_ltj.Complete += _ltj_Complete;
_ltj.Tick += _ltj_Tick;
_ltj.Run();
}
private void _ltj_Tick(object sender, TickArgs args) =>
progressBar1.Invoke((MethodInvoker)delegate { progressBar1.Value = args.Percent; });
private void _ltj_Complete(object sender, EventArgs args)
{
button1.Invoke((MethodInvoker)delegate { button1.Enabled = true; });
textBox1.Invoke((MethodInvoker)delegate { textBox1.Text = _ltj.Text; });
}
}
public class LongTimeJob
{
public int Percent { get; private set; }
public string Text { get; private set; } = "";
public void Run()
{
Percent = 0;
Task.Run(async () => await Download());
}
private async Task Download()
{
for (var i = 0; i < 9; i++)
{
Percent = i * 10;
Text += $"Download: {i}\r\n";
OnTick(Percent);
await Task.Delay(400);
}
await Parse();
}
private async Task Parse()
{
Percent = 100;
Text += "Parsing\r\n";
OnTick(Percent);
await Task.Delay(500);
OnComplete();
}
public event TickDelegate Tick;
protected virtual void OnTick(int percent)
{
Tick?.Invoke(this, new TickArgs(percent));
}
public event CompleteDelegate Complete;
protected virtual void OnComplete()
{
Complete?.Invoke(this, new EventArgs());
}
}
public delegate void TickDelegate(object sender, TickArgs args);
public delegate void CompleteDelegate(object sender, EventArgs args);
public class TickArgs : EventArgs
{
public TickArgs(int percent)
{
Percent = percent;
}
public int Percent { get; private set; }
}
// 8 раз в промежутке времени с 6 утра до 23 вечера и 2 раза с 23 вечера до 6 утра.
var now = DateTime.Now;
var startPeriod = (23 - 6) * 60;
var startList = new List<int>();
while (startList.Count < 8)
{
var val = RandomNumberGenerator.GetInt32(0, startPeriod);
if (!startList.Contains(val))
startList.Add(val);
}
var runList = startList.OrderBy(x => x).Select(item => new DateTime(now.Year, now.Month, now.Day).AddMinutes(6 * 60 + item)).ToList();
startList.Clear();
var endPeriod = (1 + 6) * 60;
while (startList.Count < 2)
{
var val = RandomNumberGenerator.GetInt32(0, endPeriod);
if (!startList.Contains(val))
startList.Add(val);
}
runList.AddRange(startList.OrderBy(x => x).Select(item => new DateTime(now.Year, now.Month, now.Day).AddMinutes(23 * 60 + item)));
;