好了,我已经创建了一个程序,它可以进行多次复制并将文本添加到文件中,运行时我得到的是一个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(“text here ....”)
在武器中被调用但是当它执行时它只是给出一个空白消息框但是当我执行weapon_gsc它完全正常时
我使用相同的调用方法和showmessage() 武器中的功能
但是在声音类和雷霆类中,它不会更新它或在执行后显示
所以我不知道我会怎么做。
因为它对于下面显示结果的weapon_gsc图像完全正常
image of it working for weapons_gsc and weapons
你最后看到的声音我必须做出一个propeties.setting.default.results
并使它成为声音alais唯一的方法我可以让它在一个消息框中显示结果。
答案 0 :(得分:0)
在进一步讨论之前,我想建议你采取略微不同的方法;即,我没有理由不简单地使用像log4net这样的日志框架,并添加custom appender,它会将消息重定向到文本框,以及文本日志文件。当您的应用崩溃时,拥有日志对于故障排除非常宝贵。
同步对记录器类的访问
然而,如果您需要从多个线程编写这些日志消息,则需要synchronize access到function recordHistory() {
dow=new Date();
var dow=dow.getDay();
if(dow==0 || dow==6){
return;
}
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Account");
var source = sheet.getRange("B7");
var values = source.getValue();
var historicalData = ss.getSheetByName("EOD Data");
var lr=historicalData.getLastRow()+1
var range = historicalData.getRange(lr , 1, 1, 1);//last row of col A
range.setValue(new Date().toString());
var range = historicalData.getRange(lr, 2, 1, 1);//last row of col B
range.setValue(values);
};
方法。您shouldn't expose the list as a public property,而是创建将要同步的方法:
Add
有了这样的东西,你需要确保不要在每次访问时创建这个类的新实例。这意味着您的班级应该只创建一次“记录器”:
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;
}
}
}
让班级成为单身人士
或者,您可能希望将记录器类设为singleton(在大多数情况下不是最好的想法,但如果这是一次性小项目,则可能会简化操作):
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());
}
}
在这种情况下,您根本不需要实例化logger类,并且您的所有类只能使用多个线程访问单个实例:
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;
}
}
}