TaskTimer,减少您在互联网上的时间






2.95/5 (7投票s)
一款新颖的独立工具,旨在帮助限制与计算机相关的干扰,例如互联网,同时促进更健康的目标驱动行为。
引言
互联网是一个很棒的工具,但它经常分散我们的注意力。同样的情况也适用于一般的计算机混乱——附近的所有图标和文件都让我们很容易陷入意想不到的冒险中。TaskTimer
是一款工具,通过控制我们对互联网的访问并定期提醒我们专注于选定的任务,来限制我们接触到的干扰。它显示为托盘图标,只需一步即可完全配置,方法是设置所需的提示消息、您希望看到提示消息的大概频率,以及您希望程序根据您的需要打开和关闭的 Internet 适配器(例如 Wi-Fi)。
启动 TaskTimer
将阻止对您直接网络的访问,包括您的互联网连接。然后,在您关闭应用程序之前,您只能在您为您的互联网目标请求的有限时间内访问互联网。同样,还有额外的功能可以鼓励您专注于非互联网目标。退出 TaskTimer
将重新启用您的互联网访问。
背景
众所周知,网页和广告旨在最大限度地提高我们接触营销材料的机会。用户甚至可能没有注意到他们花了多少时间参与他们从未计划消费的内容。结果是每天浪费数小时——本来可以用来有效地提高一个人的技能或社交联系的时间。做出更好改变的第一步是理解并接受这一潜在的挑战。
目前,还没有针对过度沉迷互联网问题的独立解决方案。有多个网站提供跟踪您在线时间——甚至提供存储您所从事任务的日志的服务——但这仍然是服务,对让您在线而不是离线感兴趣。TaskTimer
采取了只有免费软件才能采取的替代方法,因为它根本不要求用户做出任何贡献。它尊重用户关于离线的决定,并强制执行用户的既定目标,即仅在有限的时间内在线,之后互联网将被关闭,提示重新审视用户是否值得继续浏览。
TaskTimer
依赖于两个基本原则:制定计划并保持专注通常会提高生产力,而唠叨会增加我们朝着目标前进的可能性。TaskTimer
使用任务栏弹出通知(它们是可点击的)的较少探索的功能来创建一个更集中的计算机环境,轻轻地提醒我们一次专注于一个目标,或者在不再需要时停止浏览互联网。

Using the Code
用 C# .NET Framework 4.0 编写,TaskTimer
的代码作为 Visual Studio 项目附加。虽然目前功能齐全,但很可能有人希望添加一些额外的功能,在这种情况下,他们可以随意更改/增强 TaskTimer
。
核心功能使用 System.Timers.Timer
每 15 秒监控一次更改。启动应用程序后,ApplicationContext.cs 将用户的当前互联网配置(IP 地址等)存储到容器类 InternetDetails.cs 中。用户界面 Form.cs 显示了计算机中可以由程序控制的互联网适配器列表。鼓励用户在关闭初始弹出窗口之前存储他们的设置,尽管可以通过从任务栏中创建的图标中选择“关闭程序”来关闭 UI 和程序。按下“保存并关闭互联网”按钮确实会关闭互联网访问。

