小编jsz*_*eti的帖子

命名管道挂起写

我正在制作一个多客户端命名管道服务器.客户端正确连接到服务器,但每当我尝试通过服务器或客户端写入管道时,代码都会挂起写入方法.是什么原因导致写入方法被卡住了?

服务器代码:

public void ListenForConnections()
{
    Thread startListening = new Thread(AcceptConnections);
    startListening.Start(PipeName);
}

public static void AcceptConnections(object ServerPipeName)
{
    while (true)
    {
        try
        {
            NamedPipeServerStream pipeServer = 
                 new NamedPipeServerStream(ServerPipeName.ToString(),
                                           PipeDirection.InOut, 
                                           NamedPipeServerStream.MaxAllowedServerInstances,
                                           PipeTransmissionMode.Message);

            pipeServer.WaitForConnection();
            pipeServer.ReadMode = PipeTransmissionMode.Message;
            //A client has connected
            Pipes.Add(pipeServer);
            index++;

            Thread Poll = new Thread(PollPipe);
            Poll.Start(pipeServer);
            }
            catch (Exception ex)
            {
                return;
            }
        }
    }

    public static void PollPipe(Object Pipe)
    {
        byte[] bytes = new byte[1024];
        NamedPipeServerStream PipeStream = (NamedPipeServerStream)Pipe;
        MemoryStream ms = new MemoryStream();
        do 
        {
            ms.Write(bytes, …
Run Code Online (Sandbox Code Playgroud)

c# pipe named-pipes

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

C#公共接口vs接口

公共接口声明和接口之间有什么区别吗?(我认为默认情况下接口是公共的).

我问,因为VS2012抱怨访问级别.

我宣布:

interface Ixyz
{nothing important here}
Run Code Online (Sandbox Code Playgroud)

和属性(在另一个使用Ixhz作为其类型的类中):

public Ixhz Somename
{nothing important here}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译项目时,它会对访问级别抱怨,但是当我声明接口时public interface Ixyz就会停止这样做.添加公共接口有什么后果吗?

c# interface public

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

如何终止线程

我知道有很多问题.我尝试过但却得不到任何结果.

我在第一次运行时创建一个线程,当我尝试关闭表单时,表单关闭但应用程序(线程)仍在运行.

我不在乎线程会发生什么.我只想在用户点击X按钮时关闭整个应用程序.我试过thread_name.Abort();但没有任何反应.

c# multithreading

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

检查字符串是否包含字典键 - >删除键并添加值

我被困在一个检查字符串('14534000000875e')的函数中,如果它包含一个字母.如果它包含一个字母(az),请删除该字母并在末尾添加一个字符串.

为了实现这一点,我创建了一个Dictionary<char, string>Pairs,它已映射到09001,b到09002 [...]和z到09026

到目前为止这是我的代码:

public static string AlterSerial(string source)
{
    Dictionary<char, string> pairs = new Dictionary<char, string>();
    pairs.Add('a', "09001");
    ...

    int index = source.IndexOf(x);
    if (index != -1)
    {
        return source.Remove(index, 1);
    }
    return source;
}
Run Code Online (Sandbox Code Playgroud)

如何检查source字符串是否包含26个密钥中的一个,删除此密钥并将相应的字符串添加到源字符串的末尾?

注意:字母并不总是在源的末尾.

亲切的问候

c# linq generic-collections

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

如何使用linq按键搜索字典

我正在尝试使用linq在字典中搜索值

HiddenField hf_resID = (HiddenField)e.Item.FindControl("hf_resID");
int resID = Convert.ToInt32(hf_resID.Value);
////get GroupID
var value = from di in Resources_and_Groups
    where (di.Key.ToString() == resID.ToString())
    select di.Value;
//get controller from Repeater
DropDownList ddlGroup = (DropDownList)e.Item.FindControl("ddlGroup");

//ddlGroup.SelectedValue = value.ToString();
Run Code Online (Sandbox Code Playgroud)

c# linq asp.net

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

GetTempFileName()并保存到AppData文件夹

在下面的代码中,文件保存在项目的debug文件夹中,我想将文件存储在通用指定文件夹下的appdata文件夹中!

AViewModel vm = DataContext as AViewModel;
var table = vm.FileSelectedItem;

if (table != null)
{
    var filename = System.IO.Path.GetTempFileName();
    File.WriteAllBytes(table.FileTitle, table.Data);
    Process prc = new Process();
    prc.StartInfo.FileName = table.FileTitle;
    prc.Start();
}  

//table.FileTitle is the name of the file stored in the db 
//    eg:(test1.docx, test2.pdf, test3.txt, test4.xlsx)
//table.Data is public byte[] Data { get; set; } property
//    which stores the files coming from the db.
Run Code Online (Sandbox Code Playgroud)

我正在看GetFolderPath并尝试这样的事情

System.IO.Path.GetTempFileName(
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
Run Code Online (Sandbox Code Playgroud)

谢谢你的回复!

c# wpf

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

C#索引超出了数组的范围[combobox]

下面是我开始调试时导致错误的代码.当我从combobox1或combobox2选择选项时,我收到一条消息:Index超出了数组的范围.我该怎么解决这个问题?

谢谢你的时间阅读.:)

public Form1()
{
    InitializeComponent();
    String[] arr1 = new String[2];

    arr1[0] = "SG";
    arr1[1] = "MY";

    comboBox1.Items.AddRange(arr1);
    comboBox2.Items.AddRange(arr1);        
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    double[,] arr = new double[2, 2];

    for(int i = 0; i <2; i++)
    {            
        arr[0, 0] = 1;
        arr[0, 1] = 1.24;
        arr[1, 0] = 0.80;
        arr[1, 1] = 1;
        label1.Text = 
            arr[comboBox1.SelectedIndex,
                comboBox2.SelectedIndex].ToString();//Index was outside the bounds of the array.         
    }
}

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{           
    double[,] arr = …
Run Code Online (Sandbox Code Playgroud)

c# arrays combobox

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