在触发事件时,避免竞争条件(在多线程应用程序中)的一个常见做法是:
EventHandler<EventArgs> temp = SomeEvent;
if (temp != null) temp(e);
"Remember that delegates are immutable and this is why this technique works in theory. However, what a lot of developers don't realize is that this code could be optimized by the com
我是C#新手,我需要将代码从VB.NET转换为C#。
这是我的VB.NET代码
Private Sub Receive()
Dim inp As NetworkStream
Dim income As BinaryReader
inp = TCP.GetStream
income = New BinaryReader(inp)
While (State = "Connected")
Try
msg = income.ReadChar
Application.OpenForm
我正在学习C#中的事件/代表。我能问一下你对我选择的命名/编码风格的看法吗(摘自第一本C#的书)?
我明天教一个朋友这件事,并试图想出一个最优雅的方法来解释这些概念。(认为理解一门学科最好的方法就是试着教它!)
class Program
{
static void Main()
{
// setup the metronome and make sure the EventHandler delegate is ready
Metronome metronome = new Metronome();
这段添加的代码为一个名为NewMail的事件注册新的EventHandler( eventargs类的名称为NewMailEventArgs。
// A PUBLIC add_xxx method (xxx is the event name)
// Allows methods to register interest in the event.
public void add_NewMail(EventHandler<NewMailEventArgs> value) {
// The loop and the call to CompareExchange is all j
我有这段代码,但至少对我来说,它导致了一个奇怪的异常。
public class Flight
{
public class MessageEventArgs : System.EventArgs
{
public string msgContent;
}
public event System.EventHandler LogMessage;
public void StartFlight()
{
string tmpDeparture = this.Departure;
string tmp
我最近一直在使用C#,我注意到在我公司的代码中引发事件的大多数代码都是这样完成的:
EventHandler handler = Initialized;
if (handler != null)
{
handler(this, new EventArgs());
}
我真的不明白为什么,你不能这样做:
if (Initialized != null)
{
Initialized(this, new EventArgs());
}
编辑:
一些值得深思的东西,我试着做了一些测试:
using System;
using System.Collections.Generic;
我有一台带有数字I/O引脚的工业计算机。制造商提供了一些C++库和处理引脚状态更改的示例。
我需要将这些事件集成到C#应用程序中。AFAIK最简单的执行方法是:
为制造商库制作一个托管C++/CLI包装器,在从DIO引脚发出中断时触发事件。
引用该包装器并处理C#部件中的事件,因为它们是普通的C#事件。
我试着用一些没有运气的模拟对象来完成这个工作。在我的例子中,从文档中,函数EventHandler应该完成大部分“脏工作”。下面是旧线程中可用的信息,以及MSDN文档中的EventHandler示例,最后我得到了以下测试代码:
C++/CLI
using namespace S
我正试图用NAudio在C#中录制声音,但我被困在两个地方:
1. A crash:
通过对页面中的代码进行稍微修改,我得到了一个NullReferenceException。以下是崩溃日志:
************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at NAudio.Wave.WaveIn.Callback(IntPtr waveInHandle, WaveMessage messag
我有以下代码:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
namespace Test
{
public partial class MainPage : PhoneApplicationPage
{
Microphone microphone = Microphone.Default;
public MainPage()
{
InitializeComponent();
micro
使用C#,我已经使用Windows Forms拖放了一个按钮,并动态地创建了一个按钮和文本框。我想在按钮上显示文本框中的输入。硬编码按钮工作正常,但动态按钮使用System.NullReferenceException使程序崩溃。
请帮助我了解如何通过单击动态创建的按钮从动态创建的文本框中获取输入。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System
c#的代码
void method(int a)
{
int b = get(a);
if ( b == 0 )
{
throw new NullReferenceException();
}
}
如果我抛出NullReferenceException,它将生成一个警告,这不是一个好方法。
那么,如果有任何异常而不是NullReferenceException呢?我找到了ArgumentNullException,但它不太适合这段代码,因为b不是一个参数。
我试图在cSharp中引发事件,以通知我的应用程序中的代码更改,以便有tellDontAsk场景。
我有一个从事件类实现的简单类。
public class SimpleTellDontAsk : ISomeEvent
{
public void doSomething(string text, EventHandlerArgs args)
{
Console.WriteLine("Email this message {0}", text);
//sending message with email
args.I
我在把在网上找到的vb.net代码转换成C#时遇到了问题。
vb.net
Protected Overridable Sub OnRowClicked(ByVal e As GridViewRowClickedEventArgs)
'raise the RowClicked event
RaiseEvent RowClicked(Me, e)
End Sub
'setup our EventHandler for RowClicked
Public Custom Event RowClicked As EventHandler(Of GridViewRowC
我正在用C#编写一个.Net控制台应用程序。
我需要将基于事件的线程调用转换为异步方法。
例如:
public class WorkerClass
{
public EventHandler Progress;
public EventHandler Finished;
public void DoSomething()
{
Thread td = new Thread(Start);
td.Name = "Worker Thread";
我有一个应用程序,这是运行在c#窗口窗体。当用户退出应用程序时,我想让用户关闭计算机。我的应用程序有点复杂,但以下是我的问题的一个很好的例子:
public partial class Form1 : Form
{
public class shell32
{
[DllImport("shell32", EntryPoint = "#60")]
private static extern int SHShutDownDialog(long p);
public static void ShutDo
我正在尝试编码我自己的DNN 7模块,以学习。
我实际上是在跟踪。
我已经创建了这个模块,它显示了这个错误。
Error: HelloWorld is currently unavailable.
DotNetNuke.Services.Exceptions.ModuleLoadException: Object reference not set to an instance of an object. --->
System.NullReferenceException: Object reference not set to an instance of an object. at
我使用EmguCV和C#,当我想从我的网络摄像头抓取框架时,我面临一个问题,红色的下划线出现在声明中:
imgOrg = capturecam.QueryFrame();
错误:无法隐式地将类型“Emgu.CV.Mat”转换为“Emgu.CV.Image”
我该如何解决这个问题?
我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System
我正在将 (C#版的Termite)的一个子集移植到vb.net。我在C#中使用过这个应用程序,并对它进行了修改,成功地为我做了一些工作。但这一次,我需要将它折叠到一个vb.net应用程序中,并且当该应用程序从串口接收数据时,我遇到了"Object reference not set to a instance of an object“错误。
下面是失败的代码
Private Sub ReadPort()
While _keepReading
If _serialPort.IsOpen Then
Dim readBuffer As B
我正在做一个推特项目,让用户使用C#自动推送到提到的推文。我在C#中使用TweetSharp。问题是,当我尝试使用计时器刷新显示所提到的推文的form_load时,我得到NullReferenceException是未处理的错误。我尝试将时间间隔从20秒更改为40秒,以防出现时间间隔问题,但问题仍然存在,因此我不认为问题与时间间隔有关。如果有人能帮我解决这个问题,我将不胜感激。
下面是何时刷新Form1_Load:
var timer = new Timer();
timer.Tick += new EventHandler(Form1_Load);
我正在尝试使用NAudio在C#中录制音频。在看了NAudio聊天演示之后,我使用了其中的一些代码进行记录。
代码如下:
using System;
using NAudio.Wave;
public class FOO
{
static WaveIn s_WaveIn;
static void Main(string[] args)
{
init();
while (true) /* Yeah, this is bad, but just for testing.... */
System.Threading