这是通过暂时将 IP 和主机地址设置为虚假值来完成的,如下所示
Process cmd1 = new Process();
cmd1.StartInfo.FileName = "cmd.exe";
cmd1.StartInfo.RedirectStandardInput = true;
cmd1.StartInfo.RedirectStandardOutput = true;
cmd1.StartInfo.CreateNoWindow = true;
cmd1.StartInfo.UseShellExecute = false;
String sCmd = "netsh interface ip set address \"" + context.sActiveAdapter +
"\" static 192.168.1.100 255.255.255.0 192.168.0.254";
cmd1.StartInfo.Arguments = "/C " + sCmd;
cmd1.Start();
cmd1.WaitForExit();
退出程序或关机时,将恢复以前的互联网设置,以便一切再次运行。
改编框架
作为典型的托盘应用程序,TaskTimer
驻留在系统托盘中,依靠隐藏的表单在必要时与用户交互。这是通过使用名为 ApplicationContext.cs 的中间应用程序上下文类启动程序来实现的。
namespace TaskTimer
{
static class Program
{
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new ApplicationContext());
}
}
}
然后从 ApplicationContext.cs 中启动主用户表单
public ApplicationContext()
{
Application.ApplicationExit += new EventHandler(this.OnApplicationExit);
InitializeComponent();
TrayIcon.Visible = true;
bSettingsConfigured = false;
}
private void InitializeComponent()
{
...
frm = new Form(this, iDetails);
frm.Show();
}
在 InitializeComponent()
中,还有关于创建托盘图标应用程序的通用结构,以及删除托盘图标 OnApplicationExit
的方法
private void InitializeComponent()
{
...
TrayIcon = new NotifyIcon();
TrayIcon.BalloonTipIcon = ToolTipIcon.Info;
TrayIcon.Text = "Double-Click to Set Current Task";
this.sTaskDesc = "";
this.bInTask = false;
//The icon is added to the project resources.
TrayIcon.Icon = Properties.Resources.TrayIcon;
//Optional - handle doubleclicks on the icon:
TrayIcon.DoubleClick += TrayIcon_DoubleClick;
TrayIcon.BalloonTipClicked += TrayIcon_BalloonTipClick;
//////////////////
// Context Menu //
//////////////////
TrayIconContextMenu = new ContextMenuStrip();
toolstripRequestTime = new ToolStripMenuItem();
toolstripClose = new ToolStripMenuItem();
TrayIconContextMenu.SuspendLayout();
//
// TrayIconContextMenu
//
this.TrayIconContextMenu.Items.AddRange(new ToolStripItem[] {this.toolstripRequestTime});
this.TrayIconContextMenu.Items.AddRange(new ToolStripItem[] {this.toolstripClose});
this.TrayIconContextMenu.Name = "TrayIconContextMenu";
this.TrayIconContextMenu.Size = new Size(153, 70);
//
// toolstripRequestTime
//
this.toolstripRequestTime.Name = "reqTime";
this.toolstripRequestTime.Size = new Size(152, 22);
this.toolstripRequestTime.Text = "Request Time";
this.toolstripRequestTime.Click += new EventHandler(this.RequestTime_Click);
this.toolstripClose.Name = "closProg";
this.toolstripClose.Size = new Size(152, 22);
this.toolstripClose.Text = "Close Program";
this.toolstripClose.Click += new EventHandler(this.CloseProgram_Click);
TrayIconContextMenu.ResumeLayout(false);
TrayIcon.ContextMenuStrip = TrayIconContextMenu;
...
}
private void OnApplicationExit(object sender, EventArgs e)
{
TrayIcon.Visible = false;
}
关注点
理想情况下,通过简单地启用代理服务器并设置该服务器的地址,就可以更容易地完成同样的目的。但是,发现了以下 Chrome 错误,使得这种方法不切实际
还发现 Windows 7 的关机过程与任何类型的 Process.*
调用不兼容。为了适应这个流行的操作系统,使用了 WMI 对象
public void SetIP(string ipAddress, string subnetMask, string gateway)
{
using (var networkConfigMng = new ManagementClass("Win32_NetworkAdapterConfiguration"))
{
using (var networkConfigs = networkConfigMng.GetInstances())
{
foreach (var managementObject in networkConfigs.Cast<ManagementObject>().Where
(managementObject => (bool)managementObject["IPEnabled"]))
{
using (var newIP = managementObject.GetMethodParameters("EnableStatic"))
{
// Set new IP address and subnet if needed
if ((!String.IsNullOrEmpty(ipAddress)) || (!String.IsNullOrEmpty(subnetMask)))
{
if (!String.IsNullOrEmpty(ipAddress))
{
newIP["IPAddress"] = new[] { ipAddress };
}
if (!String.IsNullOrEmpty(subnetMask))
{
newIP["SubnetMask"] = new[] { subnetMask };
}
managementObject.InvokeMethod("EnableStatic", newIP, null);
}
// Set new gateway if needed
if (!String.IsNullOrEmpty(gateway))
{
using (var newGateway = managementObject.GetMethodParameters("SetGateways"))
{
newGateway["DefaultIPGateway"] = new[] { gateway };
newGateway["GatewayCostMetric"] = new[] { 1 };
managementObject.InvokeMethod("SetGateways", newGateway, null);
}
}
}
}
}
}
} // end SetIP
public void SetDHCP()
{
using (var networkConfigMng = new ManagementClass("Win32_NetworkAdapterConfiguration"))
{
using (var networkConfigs = networkConfigMng.GetInstances())
{
foreach (var managementObject in networkConfigs.Cast<ManagementObject>().Where
(managementObject => (bool)managementObject["IPEnabled"]))
{
var ndns = managementObject.GetMethodParameters("SetDNSServerSearchOrder");
ndns["DNSServerSearchOrder"] = null;
managementObject.InvokeMethod("EnableDHCP", null, null);
managementObject.InvokeMethod("SetDNSServerSearchOrder", ndns, null);
}
}
}
} // end SetDHCP
这是仅供 PC 使用的第一个 Beta 版。同样,应安装 .NET Framework 4.0。