我有一个长时间运行的代码,我想暂停恢复功能。在 start 方法中,我正在编写我的代码并开始新的异步任务。但我希望根据需要暂停/恢复此任务。
var myTask;
public void start()
{
myTask = Task.Factory.StartNew(() =>
{
///my business logic
});
}
Run Code Online (Sandbox Code Playgroud) 我最近更新了我的 Unity(从 5.4.1f1 更新到 5.5.0f3),现在我的一款游戏中的 Rigidbody2D 似乎出现了一些问题。
所以基本上我曾经使用这段代码来使我的游戏对象(玩家)变得不易处理并脱离物理控制:
Player.GetComponent<Rigidbody2D>().isKinematic = true;
Run Code Online (Sandbox Code Playgroud)
在 Unity 更新后,此功能无法正常工作,并且在将玩家设置为运动学后,它会继续沿与“动态”时相同的方向移动(但无法控制)。注意:Unity 5.4.1f1 中并非如此。
所以我进入 Rigidbody2D 组件并注意到它发生了变化。 我的旧 RigidBody2D 的外观以及我的新 RigidBody2D 现在的外观
“kinematic”选项已移至“body type”选项中,并且在运行时它确实使用“.isKinematic = true”字符串将主体类型更改为 Kinematic(但如前所述,它无法正常工作)。
因此,我尝试手动将主体类型值更改为“静态”,并发现效果非常好!
所以我的问题是:如何在代码中将主体类型更改为静态?(如“isKinematic”),以及 Rigidbody2D(和运动学?)的变化发生了什么?
我成功地将单色位图打印到我的打印机,但是当打印项目时,图像右侧有一个大的黑色条纹.
这是原作

(扫描)打印机打印的内容

