C# Winforms:模拟用户点击(开始-等待数据采集-停止)
C# Winforms: Simulate user clicks(start- wait for data collection - stop)
我正在使用 Winforms 编写 C# 应用程序,我需要根据我从组合框中选择的内容收集一些数据。我还有一个启用数据收集的开始按钮和一个停止数据收集的停止按钮。
这是我现在能做的:
Measure all data from all channels by switching my selection on the comboBox (one channel at a time). The process goes like: select a channel- start button click - wait for data collection - stop button click
但这是我想在代码中做的事情:
Select一个通道-开始按钮点击-等待数据采集-停止按钮点击
切换到下一个频道 - 开始按钮点击 - 等待数据收集 - 停止按钮点击
.....................
重复这个过程直到完成。
我的问题是:我应该采取什么来实现这个目标?
我一直在尝试使用 startButton.PerformClick( ) 来启用按钮,但是,我需要在开始和停止之间停止几秒钟以等待数据收集。
你可能会问为什么,因为这样效率很低,但是由于某些原因,来自第三方的DLL无法同时从所有通道收集数据。所以我必须从组合框中手动切换我的频道选择,以便一次性收集所有数据。
如果您有任何建议,请告诉我。
private void timer_Tick(object sender, EventArgs e)
{
startButton.PerformClick();
}
private void timer2_Tick(object sender, EventArgs e)
{
stopButton.PerformClick();
}
private void checkAll_CheckedChanged(object sender, EventArgs e)
{
int i = 0;
while (i != 40) //there are a total of 40 channels
{
System.Windows.Forms.Timer mytimer = new System.Windows.Forms.Timer();
mytimer.Interval = 5000;
System.Windows.Forms.Timer mytimer2 = new System.Windows.Forms.Timer();
mytimer2.Interval = 7000;
mytimer.Start();
mytimer.Tick += new EventHandler(timer_Tick);
mytimer2.Start();
mytimer2.Tick += new EventHandler(timer2_Tick);
physicalChannelComboBox.SelectedIndex = i;
i++;
Thread.Sleep(5000);
}
}
这是一个简单的例程 shell,应该会对您有所帮助。这使用计时器来轮询数据是否完整(检查一个布尔值,当前设置为 true 用于测试)。在您的情况下,您可能只需将计时器滴答值设置为您想要的延迟。如果您有办法知道何时完成为频道收集数据,那将更可取。此代码独立运行,因此您可以在开始将其集成到现有代码之前对其进行测试和配置。它只需要一个用于查看日志的列表框。
public partial class DataCollectorForm : Form
{
private Timer timer = new Timer();
private int numberOfChannels = 40;
private int currentChannelNumber = 0;
private DateTime routineStartTime;
private DateTime routineStopTime;
private DateTime channelStartTime;
private DateTime channelStopTime;
public DataCollectorForm()
{
InitializeComponent();
timer.Interval = 250;
timer.Tick += Timer_Tick;
DataCollectionRoutineStart();
}
private void Timer_Tick(object sender, EventArgs e)
{
// Need to check if data collection for this channel is complete..
//
var isDoneCollectingData = true;
if (isDoneCollectingData)
{
ChannelDataCollectionStop();
currentChannelNumber++;
if (currentChannelNumber >= numberOfChannels)
{
DataCollectionRoutineComplete();
}
else
{
ChannelDataCollectionStart();
}
}
}
public void DataCollectionRoutineStart()
{
routineStartTime = DateTime.Now;
Log("Data Collection Routine Start");
currentChannelNumber = 0;
ChannelDataCollectionStart();
timer.Start();
}
private void ChannelDataCollectionStart()
{
channelStartTime = DateTime.Now;
Log("Data Collection Start : channel " + currentChannelNumber);
}
private void ChannelDataCollectionStop()
{
channelStopTime = DateTime.Now;
Log("Data Collection Stop : channel " + currentChannelNumber + " : elapsed " + (channelStopTime - channelStartTime));
}
private void DataCollectionRoutineComplete()
{
routineStopTime = DateTime.Now;
timer.Stop();
Log("Data Collection Routine Complete : elapsed " + (routineStopTime - routineStartTime));
}
private void Log(string msg)
{
loggingListBox.Items.Add(msg);
}
}
我正在使用 Winforms 编写 C# 应用程序,我需要根据我从组合框中选择的内容收集一些数据。我还有一个启用数据收集的开始按钮和一个停止数据收集的停止按钮。
这是我现在能做的:
Measure all data from all channels by switching my selection on the comboBox (one channel at a time). The process goes like: select a channel- start button click - wait for data collection - stop button click
但这是我想在代码中做的事情: Select一个通道-开始按钮点击-等待数据采集-停止按钮点击 切换到下一个频道 - 开始按钮点击 - 等待数据收集 - 停止按钮点击 ..................... 重复这个过程直到完成。
我的问题是:我应该采取什么来实现这个目标? 我一直在尝试使用 startButton.PerformClick( ) 来启用按钮,但是,我需要在开始和停止之间停止几秒钟以等待数据收集。
你可能会问为什么,因为这样效率很低,但是由于某些原因,来自第三方的DLL无法同时从所有通道收集数据。所以我必须从组合框中手动切换我的频道选择,以便一次性收集所有数据。
如果您有任何建议,请告诉我。
private void timer_Tick(object sender, EventArgs e)
{
startButton.PerformClick();
}
private void timer2_Tick(object sender, EventArgs e)
{
stopButton.PerformClick();
}
private void checkAll_CheckedChanged(object sender, EventArgs e)
{
int i = 0;
while (i != 40) //there are a total of 40 channels
{
System.Windows.Forms.Timer mytimer = new System.Windows.Forms.Timer();
mytimer.Interval = 5000;
System.Windows.Forms.Timer mytimer2 = new System.Windows.Forms.Timer();
mytimer2.Interval = 7000;
mytimer.Start();
mytimer.Tick += new EventHandler(timer_Tick);
mytimer2.Start();
mytimer2.Tick += new EventHandler(timer2_Tick);
physicalChannelComboBox.SelectedIndex = i;
i++;
Thread.Sleep(5000);
}
}
这是一个简单的例程 shell,应该会对您有所帮助。这使用计时器来轮询数据是否完整(检查一个布尔值,当前设置为 true 用于测试)。在您的情况下,您可能只需将计时器滴答值设置为您想要的延迟。如果您有办法知道何时完成为频道收集数据,那将更可取。此代码独立运行,因此您可以在开始将其集成到现有代码之前对其进行测试和配置。它只需要一个用于查看日志的列表框。
public partial class DataCollectorForm : Form
{
private Timer timer = new Timer();
private int numberOfChannels = 40;
private int currentChannelNumber = 0;
private DateTime routineStartTime;
private DateTime routineStopTime;
private DateTime channelStartTime;
private DateTime channelStopTime;
public DataCollectorForm()
{
InitializeComponent();
timer.Interval = 250;
timer.Tick += Timer_Tick;
DataCollectionRoutineStart();
}
private void Timer_Tick(object sender, EventArgs e)
{
// Need to check if data collection for this channel is complete..
//
var isDoneCollectingData = true;
if (isDoneCollectingData)
{
ChannelDataCollectionStop();
currentChannelNumber++;
if (currentChannelNumber >= numberOfChannels)
{
DataCollectionRoutineComplete();
}
else
{
ChannelDataCollectionStart();
}
}
}
public void DataCollectionRoutineStart()
{
routineStartTime = DateTime.Now;
Log("Data Collection Routine Start");
currentChannelNumber = 0;
ChannelDataCollectionStart();
timer.Start();
}
private void ChannelDataCollectionStart()
{
channelStartTime = DateTime.Now;
Log("Data Collection Start : channel " + currentChannelNumber);
}
private void ChannelDataCollectionStop()
{
channelStopTime = DateTime.Now;
Log("Data Collection Stop : channel " + currentChannelNumber + " : elapsed " + (channelStopTime - channelStartTime));
}
private void DataCollectionRoutineComplete()
{
routineStopTime = DateTime.Now;
timer.Stop();
Log("Data Collection Routine Complete : elapsed " + (routineStopTime - routineStartTime));
}
private void Log(string msg)
{
loggingListBox.Items.Add(msg);
}
}