using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MyClass myClass = new MyClass();
myClass.StartTasks();
}
}
class MyClass
{
int[] arr;
public void StartTasks()
{
arr = new int[2];
arr[0] = 100;
arr[1] = 101;
for (int i = 0; i < 2; i++)
{
Task.Factory.StartNew(() => WorkerMethod(arr[i])); // IndexOutOfRangeException: i==2!!!
}
}
void WorkerMethod(int i)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
似乎i ++在循环迭代完成之前再次执行.为什么我会得到IndexOutOfRangeException?
我想用c#interop从用c编写的dll中调用一个函数.我有头文件.看看这个:
enum CTMBeginTransactionError {
CTM_BEGIN_TRX_SUCCESS = 0,
CTM_BEGIN_TRX_ERROR_ALREADY_IN_PROGRESS,
CTM_BEGIN_TRX_ERROR_NOT_CONNECTED
};
#pragma pack(push)
#pragma pack(1)
struct CTMBeginTransactionResult {
char * szTransactionID;
enum CTMBeginTransactionError error;
};
struct CTMBeginTransactionResult ctm_begin_customer_transaction(const char * szTransactionID);
Run Code Online (Sandbox Code Playgroud)
如何从c#调用ctm_begin_customer_transaction.const char*mapps很好用于字符串,但尽管有各种尝试(查看stackoverflow和其他站点),但我无法编组返回结构.如果我定义函数返回IntPtr它可以工作正常...
编辑 我将返回类型更改为IntPtr并使用:CTMBeginTransactionResult structure =(CTMBeginTransactionResult)Marshal.PtrToStructure(ptr,typeof(CTMBeginTransactionResult)); 但它抛出AccessViolationException
我也尝试过:
IntPtr ptr = Transactions.ctm_begin_customer_transaction("");
int size = 50;
byte[] byteArray = new byte[size];
Marshal.Copy(ptr, byteArray, 0, size);
string stringData = Encoding.ASCII.GetString(byteArray);
Run Code Online (Sandbox Code Playgroud)
stringData =="70e3589b-2de0-4d1e-978d-55e22225be95\0 \"\ 0\0\a\0\0\b\b?"此时."70e3589b-2de0-4d1e-978d-55e22225be95"是结构中的szTransactionID.枚举在哪里?它是下一个字节吗?
在WinForms中我使用:
我应该将什么用于非Winforms多线程应用程序?
考虑下面C#.NET 4.0中的完整代码:
using System;
using System.Threading.Tasks;
namespace ExceptionFun
{
class Program
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Task.Factory.StartNew(() =>
{
throw new Exception("Oops, someone forgot to add a try/catch block");
});
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
//never executed
Console.WriteLine("Logging fatal error");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在stackoverflow上看到了大量类似的问题,但没有一个包含令人满意的答案.大多数答案都是类型:"您应该在代码中包含正确的exeption处理"或"使用AppDomain.CurrentDomain.UnhandledException".
编辑:似乎我的问题被误解了,所以我重新制定了它并提供了一个较小的代码示例.
我知道这个问题并且我同意答案,但是我可以使用log4net执行以下操作吗?
而不是:
2013-04-09 12:54:47.093 INFO Main: Line 1 Line 1 Line 1
Line 2 Line 2 Line 2
Line 3 Line 3 Line 3
2013-04-09 12:54:47.093 INFO Main: Line 1 Line 1 Line 1
Line 2 Line 2 Line 2
Line 3 Line 3 Line 3
Run Code Online (Sandbox Code Playgroud)
我能有......吗:
2013-04-09 12:54:47.093 INFO Main: Line 1 Line 1 Line 1
Line 2 Line 2 Line 2
Line 3 Line 3 Line 3
2013-04-09 12:54:47.093 INFO Main: Line 1 Line 1 Line …Run Code Online (Sandbox Code Playgroud) 我正在使用TypeScript和来自绝对类型的three.d.ts。我一直在使用没有问题THREE.JSONLoader,但我怎么使用OBJLoader从这里的打字稿项目。我可能需要创建一个 OBJLoader.d.ts 文件,但我不知道该怎么做以及如何使用创建的定义。我试图简单地复制 THREE.JSONLoader 定义并将其重命名为 OBJLoader,但这不起作用。
是否可以在PowerShell中打印变量名称?
$myVariable = "My Value"
Write-Host "The variable name is ?????"
Run Code Online (Sandbox Code Playgroud)
预期产量:
变量名是myVariable
问题2(更棘手):是否可以打印传递给PowerShell中的函数的变量名?
Function MyFunc($myArg){
Write-Host "The variable name is ?????"
}
$myVariable = "My Value"
MyFunc $myVariable
Run Code Online (Sandbox Code Playgroud)
预期产量:
变量名是myVariable
我想在WPF中创建一个用户控件,它可以使用单空格字体绘制一个nxm字符矩阵.控件将尽可能快地接受字符串[](目标为60 fps)并在屏幕上绘制它.
我需要与mplayer ascii播放类似的性能.
所有字符都使用相同的单空格字体绘制,但根据某些规则可能具有不同的颜色和背景(类似于VS中的语法高亮).
我已经在C#WinForms中实现了解决方案而没有任何问题,并且达到了60 FPS,但是当我想学习如何在WPF中执行此操作时,我只发现了几篇描述WPF性能和冲突信息问题的文章和帖子.
那么在这种情况下实现最高性能的最佳方法是什么?
我尝试过的一种天真的方法是:
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Random rand = new Random();
public MainWindow()
{
InitializeComponent();
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(1);
timer.Tick += timer_Tick;
timer.Start();
}
string GenerateRandomString(int length)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++)
{
sb.Append(rand.Next(10));
}
return sb.ToString();
}
void timer_Tick(object sender, EventArgs e)
{
myTextBlock.Inlines.Clear(); …Run Code Online (Sandbox Code Playgroud)