クラスと組み合わせたスレッドに問題があります。私の質問のために、私の問題を説明する簡単なコードを作成しました。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace RemoteMonitorServer
{
public partial class RemoteMonitor : Form
{
Thread cThread;
Server cServer;
public RemoteMonitor()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
WriteLog("Programm started!");
cServer = new Server();
cThread = new Thread(new ThreadStart(cServer.StartServer));
cThread.IsBackground = true;
cThread.Start();
}
public void WriteLog(string sText)
{
MessageBox.Show(sText);
listBox1.Items.Add(sText);
}
}
}
クラスには次のコードがあります。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RemoteMonitorServer
{
class Server
{
public void StartServer()
{
RemoteMonitor cTest = new RemoteMonitor();
cTest.WriteLog("Thread started!");
}
}
}
さて、このコードを開始すると、リストボックスに「Programm started!」と表示されます。「スレッドが開始されました!」ではありません。2 つの MessageBox メッセージが表示されますが、メソッドが呼び出されていると思います。Visual Studio 2010 にはエラーはありません。誰かがここで間違っていることを教えてもらえますか?