如果它需要太多时间我必须Timer取消Thread它.
System.Timers.Timer timer_timers = new System.Timers.Timer();
Thread thread = new Thread(startJob);
thread.Name = "VICTIM_THREAD";
Run Code Online (Sandbox Code Playgroud)
在启动Thread方法时,我启动Timer并将当前Thread作为参数传递给事件.
public void startJob()
{
Debug.WriteLine("Name: " + Thread.CurrentThread.Name);
timer_timers.Elapsed += (sender, e) => T_Elapsed(sender, e, Thread.CurrentThread);
timer_timers.Interval = 5000;
// Start simulation process
while (true)
{
Thread.Sleep(700);
Debug.WriteLine("Thread: " + Thread.CurrentThread.Name + " ALIVE: " + thread.IsAlive);
}
}
Run Code Online (Sandbox Code Playgroud)
定时器事件:
private void T_Elapsed(object sender, ElapsedEventArgs e, Thread currentThread)
{
// EDIT: show the correct NAME! of the …Run Code Online (Sandbox Code Playgroud) 我做了一个测验,其中用户必须输入一个数字(例如4),TextBox然后程序将检查输入的数字是否正确.不幸的是,我在使用这部分代码时遇到了一些麻烦.
所以目前我有这个代码:
if(textbox1.Text=4)
Run Code Online (Sandbox Code Playgroud)
但是4带有错误消息的下划线:
不能隐式地将类型'int'转换为'string'.
我可以麻烦大家帮我找出我的代码有什么问题吗?非常感谢!!
在将过滤器添加到我的ComboBox下拉列表时需要一些帮助(Windows Forms Visual Studio 2015)
下拉列表如下所示:
public ReconciliationReport()
{
InitializeComponent();
AppDomain.CurrentDomain.AssemblyResolve += FindDLL;
this.sRootDirectory = Properties.Resources.sRootDirectory;
string[] arrProjectList = Directory.GetDirectories(sRootDirectory).Select(Directory => Path.GetFileName(Directory)).ToArray();
Array.Sort(arrProjectList);
int iProjectCount = arrProjectList.Length;
this.DropDownListSize = iProjectCount;
for (int i = 0; i < iProjectCount; i++)
{
SelectJobDropdown.Items.Add(arrProjectList[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我需要添加一个文件管理器以仅显示包含键入其ComboBox自身文本的项目,而不管下拉列表本身是否打开。
我已经禁用了这两个功能,AutoCompleteMode并且AutoCompleteSource由于打开的下拉列表无法正常工作。它是在现有列表的顶部打开附加列表,但是我只能从其下面的下拉列表中进行选择。请参阅下面的打印屏幕:

顶部的列表处于非活动状态,我无法选择文本,但是也没有显示子字符串的选项。
盒子本身只有一个
private void SelectJobDropdown_SelectedIndexChanged(object sender, EventArgs e)
{
//Plenty of code here
}
Run Code Online (Sandbox Code Playgroud)
当我在框内输入内容时,有人可以指出正确的方向如何过滤列表。
请注意,我仅使用C#三个星期,因此可能会与该语言的某些术语或其他方面混淆。
我有以下代码,用于测试C#上的Task框架
static void Main(string[] args)
{
Task<string> task1 = AsyncA();
task1.ContinueWith(Print);
Task<string> task2 = AsyncB();
task2.ContinueWith(Print);
Task.WaitAll(new Task[] {task1, task2});
}
static async Task<string> AsyncA()
{
Thread.Sleep(1500);
return "A";
}
static async Task<string> AsyncB()
{
Thread.Sleep(430);
return "B";
}
static async void Print(Task<string> str)
{
Console.WriteLine(str.Result);
}
Run Code Online (Sandbox Code Playgroud)
我的输出如下:
一个
B.
我究竟做错了什么?提前致谢.
我正在我班上做作业,问题是:创建一个方法,调用它A,然后让它返回字符串中的第一个字母.
这是我的代码:
{
String f = "flying";
String str = A(f);
Console.WriteLine(str);
Console.ReadKey();
}
public static string A(string s)
{
string first = s.Substring(0, 1);
return first;
}
Run Code Online (Sandbox Code Playgroud)
但这是不可接受的,因为我正在回归string而不是回归char.我无法在任何地方找到如何在飞行中返回字母"f" char,我会感激一些帮助.
我有以下用于检查数据库值fe的函数:
public static class ConnectionConst
{
public const int NotConnected = 0;
public const int Connected = 1;
public const int Unknown = 2;
// ...
}
Run Code Online (Sandbox Code Playgroud)
现在我不想在datagrid中显示整数值,而是显示const属性的值.Fe'已连接'而不是'1'.
public Form1()
{
InitializeComponent();
this.imgRoom.Click += new EventHandler(this.pictureBox1_Click);
}
private void pictureBox1_Click(object sender, EventArgs e)
{
var label1 = new LabelControl();
label1.Location = MousePosition;
label1.BackColor = Color.Red;
label1.Parent = imgRoom;
label1.Text = "Point";
imgRoom.Controls.Add(label1);
}
Run Code Online (Sandbox Code Playgroud)
当我点击你可以在附加屏幕上看到的位置时,点出现在另一个地方.如何解决这个问题呢?
我有一个函数将double值转换为sbyte并返回其十六进制表示:
string convertToSByte(double num, double factor)
{
double _Value = num * factor;
if (_Value > 127)
{
_Value = 127;
}
else if (_Value < -127)
{
_Value = -127;
}
return Convert.ToSByte(_Value).ToString("X2");
}
Run Code Online (Sandbox Code Playgroud)
计算出的_Value值应该在[-127 ; 127]if 的范围内,那么这些值必须设置为默认值.
问题:如何简化这两个条件和默认值的设置?
编辑:
我尝试使用条件运算符,?但实际上它并不简单(甚至更难读),也没有更少的代码
PS.这个问题更多的是教育目的.找到一种不同的方法来检查变量的范围
我有一类这种结构:
public class ClassName<T>
{
public int Id {get;set;}
public List<T> ObjectList { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
一个集合ClassName位于ParentClass
public ParentClass
{
public List<ClassName<T>> Property {Get;set;}
//Other properties...
}
Run Code Online (Sandbox Code Playgroud)
要创建此类的实例,我将执行以下操作:
var item = new ClassName<string>();
Run Code Online (Sandbox Code Playgroud)
是否可以在不指定类型的情况下创建此类的实例T?
我试过了
var item = new ClassName<null>();
var item = new ClassName();
Run Code Online (Sandbox Code Playgroud)
但显然这些不起作用....
我有一个方法,我想重新创建更通用的方法。
public Task<bool> DoSomething<T>(T t) where T : Type, IAnyInterface
Run Code Online (Sandbox Code Playgroud)
如您所见,我想要一个Type作为参数,它必须实现IAnyInterface
但如果我调用该方法,
DoSomething(typeof(ObjectThatImplementsIAnyInterface));
Run Code Online (Sandbox Code Playgroud)
我收到错误:
类型“System.Type”不能用作泛型类型或方法“DoSomething(...)”中的类型参数“T” 没有从“System.Type”到“IAnyInterface”的隐式转换
那么我怎样才能让该方法接受该类型呢?