首页 新闻 赞助 找找看

做禁止winfrom多开,如何在已经打开的情况下,通过控制台传递参数改变正在Run的winform?

0
悬赏园豆:30 [已解决问题] 解决于 2012-10-26 12:16

如题,form用来显示图片,在program.cs文件中,已经做了唯一实例的处理,但不知道如何传参然后让form重新显示新的图片:
static class Program
  {
  public static System.Threading.Mutex MutexRun;
  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main(string[] cmd)
  {   
  bool noRun = false;
  MutexRun = new Mutex(true, "HumControl", out noRun);

  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
    
  Form1 frm1 = new Form1();
    
  if (noRun)
  {
  if (cmd.Length > 0)
  {
  frm1.strFile = cmd[0];
  }
  MutexRun.ReleaseMutex();   
  Application.Run(frm1);   
  }
  else
  {
    
  if (cmd.Length > 0)
  {
  frm1.strFile = cmd[0];
  }
  //todo 如何写,改变正在运行的frm1的picbox图片变成给定的路径的图片
  MessageBox.Show("已有实例正在运行!");
  }
  }  
  }


以下是form1.cs文件: public string strFile = @"F:\g.jpg";
  private void Form1_Load(object sender, EventArgs e)
  {   
  this.Location = new Point(0, 0);
  //this.Size = new System.Drawing.Size(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);
  mvPicBox.BackColor = Color.Black;
  this.mvPicBox.Image = Image.FromFile(strFile);
  }
求高手指点,谢谢

tli-terry的主页 tli-terry | 初学一级 | 园豆:150
提问于:2012-10-25 15:54
< >
分享
最佳答案
0

可以在启动程序的时候去File.Open一个文件不关闭,然后做个异常捕获,如果有占用就表示已经打开了。

同样在启动程序的时候,去修改一个标识文件,然后用FileWacher监控这个文件的修改,一旦发生修改就会被当前运行的程序监控到,表示又在启动一个实例,然后设置你那个图片。

收获园豆:20
向往-SONG | 老鸟四级 |园豆:4853 | 2012-10-25 16:24

可以给点代码参考么?再启动一个新的实例那之前那个怎么办?我只希望打开一个,谢谢了

tli-terry | 园豆:150 (初学一级) | 2012-10-25 16:30

@tli-terry: 

我说的实例是指执行Main函数,你不创建form就是了

向往-SONG | 园豆:4853 (老鸟四级) | 2012-10-25 16:37
其他回答(1)
0

使用Process 类,检查当前运行进程,发现有同名的,就触发替换图片的事件

收获园豆:10
luofer | 园豆:468 (菜鸟二级) | 2012-10-25 16:36

非常感谢,之前没有接触过线程,也不知道触发图片替换的事件怎么写,能不能给点代码提示啊,谢谢了

支持(0) 反对(0) tli-terry | 园豆:150 (初学一级) | 2012-10-25 16:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册