是否可以在另一个进程的主窗口上显示WinForms模式表单?
例如,我的WinForms应用程序包含一个表单,该表单是模态的,而另一个进程的主窗口是PID x.
当编组到类类型的实例时,该类型需要无参数构造函数.否则Marshal.PtrToStructure会抛出MissingMethodException.当编组到包含嵌套类的实例时,它们不需要无参数构造函数.
请参阅下面的示例代码 编组工作正常,输出为1 2 3 4.但为什么类DummyNested不需要无参数构造函数?
static void Main(string[] args)
{
var buffer = new byte[10];
buffer[0] = 1;
buffer[1] = 2;
buffer[2] = 3;
buffer[3] = 4;
Dummy dummy;
unsafe
{
fixed(byte* bufferPtr = &buffer[0])
{
dummy = (Dummy)Marshal.PtrToStructure(new IntPtr(bufferPtr), typeof(Dummy));
}
}
Console.WriteLine("{0} {1} {2} {3}", dummy.m_nested1.m_data1, dummy.m_nested1.m_data2, dummy.m_nested2.m_data1, dummy.m_nested2.m_data2);
Console.ReadKey();
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private class DummyNested
{
public byte m_data1;
public byte m_data2;
public DummyNested(byte data1, byte data2)
{ …
Run Code Online (Sandbox Code Playgroud) 我有以下代码,其目标是等待所有给定的等待句柄,但可以通过特定的等待句柄取消:
public static bool CancelableWaitAll(WaitHandle[] waitHandles, WaitHandle cancelWaitHandle)
{
var waitHandleList = new List<WaitHandle>();
waitHandleList.Add(cancelWaitHandle);
waitHandleList.AddRange(waitHandles);
int handleIdx;
do
{
handleIdx = WaitHandle.WaitAny(waitHandleList.ToArray());
waitHandleList.RemoveAt(handleIdx);
}
while (waitHandleList.Count > 1 && handleIdx != 0);
return handleIdx != 0;
}
Run Code Online (Sandbox Code Playgroud)
这仅适用于ManualReset事件.使用AutoReset事件时,WaitAny会重置所有已发出信号的事件,但仅返回第一个发出信号的事件(根据MSDN).
任何想法如何在没有轮询的情况下以适当的方式完成AutoReset事件?
我正在将VSF中的WPF应用程序移动到VS 2012.
我有一个主要的可执行文件,其中包含App.xaml和一个单独的类库,用于所有WPF内容.在VS2012(SP3)中,XAML设计器没有显示在App.xaml中定义的MergedResourceDictionary中的任何样式,VS 2010确实......
出于测试目的,我将App.xaml移动到了我的类库.之后,VS 2012设计师正确地展示了一切.
由于类库不能包含App.xaml,因此这不是一个解决方案.
有任何想法吗?
我的App.xaml如下所示:
<Application x:Class="MyApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Startup="AppStartup">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/OtherAssemblyName;component/Resources/Resource1.xaml" />
<ResourceDictionary Source="pack://application:,,,/OtherAssemblyName;component/Resources/Resource2.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
我有以下C#代码:
foreach (var x in m_collection)
{
m_actionCollection.Add(() =>
{
x.DoSomething();
});
}
Run Code Online (Sandbox Code Playgroud)
如果我在VS2010中编译解决方案,则会生成以下代码(使用IlSpy进行反编译):
foreach (var x in m_collection)
{
m_actionCollection.Add(() =>
{
x.DoSomething();
});
}
Run Code Online (Sandbox Code Playgroud)
如果我通过MsBuild命令行编译解决方案,则会生成以下代码:
using (List<X>.Enumerator enumerator = this.m_collection.GetEnumerator())
{
while (enumerator.MoveNext())
{
X x = enumerator.Current;
m_actionCollection.Add(() =>
{
x.DoSomething();
});
}
}
Run Code Online (Sandbox Code Playgroud)
项目文件包含
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
Run Code Online (Sandbox Code Playgroud)
并且MsBuild命令行如下所示: C:\Windows\Microsoft.NET\Framework\v4.0.30319\MsBuild.exe Solution.sln /property:Configuration=Debug /property:Platform=x86 /maxcpucount /nologo /toolsversion:4.0
所以我假设编译器是相同的,但它不是......
必须完成/检查才能通过MsBuild命令行和VS 2010建立完全相同的环境?
是否可以在使用自定义封送拆收器的结构上使用Marshal.SizeOf()?
例如:
struct Abcde {
public int test1;
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MyCustomMarshaler)]
public string customString1;
}
Run Code Online (Sandbox Code Playgroud)
如果我打电话:
var size = Marshal.SizeOf(typeof(Abcde));
Run Code Online (Sandbox Code Playgroud)
抛出一个异常,表示无法计算有意义的大小或偏移量。我注意到ICustomMarshaler有一个名为GetNativeDataSize()的方法,但是无论我在那里返回什么都会抛出异常。
处理NuGet包的最佳方法是什么,这些包将用于构建或开发VS解决方案,但不能直接在任何项目中引用?
例如NUnit GUI,OpenCover,NAnt等......需要所有这些工具来构建最终产品,但不需要从任何项目中引用.这只需要在解决方案和构建过程中使用,但从不直接用作对解决方案中任何项目的引用.
我正在使用VS 2015的NuGet扩展以及NuGet命令行......
c# ×6
marshalling ×2
.net ×1
modal-dialog ×1
msbuild ×1
nuget ×1
pinvoke ×1
waithandle ×1
winforms ×1
wpf ×1
xaml ×1