2016-02-17 11 views
-1

好吧,我已经创建了一个程序,它执行多个副本并将文本添加到文件中,其运行时间我所提出的是一个list_,用于存储大量密钥然后打印回来的过程结束,而不是有消息框加载这是我有创建一个全局列表并将其存储在其中的字符串形式多类

列表类。

public class Messageresult : Weaons 
{ 

    private List<string> elfenliedtopfan5 = new List<string>(); 
    public List<string> _Message 

    { 
     get { return elfenliedtopfan5; } 
     set { elfenliedtopfan5 = value; } 
    } 

} 

和班多我把这个像这样

public void ShowMessage() 
    { 
      elfy1 = new Messageresult(); 
      //MessageBox.Show(elfy1._Message.ToString()); 

      update(); 
      refreshlist(); 
      var message = string.Join(Environment.NewLine, elfy1._Message); 
      MessageBox.Show(message + Environment.NewLine + 
       createa + Environment.NewLine + results); 
      elfy1._Message.Clear(); } 

所以这是我在多个不同的类中使用,我使用继承 TIS类以上的称为武器。和我所有其他类继承武器,但

我有这样的问题,我有像我的雷枪类 某些类时,我称之为。

public void cliantfx() 
    { 
     elfy1 = new Messageresult(); 
     string path = modcliant; 
     if (!Directory.Exists(path)) 
     { 
      Directory.CreateDirectory(path); 
      File.Copy(Properties.Settings.Default.root + "//raw//clientscripts//" + ModName + ".csc", path + ModName + ".csc"); 
      MessageBox.Show(path); 
     } 


     if (File.Exists(path + ModName + ".csc")) 
     { 


      using (StreamReader elfenliedtopfan6 = new StreamReader((path + ModName + ".csc"))) 
      { 
       string elfenliedtopfan6_program = elfenliedtopfan6.ReadToEnd(); 
       if (elfenliedtopfan6_program.Contains(@"clientscripts\_thundergun::init();")) 
       { 
        //MessageBox.Show(@"clientscripts\_thundergun::init();" + "Already Added:"); 
        refreshlist(); 
        elfy1._Message.Add("clientscripts\\_thundergun::init();" + "Already Added:"); 

       } 

       if (!elfenliedtopfan6_program.Contains(@"clientscripts\_thundergun::init();")) 
       { 

        elfenliedtopfan6.Dispose(); 
        if (File.Exists(path + ModName + ".csc")) 
        { 
         { 
          string s = Environment.NewLine 
             + @" clientscripts\_thundergun::init();"; 

          string file = path + ModName + ".csc"; 
          List<string> lines = new List<string>(System.IO.File.ReadAllLines(file)); 
          int index = lines.FindLastIndex(item => item.Contains(@"clientscripts\_zombiemode_tesla::init();")); 
          if (index != -1) 
          { 
           lines.Insert(index + 1, s);//"" 
          } 
          System.IO.File.WriteAllLines(file, lines); 

          MessageBox.Show("Added: " + s + "To " + ModName); 


         } 

        } 

,正如你可以看到elfy1._message.Add(“文字在这里......”)

被称为武器,但是,当它执行寿这一点,只是给一个空白消息框,但是当我执行weapons_gsc它工作完全正常

我用的是相同的呼叫methord和showmessage()函数 武器

但在声音类thundergun类,它不会更新或一旦其执行

0显示

所以即时通讯不知道如何我会去这个。

,因为它的工作原理下面显示weapons_gsc图像完全正常的结果

image of it working for weapons_gsc and weapons

你在看到底的声音一个我不得不做出一个propeties.setting.default.results

并提出它=声音alais只有一种方式,我可以让它显示在一个消息框中的结果。

+1

每次编写新的Messageresult();时,都会创建该类的新实例。所以这不是一个“全局列表”,即使它的成员不是静态的,它只存在直到你创建一个新的实例。如果只有一个线程,只需在构造函数或字段初始值设定项中标记字段'readonly'并实例化一次。 – Groo

+0

在雷霆队我有大约7个线程,我只是选择那个作为例子,因为那个类是一个大类,所以不想在这里发布整个事情,我将如何工作只读(因为我的主要类是武器.cs并且被所有其他类继承。 –

+0

这个类根本没有同步,所以它对于多线程访问是不安全的。你确定这应该是一个*字段*吗?因为你要么:1)想要一个单一的实例,它可以被多个线程同时使用(在这种情况下,您需要一个**单线程安全实例**或2)希望每个方法/线程都有一个单独的**局部变量**,该变量将在该范围的末尾转储。现在,您将'elfy1'字段设置为指向一个新实例(在某个方法的开始处),然后对其列表进行变异(从其他方法),最后从某处读取其内容。 – Groo

回答

0

在进一步讨论之前,我想建议您采取一种稍微不同的方法;即我没有看到为什么您不会简单地使用日志框架(如log4net),并添加一个custom appender,这会将消息重定向到文本框以及文本日志文件。当您的应用崩溃时,拥有日志对于故障排除非常重要。

同步存取记录器类

不过,如果你需要写从多个线程这些日志信息,那么你需要synchronize accessAdd方法。你shouldn't expose the list as a public property,而是创造出将同步方法:

public class Logger 
{ 
    private readonly object _lock = new object(); 
    private readonly List<string> _messages = new List<string>(); 

    public void Append(string message) 
    { 
     // locking ensures that only a single thread can enter this block 
     lock (_lock) 
     { 
      _messages.Add(message); 
     } 
    } 

    // since we are locking this method too, we can be sure that 
    // we will join messages into a single string and clear the list 
    // without being interrupted (atomically) 
    public string Merge() 
    { 
     lock (_lock) 
     { 
      // now you are sure you won't lose any data, because 
      // it's impossible for other threads to append to _messages 
      // between joining and clearing it 

      var result = string.Join(Environment.NewLine, _messages); 
      _messages.Clear(); 
      return result; 
     } 
    } 
} 

已具备了这样的事情,你需要确保你没有在每个接入创建这个类的一个新实例。这意味着,你的类应该创建“记录仪”只有一次:

public class SomeOtherClass 
{ 
    // once you add the 'readonly' keyword, compiler   
    // won't let you accidentally create another instance 
    private readonly Logger _logger = new Logger(); 

    public void SomeMethod() 
    { 
     // _logger = new Logger(); // <-- this will not compile 

     _logger.Append("Some message"); 
    } 

    public void SomeOtherMethod() 
    { 
     MessageBox.Show(_logger.Merge()); 
    } 
} 

制作类单身

或者,您可能希望使记录器类singleton(在大多数情况下不是最好的办法,但如果这是一次小的项目,它可能会简化事情):

public class Logger 
{  
#region Singleton pattern 

    // this is the static one-and-only logger instance, alive 
    // throughout the lifetime of your application 
    private static readonly Logger _instance = new Logger(); 
    public static Logger Instance 
    { 
     get { return _instance; } 
    } 

    // private empty constructor is here to ensure 
    // that you can only access this class through the 
    // Logger.Instance static property 
    private Logger() { } 

#endregion 

    private readonly object _lock = new object(); 
    private readonly List<string> _messages = new List<string>(); 
    public void Append(string message) 
    { 
     lock (_lock) 
      _messages.Add(message); 
    } 

    public string Merge() 
    { 
     lock (_lock) 
     { 
      var result = string.Join(Environment.NewLine, _messages); 
      _messages.Clear(); 
      return result; 
     } 
    } 
} 

在这种情况下,你就不需要在所有实例的记录器类,和所有的类都只能交流使用多线程的单个实例:

public class SomeOtherClass 
{ 
    public void SomeMethod() 
    { 
     Logger.Instance.Append("Some message"); 
    } 

    public void SomeOtherMethod() 
    { 
     MessageBox.Show(Logger.Instance.Merge()); 
    } 
} 
+1

非常感谢你真的很感谢你它是一个真正的作品,你的生活救星:) [工作的责任](https:// winpic.co/46M76a2f16ef8.png) –

相关问题