我得到应用程序异常
at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
at System.Windows.Forms.CurrencyManager.get_Current()
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnRowEnter(DataGridViewCellEventArgs e)
at System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean canCreateNewRow, Boolean validationFailureOccurred)
at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)
at System.Windows.Forms.DataGridView.OnCellMouseDown(HitTestInfo hti, Boolean isShiftDown, Boolean isControlDown)
at System.Windows.Forms.DataGridView.OnCellMouseDown(DataGridViewCellMouseEventArgs e)
at System.Windows.Forms.DataGridView.OnMouseDown(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, …Run Code Online (Sandbox Code Playgroud) 我已经在各个地方读过,具有全局范围的变量,即具有静态成员的公共静态类,被认为违背了OO的哲学,并且不是好的设计.(例如,我看到过以下几点的评论:"如果你使用的是全局,那么你做得不对."或者是这样的话.)
但是,如果您使用Visual Studio提供的设置机制,例如"Settings.Default.MySetting"等,这可以在整个应用程序中全局使用,那么这与使用公共静态类有何不同?
而且,通过使用单个对象可以实现相同的结果,但是至少可以引起各种意见.
全局变量只是有用,(VB模块,任何人?),但我正在努力教自己如何正确地做这个OO malarky,所以,如果从OO的角度来看全局变量闻起来很糟糕,还有什么选择呢?
我对人们对使用"设置"功能的看法特别感兴趣.这被认为是好的OO设计吗?
谢谢你的任何评论.
好吧,据我所知,.NET Threadpool维护了许多后台线程,可以用于某种任务.
Get/SetMinThreads和Get/SetMaxThreads方法包含两个可以返回或调整的参数.
根据MSDN,这两个参数指示工作线程的数量和用于异步IO操作的线程数.
什么类型的操作使用这些特定类型的线程?
工人线程:
异步IO线程:
感谢您对此主题的任何澄清或良好链接.
参考MSDN关于System.Timers.Timer的这句话:
Timer.Elapsed事件是在ThreadPool线程上引发的,因此事件处理方法可能在一个线程上运行,同时调用Timer.Stop方法在另一个线程上运行.这可能导致在调用Stop方法后引发Elapsed事件.通过将SignalTime属性与调用Stop方法的时间进行比较,无法阻止此竞争条件,因为事件处理方法可能在调用Stop方法时已经执行,或者可能在Stop方法之间开始执行被调用以及保存停止时间的时刻.如果在事件处理方法仍在执行时阻止调用Stop方法的线程继续运行至关重要,请使用更强大的同步机制,例如Monitor类或CompareExchange方法.使用CompareExchange方法的代码可以在Timer.Stop方法的示例中找到.
任何人都可以举一个" 强大的同步机制,如Monitor类 "来解释这意味着什么?
我认为这意味着以某种方式使用锁,但我不确定如何实现它.
我正在使用NUnit和Moq来测试一个有一些事件的类,我正在尝试找到测试事件是否被触发的最佳方法.我提出了这个解决方案,但感觉有点脏,因为我必须为测试创建一个界面.用任何方式我可以用更少的代码做同样的事情或不必创建一个接口?
它并没有那么糟糕,但我觉得有人可能有更好的解决方案.任何想法都表示赞赏.谢谢.
[Test]
public void StartedAndStoppedEventsShouldFireWhenStartedAndStopped()
{
var mockStartedEventSubscriber = new Mock<IEventSubscriber>();
var mockStoppedEventSubscriber = new Mock<IEventSubscriber>();
_NetworkMonitor.Started += mockStartedEventSubscriber.Object.Handler;
_NetworkMonitor.Stopped += mockStoppedEventSubscriber.Object.Handler;
_NetworkMonitor.Start();
_NetworkMonitor.Stop();
Func<bool> func = () => { return (eNetworkMonitorStatus.Stopped == _NetworkMonitor.Status); };
Utilities.WaitUntilTrue(func, _NetworkMonitor.Interval * 2, 10);
_NetworkMonitor.Started -= mockStartedEventSubscriber.Object.Handler;
_NetworkMonitor.Stopped -= mockStoppedEventSubscriber.Object.Handler;
mockStartedEventSubscriber.Verify(h => h.Handler(_NetworkMonitor, EventArgs.Empty), Times.Once());
mockStoppedEventSubscriber.Verify(h => h.Handler(_NetworkMonitor, EventArgs.Empty), Times.Once());
}
public interface IEventSubscriber
{
void Handler(object sender, EventArgs e);
}
Run Code Online (Sandbox Code Playgroud) 我有一个像这样的模拟:
var mock = new Mock<IPacket>(MockBehavior.Strict);
mock.Setup(p => p.GetBytes()).Returns(new byte[] { }).Verifiable();
Run Code Online (Sandbox Code Playgroud)
Setup方法的intellisense说:
"指定模拟类型的设置,以调用void 返回方法."
但是模拟的方法p.GetBytes()不返回void,它返回一个字节数组.
另外,另一个Setup方法定义为Setup <>,我可以像这样创建我的mock:
var mock = new Mock<IPacket>(MockBehavior.Strict);
mock.Setup<byte[]>(p => p.GetBytes()).Returns(new byte[] { }).Verifiable();
Run Code Online (Sandbox Code Playgroud)
此Setup方法的intellisense指出:
"指定模拟类型的设置,以调用值 返回方法."
.
.
无论我选择哪种方法,它都可以编译和测试.所以,我很困惑我应该采取哪种方式..Setup()和.Setup <>()之间有什么区别,我做得对吗?
我们说,Moq的文档有点缺乏.:)
我正在开发一个使用UdpClient的类,并尝试在此过程中使用NUnit和Moq学习/利用TDD方法.
到目前为止,我班上的一个简单部分如下:
public UdpCommsChannel(IPAddress address, int port)
{
this._udpClient = new UdpClient();
this._address = address;
this._port = port;
this._endPoint = new IPEndPoint(address, port);
}
public override void Open()
{
if (this._disposed) throw new ObjectDisposedException(GetType().FullName);
try
{
this._udpClient.Connect(this._endPoint);
}
catch (SocketException ex)
{
Debug.WriteLine(ex.Message);
}
}
public override void Send(IPacket packet)
{
if (this._disposed) throw new ObjectDisposedException(GetType().FullName);
byte[] data = packet.GetBytes();
int num = data.Length;
try
{
int sent = this._udpClient.Send(data, num);
Debug.WriteLine("sent : " + sent);
}
catch (SocketException ex) …Run Code Online (Sandbox Code Playgroud) 我想在基类上显式实现一个接口方法.
除此之外,我想将此方法设为虚拟,以便我可以在派生类上覆盖它,但显式实现的方法不允许这样做.
我尝试在基类中创建一个受保护的虚方法,从接口方法调用它,然后在派生类中重写此方法.这似乎有效,但FxCop抱怨规则CA1033"接口方法应该可以通过子类型调用".
(我的基类实现了接口,派生类没有.)
我应该如何改进这个(缩写的)代码更正确,或者我应该在这种情况下忽略FxCop?
在基类中:
protected virtual string ConstructSignal()
{
return "Base string";
}
#region ISignal Members
string ISignal.GetMessage()
{
this.ConstructSignal();
}
#endregion
Run Code Online (Sandbox Code Playgroud)
在派生类中:
protected override string ConstructSignal()
{
return "Derived string";
}
Run Code Online (Sandbox Code Playgroud)
决定在最后隐式实现接口方法,这仍然有效并使FxCop保持高兴.
根据MSDN:
Dictionary.KeyCollection中键的顺序未指定
我假设这是因为字典的添加被放入某种哈希表中.
但是,我想从一个Dictionary中将.Keys集合作为IEnumerable(或者可能是ICollection)从一个方法返回,并按照它们最初添加到Dictionary中的顺序枚举它们.
如何最好地完成这个?
(我使用的是Winforms,.NET 2.0)
您是否应该考虑Properties.Settings.Default在类中使用作为依赖项,并因此注入它?
例如:
public class Foo
{
private _settings;
private bool _myBool;
public Foo(Settings settings)
{
this._settings = settings;
this._myBool = this._settings.MyBool;
}
}
Run Code Online (Sandbox Code Playgroud)
.
.
或者您是否会考虑将Settings全球应用作为良好做法?
例如:
public class Foo
{
private bool _myBool;
public Foo()
{
this._myBool = Properties.Settings.Default.MyBool;
}
}
Run Code Online (Sandbox Code Playgroud) c# ×9
winforms ×5
moq ×3
unit-testing ×3
.net ×2
nunit ×2
settings ×2
datagridview ×1
definition ×1
dictionary ×1
events ×1
ienumerable ×1
interface ×1
key ×1
overriding ×1
sql ×1
threadpool ×1
timer ×1