小编Mar*_*zek的帖子

在C#中同步线程

我有一个基于表单的应用程序.UI由线程处理.从UI线程,我创建3个线程来执行一些处理.

Thread t1 = new Thread(() => performprocessing());
Thread t2 = new Thread(() => performprocessing());
Thread t3 = new Thread(() => performprocessing());
Run Code Online (Sandbox Code Playgroud)

现在,我想在所有3个线程完成执行后在UI线程中执行一个语句.所以,我做了以下事情:

do 
{

} while (t1.IsAlive || t2.IsAlive || t3.IsAlive);
/*--execute statement---*/
Run Code Online (Sandbox Code Playgroud)

但我希望UI能够保持响应.在这种情况下,UI线程停留在循环中,直到所有三个线程完成执行.我该怎么做?

我读到了手动和自动重置事件,但我不知道在这种情况下如何使用它们.请指导我.

.net c# multithreading

1
推荐指数
1
解决办法
153
查看次数

可以消除这个子查询?

我有一个说明字段的文件表

DocumentID int - PK and autoincrement 
LastStatusChangedDateTime - datetime 
JurisdictionID - int 
DocumentStatusID - int
Run Code Online (Sandbox Code Playgroud)

现在我想引入一个名为的计算列CanChangeStatus.这就像一个队列.因此,如果文档是第一个,则可以更改其状态.这是我的查询:

Select d.DocumentID,  
(
    Select Cast(Case When d.DocumentID = v.DocumentID Then 1 Else 0 End as bit)
    From
    (
        Select Top 1 DocumentID
        From Documents
        Where JurisdictionID = d.JurisdictionID
            And
        DocumentStatusID = d.DocumentStatusID
        Order By LastStatusChangedDateTime 
    ) v
) as CanChangeStatus    
From Documents d
Run Code Online (Sandbox Code Playgroud)

指数是那里JurisdictionIDDocumentStatusID.罪魁祸首是子查询 - 我试图计算的方式CanChangeStatus.有什么方法可以加快速度或将此子查询转换为连接,因为连接大多比子查询快?

sql t-sql sql-server sql-server-2012

0
推荐指数
1
解决办法
56
查看次数

如何从字符串中读取?

我正在尝试学习如何根据用户选择的时间来安排图片.

这是带有问题的代码:

private void startjob()
{
     string theDate = DateTimePicker1.Value.ToString();
     DateTime dt = Convert.ToDateTime(date);
     {
         DateTime start = new DateTime(2009, 12, 9, 10, 0, 0); //How Do I make this to read the string that is converted from DateTimePicker instead of this?
         DateTime end = new DateTime(2009, 12, 10, 12, 0, 0); //How Do I make this to read the string that is converted from DateTimePicker instead of this?
         DateTime now = DateTime.Now;
         if ((now > start) && (now < …
Run Code Online (Sandbox Code Playgroud)

c# methods datetime function datetimepicker

0
推荐指数
1
解决办法
92
查看次数

检查调用范围或函数内部的条件

我有以下3个例子,它们做同样的事情

//case1 do it if the condition is valid
private void SetMultiplePropertyValues()
{
    if (Keyboard.GetKeyStates(Key.CapsLock) == KeyStates.Toggled)
    {
        //do somthing
    }
}

//case 2 return if the condition is not valid
private void SetMultiplePropertyValues()
{
    if (Keyboard.GetKeyStates(Key.CapsLock) != KeyStates.Toggled) return;
        //do somthing
}

//case 3 checking the condition in the calling scope
if (Keyboard.GetKeyStates(Key.CapsLock)== KeyStates.Toggled)
    SetMultiplePropertyValues())

private void SetMultiplePropertyValues()
{
    //do somthing
}
Run Code Online (Sandbox Code Playgroud)

你会选择哪一个?为什么?

c#

0
推荐指数
1
解决办法
259
查看次数

C#字符串不包含文本

我有一个短语输入到类似于此的文本框中:"明天在taree中的天气是什么"

目前,此代码将郊区字符串设置为大写名称,并将suburbID设置为值.

public void requestedSuburb(String suburbFromForm1)
{
    suburb = suburbFromForm1;

    if (suburb.Contains("taree"))
    {
       suburb = "Taree";
       suburbID = "zmw:00000.1.95784.xml";
    }
    else if (suburb.Contains("port macquarie"))
    {
       suburb = "Port Macquarie";
       suburbID = "zmw:00000.1.94787.xml";
    }
Run Code Online (Sandbox Code Playgroud)

(如果低于此点,使用else语句检查更多的郊区名称.

将整个InputTextbox.Text传递到郊区字符串以检查上述内容.

我的问题是,是否有一个if语句可以检查郊区字符串,如果没有找到郊区的文本,它可以默认选择?(即:我希望'Taree'成为默认设置.)

我现在拥有的是这个,但它没有返回任何东西:

else if (!suburb.Contains("taree") || (!suburb.Contains("port macquarie")))
{
   suburb = "Port Macquarie"
   suburbID = "zmw:00000.1.94787.xml";
}
Run Code Online (Sandbox Code Playgroud)

c# string contains

0
推荐指数
1
解决办法
2万
查看次数

如何使用LINQ以降序获取数据

我有以下LINQ查询,我希望数据按totalPopulation降序排列.

var query = from u in xdoc.Root.Elements(ns + "DetailedUtility")
            select new
            {
                UtilityId = (int)u.Attribute("UtilityId"),
                TotalPopulation = u.Elements(ns + "City")
                                   .Sum(c => (int)c.Attribute("Population"))
            };
Run Code Online (Sandbox Code Playgroud)

如何按降序获取列表.请帮忙

c# linq

0
推荐指数
1
解决办法
1606
查看次数

如何在没有For Each Loop的情况下更新C#中的DataTable?

我正在尝试为我的数据表中的每一行运行循环但它给我错误,因为我无法修改每个循环的集合...我还能做些什么来更新表?

我正在尝试删除作为设备名称的单个列的重复条目,如果根据重复的设备名称进行此类选择,则只需更新旧数量.

这是代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        dt = new DataTable();
        dt.Columns.Add("Appliance Name", typeof(string));
        dt.Columns.Add("Quantity", typeof(int));
        dt.Columns.Add("Day Time(Hrs)", typeof(int));
        dt.Columns.Add("Backup Time(Hrs)", typeof(int));
        // dt.Rows.Add("Sana", 5, 4, 3);
        Session["MyDataTable"] = dt;
    }
}

protected void BtnAddNext_Click(object sender, EventArgs e)
{
    DataTable dtab = (DataTable)Session["MyDataTable"];

    if (dtab.Rows.Count != 0)
    {
        foreach (DataRow r in dtab.Rows)
        {
            if (Convert.ToString(r["Appliance Name"]) == DDLAppName.Text)
            {
                int temp = Convert.ToInt32(r["Quantity"]);
                r["Quantity"] = Convert.ToInt32(QtyTB.Text) + temp;
            } …
Run Code Online (Sandbox Code Playgroud)

c# asp.net datatable gridview datarow

0
推荐指数
1
解决办法
7123
查看次数

运行时错误尝试反序列化空流

我正在尝试deserialize从文件中的字符串列表.在这里我的代码

FileStream filestream = new FileStream(@"D:\cache.bin", FileMode.OpenOrCreate);
try
{
    BinaryFormatter binformat = new BinaryFormatter();
    _cacheFileList = (List<string>)binformat.Deserialize(filestream);
}
catch (SerializationException ex)
{
    Console.WriteLine(ex.Message);
}
finally 
{
    filestream.Close();
}
Run Code Online (Sandbox Code Playgroud)

我收到运行时错误.尝试反序列化空流.

.net c# filestream deserialization

0
推荐指数
1
解决办法
5766
查看次数

在循环完成之前运行代码,线程池c#

我试图首先在线程内执行代码,并等待for循环完成后再执行for循环后的代码.

for (int i = 254; i > 1; i--)
{

    //some code here...

    WaitCallback func = delegate (object state)
    {
        //do something here.... - i want this to finish with the loop first
    };

    ThreadPool.QueueUserWorkItem(func);

}

// this code is executed once the for loop has finished
// however i want it to be done 
// after the thread has finished executing its code and the for loop.
Run Code Online (Sandbox Code Playgroud)

.net c# multithreading

0
推荐指数
1
解决办法
76
查看次数

C# Linq 中的 Java noneMatch() 等效

使用 Linq 的 C# 中的 Java 示例的等效项是什么?

int[] someArray(int[] arr1, int[] arr2)
{
    return Arrays.stream(arr1).filter(a->Arrays.stream(arr2).noneMatch(i->i == a)).toArray();
}
Run Code Online (Sandbox Code Playgroud)

c# linq

0
推荐指数
1
解决办法
345
查看次数

为什么我的List.Contains不起作用?

出于某种原因,我的代码不起作用.我已确认文件夹中有.idx和.log文件.有人可以帮忙吗?

谢谢.

List<string> subDirList = new List<string>(Directory.GetFiles(srcPath, "*"));

if (subDirList.Contains(".idx") && subDirList.Contains(".log"))
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

c# string contains list

-1
推荐指数
1
解决办法
461
查看次数

C#变量问题

遇到一些c#变量的问题.无法弄清楚我做错了什么.可能是一些简单的东西,但我已经有点了.

public class DataFileLoad
{
    //Public jagged array variables

    public string[][] aConfJag = null;
    public string[][] aPartJag = null;
    public string[][] aTypeJag = null;
    public string[][] aWkshpJag = null;
    public string[][] aWorkJag = null;

    public void loadFiles()
    {
        //The path of our exectuable
        string exePath = Application.StartupPath;

        //Set our working directory to our exe path for file reading
        Directory.SetCurrentDirectory(exePath);

        string[] aConferences = File.ReadAllLines(@"DATA\CONFERENCES.txt");
        string[] aParticipants = File.ReadAllLines(@"DATA\PARTICIPANTS.txt");
        string[] aType = File.ReadAllLines(@"DATA\TYPE.txt");
        string[] aWkshpReg = File.ReadAllLines(@"DATA\WKSHP_REGISTRATIONS.txt");
        string[] aWorkshops = File.ReadAllLines(@"DATA\WORKSHOPS.txt");

        //Make …
Run Code Online (Sandbox Code Playgroud)

c#

-1
推荐指数
1
解决办法
111
查看次数

VB到C#可选参数

我一直在寻找解决方案,但还没有得到一个坚实的答案.我有VB代码,我需要转换为c#.这是VB代码.我尝试了不同的解决方案,但似乎都没有.这是函数的定义

Protected Sub LoadData(ByVal ProcedureName As String, ByVal NumOuts As Long, ByRef Label1 As Label, Optional ByRef Label2 As Label = Nothing, Optional ByRef Label3 As Label = Nothing)

    Dim ConnStr As String

    ConnStr = "Data Source=H50;Initial Catalog=" + Me.DatabaseName.Text + ";Integrated Security=True"
    Dim conn As New SqlConnection(ConnStr).....
Run Code Online (Sandbox Code Playgroud)

这是要求这一点的众多功能之一

Protected Sub LoadtblStaffContactsBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoadtblStaffContactsBtn.Click
    LoadData("Load_tblStaffContacts", 2, Me.tblStaffContacts_Label1, Me.tblStaffContacts_Label2)
    Me.LoadtblStudentBtn.Enabled = True
End Sub
Run Code Online (Sandbox Code Playgroud)

c# vb.net

-3
推荐指数
1
解决办法
364
查看次数