小编Maa*_*rio的帖子

线程必须是STA-Thread,但它已经是

我正在研究关于C#WPF程序的论文,但我遇到了一个我不明白的错误.

在我的MainWindow代码的某处,我开始一个新的线程,如下所示:

Thread searchServer = new Thread(new ThreadStart(doSearchServer));
            searchServer.SetApartmentState(ApartmentState.STA);
            searchServer.Start();
Run Code Online (Sandbox Code Playgroud)

doSearchServer方法执行以下操作:

    private void doSearchServer()
    {
        bool connected = ServerConnection.authentication();
        ServerConnection.getDeviceList();
        gotDataFromServer = connected;

        if (connected)
          ..
          ..
    }
Run Code Online (Sandbox Code Playgroud)

ServerConnection类是静态的,因为我在其他Windows中也需要该类.

在ServerConnection.authentication()中,客户端(我的程序)尝试在我的服务器上进行身份验证.如果需要密码,我想打开一个新的PasswordWindow,如下所示:

public static bool authentication()
    {
        UdpClient client = new UdpClient();
        IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, 55042);
        IPEndPoint ipRec = new IPEndPoint(IPAddress.Any, 0);

        byte[] bytes = Encoding.UTF8.GetBytes("authent|Username|Windows");
        client.Send(bytes, bytes.Length, ip);

        //Receive Answer
        byte[] recBuffer = client.Receive(ref ipRec);
        string recString = Encoding.UTF8.GetString(recBuffer);

        if (recString.Equals("authent|password"))
        {
            //Send Passwort
            Console.WriteLine("Password Required");

            Dispatcher.CurrentDispatcher.Invoke(new Action(() => …
Run Code Online (Sandbox Code Playgroud)

c# wpf multithreading dispatcher sta

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

命名空间bla已经包含(我的部分)类的定义

我用一些非常大的.cs文件创建了一个程序.所以我试图通过使用部分类来拆分其中一个.所以我创建了第二个文件,其中包含相同的类名和相同的命名空间.我从一个文件中剪切了一些函数并将其粘贴到第二个文件中.但是,当我想运行该程序时,它说该类已经存在.但它的部分类必须具有相同的名称:S

在第一个文件(ChartWidget.cs),它以:

namespace UGS.Sidebar.ChartWidget
{
    public partial class ChartWidget : UserControl
    {
        #region declarations
        private int id = -1;
Run Code Online (Sandbox Code Playgroud)

.....

在第二个文件(Debugging.cs),它以:

namespace UGS.Sidebar.ChartWidget
{
    public partial class ChartWidget : UserControl
    {

        #region debugvars
        Random _r = new Random();
        #endregion
        .....
Run Code Online (Sandbox Code Playgroud)

这些文件来自Usercontrol(如你所见),但我不认为这是问题所在?

希望你们能告诉我为什么它不起作用:(抱歉我的英语不好我真的很糟糕... :)

c# class partial

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

标签 统计

c# ×2

class ×1

dispatcher ×1

multithreading ×1

partial ×1

sta ×1

wpf ×1