我目前有一个方法调用,需要很长时间才能加载,有时还会超时。在尝试更快地调用数据之前,我想编写一个集成测试,以确保在合理的时间内返回我的数据。我该怎么做呢?
[TestMethod]
public void GetResults_ReturnsDataInReasonableAmountOfTime_Test()
{
var result = _dataAccess.GetListOfResults();
Assert.IsTrue(##How do I Test that result was returned in under 2 se
Windows没有NetworkStream,与服务器通信的所有操作都应该是异步的。
所以在普通的.net应用程序中,我可以使用这样的^
var stream = socket.GetStream();
while ((read = stream.Read (buffer, offset, size)) != 0)
{
// bufferize it
}
//stream ended and we can parse all bytes.
在Windows中,我只能这样使用:
var args = new SocketAsyncEventArgs();
args.S
我得到了一个复杂的.net控制台应用程序,它在一些通知窗口中使用WPF,还可以执行一些http调用。在非常罕见的情况下,应用程序崩溃以及我能够获得的唯一错误消息来自于windows事件查看器:
Application: xyz.exe
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ObjectDisposedException
Stack:
at System.Runtime.InteropServices.SafeHandle.Dangerous
主线:
(是的,我现在确实停止了主线程,我现在真的需要它来测试,这是测试单元。在最后阶段,我将在另一个线程中完成所有这些工作。)
代码:
function TFreHolder.GetVersion: TFreHolderVersion;
Var
Data : TBArray;
StrLen : Integer;
begin
SetLength(Data, 0);
FFrePro.SendCommand(PROTO_COMVERSION, Data);
ResetEvent(FAsyncMutex);
if (WaitForSingleObject(FAsyncMutex
这就是我想要达到的目标。
我有一个登录类。一旦对用户进行了身份验证,一些post登录操作将在线程中完成。然后用户进入主页。
现在,我从主页转到一个不同的功能,比如类FindProduct。我需要检查登录线程中的post登录操作是否已经完成。只有当post登录操作完成时,我才允许输入该功能。
我必须在PerformLoginAsyncThread和OnClickFindProduct上放置等待句柄吗?
Class Login
{
public bool Login(Userinfo)
{
// do tasks like authenticate
if(aut
我在主线程中创建了一个System.Timers.Timer实例。现在,我调用timer.Stop()试图终止该时间,并希望等到计时器真正结束。我怎么能这么做?
有没有类似于System.Threading.Thread.Join()的方法?
这里有一些代码
//the main thread:
var aTimer = New Timer();
aTimer.Elapsed += SomeTimerTask;
aTimer.AutoReset = True;
aTimer.Start();
//some other logic...
//stop that timer:
aTimer.S
private void DoSomeWork()
{
ManualResetEvent[] waitEvents = new ManualResetEvent[rowCount];
int i = 0;
foreach (DataRow row in StoredProcedureParameters.Tables[0].Rows)
{
waitEvents[i] = new ManualResetEvent(false);
var workerClass = new WorkerClass();
var
我有一个方法StartProcess()。如果同一方法由另一个线程在同一时间调用,或者在调用EndProcess()之前由同一个初始线程调用,则此方法将引发异常。
我尝试了Monitor类,但是我不知道如何将它应用到上述说明的情况中。我看到的与我所寻找的接近的是:
var obj = new Object();
// Define the critical section.
Monitor.Enter(obj);
try {
// Code to execute one thread at a time.
}
// catch blocks go here.
finally {
我有两个窗体,一个是主窗体,另一个是弹出的模式对话框。从主窗体中产生的进程中,我希望动态更新模式对话框上的文本。这就是我所拥有的:
在主窗体中,我这样做:
// show the wait modal
var modal = new WaitDialog { Owner = this };
// thread the packaging
var thread = new Thread(() => Packager.PackageUpdates(clients, version, modal));
我们有下面这段代码(这段代码的想法可以在这个网站上找到),它将为"Do_SomeWork()“方法产生新的线程。这使我们能够异步多次运行该方法。
代码是:
var numThreads = 20;
var toProcess = numThreads;
var resetEvent = new ManualResetEvent(false);
for (var i = 0; i < numThreads; i++)
{
new Thread(delegate()
{
Do_Some
我试图在linux arm设备上优雅地关闭我的控制台应用程序。发送的信号是SIGTERM。我用新的方法实现了一个Posix。当在调试模式下编译时,这是非常完美的。但是,在发布模式下(在打开优化的情况下),从未输入处理程序。
这是一份MRE:
using Microsoft.Extensions.Hosting;
using System.Runtime.InteropServices;
ManualResetEvent resetEvent = new(false);
CancellationTokenSource cts = new();
// Handles CTRL + C in b
我正在用HID设备获取数据。下面的代码是我的计时机制的大致轮廓。
Dim CANTimer as New System.Diagnostics.Stopwatch
Dim resetEvent as New Threading.ManualResetEvent(False)
....
CANTimer.Start()
ResetEvent.WaitOne(1)
CANTimer.Stop()
计时,我通常会得到3ms的时间,这是我期望的HID传输的延迟...并且大约每4次或5次迭代将花费20ms。无论我将超时设置为毫秒,这些数字都不会真正改变。
为什么ResetEvent不会在一毫秒时超时?
我想对两种不同的类型进行WaitForMultipleObjects:
“EventWaitHandle”
“Process.Handle”==> intptr
我不知道如何(以适当的方式)将"process.Handle“转换为WaitHandle,以便使以下代码工作:
var waitHandles = new WaitHandle[2];
waitHandles[0] = waitHandleExit;
// Next line is the offending one:
waitHandles[1] = new SafeWaitHa
我有一些旧的代码,我正试图为其编写测试。代码解析日志文件(在后台线程上),并在完成时触发传递的委托。
即
public delegate void finread(LogData l, LRParseState l, string e="");
void Thread_ParseLog(object info) {
var info = ti as ThreadInfo;
// some time later
info.fin(log, state, error);
}
public static void ParseErrorLog(string log, fin
我有一个用C#编写的WPF应用程序,它提供了一个用于与串口上的一些设备通信的图形用户界面。GUI包含一个原始输出字段,在该字段中,字符在输入时显示,并包含一个显示数据、过滤和处理数据的部分。有时(当我的外部设备出问题时),大量数据进入,程序间歇性地锁定。
简单地说,这就是我的代码的样子:
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
// get string out of buffer
// do filtering and processing (see de
我正在玩一个套接字服务器,并试图按照测试驱动的开发模式工作。
对于套接字创建方法有一个有效的测试,但是我的测试挂起是因为我有一个ManualResetEvent.WaitOne(),以便我的套接字在创建另一个连接之前等待一个连接。
下面是要测试的代码块:
ManualResetEvent allDone = new ManualResetEvent(false);
public void StartListening()
{
allDone.Reset();
//Inform on the console that the socket is ready
Consol
我创建了一个带有Start和End Index的存储过程,以便在两者之间读取行
例如,用于
Create Procedure ProcA
@StartIndex int,
@EndIndex int
As
Begin
Select * from Table A where Rowno between @StartIndex and @EndIndex
End
现在我需要同时使用多个线程从C#获取行,
这是我的代码,
public static DataTable ReadRecord(int startindex)
{
SqlConnection SqlConn = new Sq
我使用Owin自托管和WebApi编写了简单的服务器:
namespace OwinSelfHostingTest
{
using System.Threading;
using System.Web.Http;
using Microsoft.Owin.Hosting;
using Owin;
public class Startup
{
public void Configuration(IAppBuilder builder)
{
var config = new HttpConfi