在stream.DataAvailable为false之前,如何进行以下可观察重复?目前它看起来永远不会停止.
在Defer部分内的AsyncReadChunk和Observable.Return使OnNext调用然后OnCompleted调用.当Repeat接收OnNext调用时,它将它传递给TakeWhile.当TakeWhile不满意时,它完成了observable,但我认为在OnNext之后出现的OnCompleted非常快,它会使Repeat重新订阅observable并导致无限循环.
我该如何纠正这种行为?
public static IObservable<byte[]> AsyncRead(this NetworkStream stream, int bufferSize)
{
return Observable.Defer(() =>
{
try
{
return stream.DataAvailable ? AsyncReadChunk(stream, bufferSize) : Observable.Return(new byte[0]);
}
catch (Exception)
{
return Observable.Return(new byte[0]);
}
})
.Repeat()
.TakeWhile((dataChunk, index) => dataChunk.Length > 0);
}
Run Code Online (Sandbox Code Playgroud) 有没有一种方法来解码在C#中用HttpUtility.JavaScriptStringEncode()编码的字符串?
示例编码字符串:
<div class=\"header\"><h2>\u00FC<\/h2><script>\n<\/script>\n
Run Code Online (Sandbox Code Playgroud)
我的临时解决方案是:
public static string JavaScriptStringDecode(string source)
{
// Replace some chars.
var decoded = source.Replace(@"\'", "'")
.Replace(@"\""", @"""")
.Replace(@"\/", "/")
.Replace(@"\t", "\t")
.Replace(@"\n", "\n");
// Replace unicode escaped text.
var rx = new Regex(@"\\[uU]([0-9A-F]{4})");
decoded = rx.Replace(decoded, match => ((char)Int32.Parse(match.Value.Substring(2), NumberStyles.HexNumber))
.ToString(CultureInfo.InvariantCulture));
return decoded;
}
Run Code Online (Sandbox Code Playgroud) 大约有1000个任务在运行,但有时我会收到任务调度程序抛出的以下内存异常.可能是什么原因以及如何避免它.
System.Threading.Tasks.TaskSchedulerException: An exception was thrown by a TaskScheduler. ---> System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
at System.Threading.Thread.StartInternal(IPrincipal principal, StackCrawlMark& stackMark)
at System.Threading.Thread.Start(StackCrawlMark& stackMark)
at System.Threading.Thread.Start(Object parameter)
at System.Threading.Tasks.ThreadPoolTaskScheduler.QueueTask(Task task)
at System.Threading.Tasks.Task.ScheduleAndStart(Boolean needsProtection)
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ScheduleAndStart(Boolean needsProtection)
at System.Threading.Tasks.Task.InternalStartNew(Task creatingTask, Object action, Object state, CancellationToken cancellationToken, TaskScheduler scheduler, TaskCreationOptions options, InternalTaskOptions internalOptions, StackCrawlMark& stackMark)
at System.Threading.Tasks.TaskFactory.StartNew(Action action, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
at App.StartReadSocketTask()
Run Code Online (Sandbox Code Playgroud) 我做了一个控件来将不同线程的消息记录到屏幕上.它使用富文本框来显示格式化文本.
当有20个线程每隔200-250ms追加它们的消息时,主UI在一段时间内变得无响应,并且在处理消息等待之后,UI开始再次响应.当线程运行时,窗口的移动不平滑.
写入富文本框的消息与锁同步.
你有什么建议可以改善表现?我打算运行100个线程.
这是我的代码.我将控制台输出重定向到它,它记录正在进行的所有操作,并以格式化的形式显示在富文本框中.
public void RedirectStandardOutput()
{
Console.SetOut(ConsoleStream);
System.Diagnostics.Debug.Listeners.Add(new System.Diagnostics.TextWriterTraceListener(Console.Out));
System.Diagnostics.Debug.AutoFlush = true;
}
Run Code Online (Sandbox Code Playgroud)
控制台重定向后所有Console.WriteLine("bla bla"); 被写入屏幕.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CoreLib.Parsers;
namespace ConsoleWidget
{
public class ConsoleStream : System.IO.TextWriter
{
private readonly object _textBoxLock = new object();
public RichTextBox TextBox { get; set; }
public List<TextFormat> TextFormatList { get; set; }
public bool AutoClear { get; set; }
public int AutoClearLength { get; set; }
public bool AutoSave { get; …Run Code Online (Sandbox Code Playgroud) 我通过下面的存储过程随机从DB中选择可用的登录信息.但是当多个线程想要获取可用的登录信息时,虽然我正在更新记录的时间戳字段,但是会返回重复的记录.
如何在此处锁定行,以便一次返回的记录不会再次返回?
把
WITH(HOLDLOCK,ROWLOCK)
没有帮助!
SELECT TOP 1 @uid = [LoginInfoUid]
FROM [ZPer].[dbo].[LoginInfos]
WITH (HOLDLOCK, ROWLOCK)
WHERE ([Type] = @type)
Run Code Online (Sandbox Code Playgroud)
...... ......
ALTER PROCEDURE [dbo].[SelectRandomLoginInfo]
-- Add the parameters for the stored procedure here
@type int = 0,
@expireTimeout int = 86400 -- 24 * 60 * 60 = 24h
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
DECLARE @processTimeout int …Run Code Online (Sandbox Code Playgroud) c# ×4
asyncsocket ×1
database ×1
encoding ×1
javascript ×1
json ×1
locking ×1
rowlocking ×1
sql ×1
t-sql ×1
task ×1
winforms ×1