我有一个WPF应用程序,需要向用户提供有关内部状态的反馈.设计是有三个图像,称为红色,黄色和绿色.其中一个图像将根据状态一次显示.以下是要点:
我假设我需要一个图像转换器来将JPG图像更改为图像源,例如:
[ValueConversion(typeof(System.Drawing.Bitmap), typeof(ImageSource))]
public class BitmapToImageSourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var bmp = value as System.Drawing.Bitmap;
if (bmp == null)
return null;
return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bmp.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
Run Code Online (Sandbox Code Playgroud)
我更喜欢在初始化期间转换图像一次并保留图像源列表.我也假设我需要一个依赖属性来绑定控件,但我不知道如何使用这个图像源列表进行设置:
// Dependancy Property for the North Image
public static readonly DependencyProperty NorthImagePathProperty
= DependencyProperty.Register(
"NorthImagePath",
typeof(ImageSource),
typeof(MainWindow),
new PropertyMetadata("**Don't …Run Code Online (Sandbox Code Playgroud) 我正在尝试基于仅包含公共字段的现有类型创建动态类型.新的动态类型还必须从仅具有完全实现的方法的不同基类型继承.
我创建了TypeBuilder指定基类型然后我将公共字段添加到它,最后我调用CreateType().生成的错误消息是:
"无法从程序集'MyDynamicAssembly,Version = 0.0.0.0,Culture = neutral,PublicKeyToken = null'加载类型'InternalType',因为字段'first'未被赋予显式偏移量."
对我来说这意味着该CreateType方法在基类中寻找公共字段"first",这是一个问题,因为它不在那里.为什么它认为添加的字段应该在基类中?或者,我是否误解了这个例外?
这是代码:
public class sourceClass
{
public Int32 first = 1;
public Int32 second = 2;
public Int32 third = 3;
}
public static class MyConvert
{
public static object ToDynamic(object sourceObject, out Type outType)
{
// get the public fields from the source object
FieldInfo[] sourceFields = sourceObject.GetType().GetFields();
// get a dynamic TypeBuilder and inherit from the base type
AssemblyName assemblyName
= new AssemblyName("MyDynamicAssembly"); …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用WCF设置发布/订阅系统,并且WCF服务器位于Windows服务中.绑定是net.TCP.该服务向客户端提供"订阅"方法,以便客户端可以将回调处理程序注册到将从链接到服务器的DLL引发的事件.在Subscribe方法中,我尝试使用OperationContext.Current.GetCallbackChannel方法获取回调通道.当我尝试这个时,OperationContext.Current属性返回NULL.
谁能告诉我在什么情况下这个属性会返回null?我错过了设置吗?我将在下面包含服务代码和接口代码.我在Visual Studio 2012和目标框架4.5中使用c#.
服务:
namespace WService
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class WcfPublisherService : IWcfPublisherContract
{
IOALogic logic = new OAControlExample();
IWcfSubscriberContract _callback = null;
public void Subscribe()
{
_callback = OperationContext.Current.GetCallbackChannel<IWcfSubscriberContract>();
logic.BarriersChanged += logic_BarriersChanged;
}
public void UnSubscribe()
{
logic.BarriersChanged -= logic_BarriersChanged;
}
void logic_BarriersChanged(object sender, BarriersChangedEventArgs e)
{
_callback.BarriersChanged(e.BarrierLines);
}
}
}
Run Code Online (Sandbox Code Playgroud)
接口:
namespace WService
{
[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IWcfSubscriberContract))]
public interface IWcfPublisherContract
{
[OperationContract(IsOneWay=false, IsInitiating=true)]
void Subscribe();
[OperationContract(IsOneWay = false, IsTerminating=true)]
void UnSubscribe();
} …Run Code Online (Sandbox Code Playgroud) 我有一个 Windows 服务最终抛出“内存不足”异常。它是用 C# 编写的,运行在 Windows 7 上。
\n\n是的,我已经在 Stack Overflow 以及互联网上的其他地方阅读了有关此问题的现有问题。事实上,我发现了 Eric Lippert 写的一篇很棒的文章,名为“内存不足”并不指物理内存,他在其中对这种情况提供了非常清晰的解释。
\n\n\n\n在这篇文章中,他用这样的语句来提到“内存不足”:“现在这是一个用词不当。它确实应该是一个 \xe2\x80\x9cunable 找不到足够的连续地址空间\xe2\x80\x9d 错误;那里\xe2\x80\x99s 有足够的内存,因为内存等于磁盘空间”。
\n\n他还指出:
\n\n\n\n\n\xe2\x80\x9cout of memory\xe2\x80\x9d 错误几乎不会发生,因为\xe2\x80\x99s 没有足够的可用存储空间;正如我们\xe2\x80\x99所看到的,存储就是磁盘空间,而如今磁盘空间非常大。相反,发生 \xe2\x80\x9cout of memory\xe2\x80\x9d 错误是因为进程无法在其虚拟地址空间中找到足够大的连续未使用页面部分来执行请求的映射。
\n
当我查看 PerfMon 中的服务时,我看到“提交内存”、“工作集”和“私有内存”下的列,所有这些列都在不断增长。我确信有一些字符串不断被添加到或一些列表没有被清除。
\n\n我的问题是,C# 中是否有一些技术可以用来监视代码本身内单个对象或集合的内存请求,例如可以监视系统的类库? 如果是这样,我可以从代码本身内部监视连续的内存请求。
\n我已将我的应用程序精简到最小的 POC,但仍然得到相同的效果。看来 ExecuteScalarAsync 的行为类似于同步调用。我认为,当遇到等待时,异步方法中的其余代码将暂停,并且消息泵返回并从消息队列获取另一条消息,从而允许 UI 继续。当标量调用完成时,异步方法的剩余部分将被放回到消息队列中,以便它完成。
当这个小应用程序运行时,TestConnectionAsync 方法会挂起 UI,并且不会执行其他消息,直到 ExecuteScalarAsync 调用超时。
我做错了什么,还是这个异步方法的行为像同步方法?
该表单有两个按钮。第一个运行异步方法,第二个尝试使用令牌取消异步方法。我从来没有机会点击第二个按钮。
Form1.cs
public partial class Form1 : Form
{
private DB _db = new DB();
private string _nl = Environment.NewLine;
public Form1()
{
InitializeComponent();
}
private async void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "Starting" + _nl;
string resultString
= (string) await _db.TestConnectionAsync();
textBox1.AppendText(resultString + _nl);
textBox1.AppendText("Done" + _nl);
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.AppendText("Cancelling..." + _nl);
_db.CancelTest();
textBox1.AppendText("Submitted Cancel Token" + _nl);
} …Run Code Online (Sandbox Code Playgroud) c# ×4
async-await ×1
asynchronous ×1
callback ×1
imagesource ×1
inheritance ×1
memory ×1
reflection ×1
sqlcommand ×1
typebuilder ×1
wcf ×1
wpf ×1