使用Java.
我为一些计算等记录小物件,我只需要最后的一千个.所以我想先发布垃圾收集器.但是因为从ArrayLists中删除是昂贵的...
以下是重要的(不能改变)
这可以改变:
ArrayList<MyObject>
我的猜测:
我能做些什么来快速迭代并同时快速释放旧对象?
(我知道这可能是重复的,但我不理解其他线程)
我正在使用C#,我有一个dll
需要将int数组(或指向int数组的指针)作为参数的第三方。如何在C#和C / C ++之间封送一个int数组?函数声明如下:
// reads/writes int values from/into the array
__declspec(dllimport) void __stdcall ReadStuff(int id, int* buffer);
Run Code Online (Sandbox Code Playgroud)
在C中int*
会是指针吗?所以我很困惑是否必须使用IntPtr
或可以使用int[]
(首选)?我认为这可能没问题:
[DllImport(dllName)]
static extern void ReadStuff(int id, [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_I4)] ref int[] buffer);
// call
int[] array = new int[12];
ReadStuff(1, ref array);
Run Code Online (Sandbox Code Playgroud)
那行得通吗?还是我必须以安全代码在C#中声明此函数?
是否可以AcceptSocket
在TcpListener
超时的对象上进行一次中断?
TcpListener server = new TcpListener(localIP, port);
server.Start();
while (!shuttingDown)
try
{
Socket client = server.AcceptSocket();
if (client != null)
{
// do client stuff
}
}
catch { }
Run Code Online (Sandbox Code Playgroud)
尝试 BeginAccept 和 EndAccept:如果 3 秒内没有客户端,我该如何结束接受?(我试图在这里近似解决方案)
server.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpClientCallback), server);
Thread.Sleep(3000);
server.EndAcceptTcpClient(???);
Run Code Online (Sandbox Code Playgroud) 我有几个派生类Form
.我想要计算Form2等1个特殊类的所有开放表单实例.这是一个简单的WinForms应用程序(没有Mdi).
该应用无法多次启动.所以它只是在这个应用程序中计算窗口.
我的想法:
Application
给我一个打开的窗口列表?这与字符串的大小无关.
我指定了绘制字符串的字体.现在我需要1个字符的精确宽度(以像素为单位).我只允许具有静态字符宽度的字体(W和i在绘制时具有相同的宽度).如果我使用大小为10的"Consolas",我的估计宽度为7.75f
.我可以直接以某种方式检索此信息吗?
绘图时我不需要这些信息,但是当我想设置光标位置等时,我不知道光标下面有什么文字.
编辑: MeasureString给我11.99653作为字符宽度.不知道为什么.但我不能使用这个值.我估计7.75f而且非常精确.如果我想改变字体大小,我会遇到麻烦.
什么是最短的路INC或十二月的绝对值int
,但保持标志(+
-
)?
int a = 5;
int b = -7;
// increment abs of both
// a -> 6
// b -> -8
// incrementing 0 wasn't interesting me, I guess it can stay 0
Run Code Online (Sandbox Code Playgroud)
(我确定它并不是那么难,但我可能最终会得到一些代码.但我想知道我是否可以制作那么短的1-liner?).我还删除了C#标签,因为这可能对各种语言都很有用.
我有一个用黑色Windows控制台打开的C#控制台应用程序(A)。有时在启动时,它会从另一个需要焦点的程序(B)中抢走焦点。
问:我怎样才能给从焦点回到A.exe
到B.exe
?
A -> Focus -> B
Run Code Online (Sandbox Code Playgroud)
当我更改ToolStripLabel
上下文菜单中的文本时,当我更改菜单项的文本时,上下文菜单不会按预期自动调整大小。
那么看起来像这样:
如何使上下文菜单正确调整大小?
我可以更改真实菜单项的文本,但我认为这是一个肮脏的解决方案。
测试表格:( 使用鼠标左键,左侧和右侧)
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1: Form
{
private ToolStripLabel menuLabel;
private void CreateNewContextMenu()
{
ContextMenuStrip = new ContextMenuStrip();
// label
menuLabel = new ToolStripLabel("hello");
menuLabel.ForeColor = Color.Blue;
ContextMenuStrip.Items.Add(menuLabel);
// items
ContextMenuStrip.Items.Add("Test");
ContextMenuStrip.Items.Add("Cut");
ContextMenuStrip.Items.Add("&Copy");
ContextMenuStrip.Items.Add("&Paste");
ContextMenuStrip.Items.Add("&Delete");
}
protected override void OnMouseClick(MouseEventArgs e)
{
CreateNewContextMenu();
menuLabel.Text = "hello world hello world hello world";
Point p = PointToScreen(Point.Empty);
// left
if (e.X < ClientSize.Width / 2)
ContextMenuStrip.Show(p.X + 8, …
Run Code Online (Sandbox Code Playgroud) 简单:如何明确封送WinAPi函数的结果?
BOOL
或所有类型INT
(句柄在非托管代码中也是int).
// common signature
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
static extern int GetFileAttributes([MarshalAs(UnmanagedType.LPStr)] string filename);
// my prefered signature because easy to handle the result
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
static extern FileAttributes GetFileAttributes([MarshalAs(UnmanagedType.LPStr)] string filename);
Run Code Online (Sandbox Code Playgroud)
因为FileAttributes
是enum并且每个值都可以很容易地被投射到int
我确定我用这种表示法是安全的.但是我可以像参与参数一样编组这个签名中的结果吗?我实际上只知道Marshal
类和MarshalAs
属性.
我正在尝试找到一种优雅的方法来迭代列表,同时删除项目.
我知道这个解决方案.但我的条件更难:
问题:这可能吗?如果有,怎么样?
我有想法将对象标记为已删除/不活动.当我稍后再次迭代时,我将删除它们而不要求它们做事情.迭代将经常重复,这就是为什么每个对象在每次迭代时必须正好转1圈.那会有用吗?
伪代码:
class Foo
{
public void DoStuff()
{
// do other stuff
if (condition)
Kill(x); // should result in list.RemoveAt(x) somehow
}
}
class Program
{
[STAThread]
static void Main(string[] args)
{
List<Foo> list = new List<Foo>();
for (int i = 0; i < 15; i++)
list.Add(new Foo());
for (int i = 0; …
Run Code Online (Sandbox Code Playgroud) c# ×8
winapi ×3
list ×2
marshalling ×2
winforms ×2
arrays ×1
binary ×1
c ×1
console ×1
contextmenu ×1
drawstring ×1
focus ×1
fonts ×1
int ×1
iteration ×1
java ×1
performance ×1
sockets ×1
toolstrip ×1
windows ×1