用于生成二进制blob的代码
Rectangle rect = new Rectangle(0, 0, Bitmap.Width, Bitmap.Height);
System.Drawing.Imaging.BitmapData bmpData = null;
byte[] bitVaues = null;
int stride = 0;
try
{
bmpData = Bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, Bitmap.PixelFormat);
IntPtr ptr = bmpData.Scan0;
stride = bmpData.Stride;
int bytes = bmpData.Stride * Bitmap.Height;
bitVaues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, bitVaues, 0, bytes);
}
finally
{
if (bmpData != null)
Bitmap.UnlockBits(bmpData);
}
string str = String.Format("GW{0},{1},{2},{3},", X, Y, stride, Bitmap.Height);
byte[] ascii = Encoding.ASCII.GetBytes(str);
byte[] …Run Code Online (Sandbox Code Playgroud) 我正在使用GDI +,我正在使用的图像是一个1bbp的图像.我想做的是在图像上绘制一个矩形,该矩形下的所有内容都将被反转(白色像素将变为黑色,黑色像素变为白色).
我见过的所有示例代码都是针对8位RGB色阶图像,我不认为他们使用的技术对我有用.
这是我到目前为止的代码.这是父控件,其中一个Epl2.IDrawableCommand将是执行反转的命令.
public class DisplayBox : UserControl
{
(...)
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
(...)
using (Bitmap drawnLabel = new Bitmap((int)((float)Label.LabelHeight * _ImageScaleFactor), (int)((float)Label.LableLength *(int) _ImageScaleFactor), System.Drawing.Imaging.PixelFormat.Format1bppIndexed))
{
using (Graphics drawBuffer = Graphics.FromImage(drawnLabel))
{
(...)
foreach (Epl2.IDrawableCommand cmd in Label.Collection)
{
cmd.Paint(drawBuffer);
}
(...)
}
}
}
}
}
public class InvertArea : IDrawableCommand
{
(...)
public Rectangle InvertRectangle {get; set;}
public void Paint(Graphics g)
{
throw new NotImplementedExecption();
}
}
Run Code Online (Sandbox Code Playgroud)
我应该Paint(Graphic g) …
我试图去除字符串中不是字母数字或空格的所有内容,因此我创建了正则表达式
private static Regex _NonAlphaChars = new Regex("[^[A-Za-z0-9 ]]", RegexOptions.Compiled);
Run Code Online (Sandbox Code Playgroud)
但是,当我打电话给_NonAlphaChars.Replace("Scott,", "");它返回"Scott,"
我做错了什么不匹配,?
我有一个更清洁的类实现IDataReader,它的作用是它过滤DateTime掉SQL Server DateTime范围之外的任何值,然后返回DBNull(此类的输出被输入到a SqlBulkCopy,我的数据源可以返回1之前的日期/ 1753分之1)
我的问题是接口的一个函数返回一个新的IDataReader,我想要的是任何派生类都不需要重写函数来返回一个新的对象本身.这是一个例子
public class SqlServerDataCleaner : IDataReader
{
public SqlServerDataCleaner(IDataReader dataSource)
{
this.dataSource = dataSource;
//(Snip)
}
//(Snip)
public virtual IDataReader GetData(int i)
{
return new SqlServerDataCleaner(dataSource.GetData(i));
}
}
class SqlServerDataCleanerDerived : SqlServerDataCleaner
{
public SqlServerDataCleanerDerived (IDataReader dataSource)
: base(dataSource)
{
}
//(Snip)
//Need to return the correct class
public override IDataReader GetData(int i)
{
return new SqlServerDataCleanerDerived (dataSource.GetData(i));
}
}
Run Code Online (Sandbox Code Playgroud)
是否有任何方法可以删除对覆盖的需要,GetData因此父类将自动在其副本中创建类的派生形式GetData(假设所有派生类将始终具有 …
如果值不存在,我想将其初始化为 0。否则它应该增加现有值。
ConcurrentDictionary<int, int> dic = new ConcurrentDictionary<int, int>();
dic.AddOrUpdate(1, 0, (key, old) => old++);
dic.AddOrUpdate(2, 0, (key, old) => old++);
Run Code Online (Sandbox Code Playgroud)
此时,字典的键为 1 和 2,每个值为 0。
dic.AddOrUpdate(1, 0, (key, old) => old++);
Run Code Online (Sandbox Code Playgroud)
此时,键 1 的值应该是 1,而键 2 的值应该是 0,但是,两者的值都是 0。知道为什么吗?
当我尝试创建字典时遇到问题,其中键是 System.Enum。问题是,像这样的字典会分配垃圾,因为默认的 EqualityComparer 不是最好的之一。我尝试编写自己的比较器,但没有成功。这有可能吗?
public enum MyEnum
{
One, Two, Three
}
public Dictionary<Enum, string> dict = new Dictionary<Enum, string>();
public void Test()
{
this.dict.Add(MyEnum.One, "One");
this.dict.Add(MyEnum.Two, "Two");
this.dict.Add(MyEnum.Three, "Three");
string result;
this.dict.TryGetValue(MyEnum.Two, out result); // <-- memory alocation :-(
}
Run Code Online (Sandbox Code Playgroud) 这些都回答了如何设置默认启动项目,但是没有回答如何更改“ Default Project”属性。我一直在关注配置ASP.Net Identity的本教程。在页面的大约2/3处,有一个数字列表,其中#1为:
- 将
Default Project属性设置为WebApi。- 打开
Package Manager Console。- 通过定位我们的自定义上下文来启用迁移:Enable-Migrations-
contexttypename AppUsersDbContext- 添加迁移:
Add-Migration InitialUpdate-Database
其他答案则涉及更改项目顺序以将其中一个设置为默认启动项目,但这听起来好像有一些我需要更改的名为默认项目的属性。
只是一些背景知识-本教程从创建解决方案开始。仅添加了一个项目,因此设置默认的启动项目没有任何意义。
本教程没有日期,但是谈到使用SQL Server 2016,所以它是相当新的。
感谢您可以提供的任何帮助,到目前为止,该项目运行良好,但是在完成其余列表后,出现了问题。我不知道这是否应该归咎于我,但我认为我可能会误解他的意思,并且做错了什么。
再次感谢您的帮助,
泰勒
我正在尝试做一个相当简单的任务但是我的经验是在.net而不是在vb6中.鉴于两个字符串(比如"10/17/94"和"10/17/95"这个例子),我想返回一个字符串表达式
X年
//Date_Due and Date_Time_Performed are strings in the mm/dd/yy format
Duration = (Year(CDate(Date_Due)) - Year(CDate(Date_Time_Performed))) & " years"
Run Code Online (Sandbox Code Playgroud)
但这给了我一个runtime error '13' Type Mismatch.
有什么建议?
编辑:这些答案都没有得到解决.必须将转换结果添加到"years"字符串.我需要字符串表示而不是int.
c# ×8
.net ×1
.net-4.0 ×1
async-await ×1
asynchronous ×1
bitmap ×1
c#-4.0 ×1
colors ×1
constructor ×1
datetime ×1
gdi+ ×1
inheritance ×1
overriding ×1
printing ×1
regex ×1
vb6 ×1