登陆过度窗体问题

damnhe 2008-07-29 02:24:44

程序运行Main,首先弹出一窗体,然后运行其他代码,在其他代码运行完之后,关闭窗体,
可代码还没运行完,窗体就消失了(太快,闪的那一刻要仔细看才能看见),怎么实现这效果?



using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;

namespace FormThread
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 form1 = new Form1();
Thread tempThread = new Thread(new ThreadStart(form1.Show));
tempThread.Start();
while (true)
{
// other code
}
}
}
}
...全文
266 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
damnhe 2008-07-30
  • 打赏
  • 举报
回复
果然厉害,以后还请多指教
waterspace 2008-07-30
  • 打赏
  • 举报
回复
我说了例子有点简单,只是要告诉你耗时的代码应该放在线程中做。
那个代码在VS2003是可以正常工作的
仍然是很简单的代码


[STAThread]
static void Main()
{
Form2 f = new Form2();
fHandle = f.Handle;

System.Threading.Thread tempThread = new System.Threading.Thread(new System.Threading.ThreadStart( aaa));
tempThread.Start();

f.ShowDialog();
Application.Run(new Form1());
}
static IntPtr fHandle;

[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, CallingConvention = System.Runtime.InteropServices.CallingConvention.Winapi, SetLastError = true)]
private static extern bool PostMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

private static void aaa()
{
System.Threading.Thread.Sleep(6000);
PostMessage(fHandle, 0x0010, IntPtr.Zero, IntPtr.Zero);
}

damnhe 2008-07-30
  • 打赏
  • 举报
回复
继续顶上去
damnhe 2008-07-29
  • 打赏
  • 举报
回复
麻烦大家用代码说话,真的复杂吗
mm6268 2008-07-29
  • 打赏
  • 举报
回复
启动一个timer,设定6秒,程序运行完毕启动timer,timer结束关闭窗口
w4585 2008-07-29
  • 打赏
  • 举报
回复
我想进来说 我是PPG的fans~
damnhe 2008-07-29
  • 打赏
  • 举报
回复
有些东西不是想的那么简单,试了就出问题
damnhe 2008-07-29
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 waterspace 的回复:]
引用 17 楼 Cherishny 的回复:
类似与淡出的效果吧!

他只是想做欢迎窗口

写点代码你就明白jinjazz的意思了
有点简单,将就着看吧


C# code

[STAThread]
static void Main()
{
System.Threading.Thread tempThread = new System.Threading.Thread(new System.Threading.ThreadStart(aaa));
tempThread.Start();
f = new Form2();
f…
[/Quote]

这代码问题多多
未处理 System.InvalidOperationException
Message="线程间操作无效: 从不是创建控件“Form2”的线程访问它。"
Source="System.Windows.Forms"
StackTrace:
在 System.Windows.Forms.Control.get_Handle()
在 System.Windows.Forms.Control.SendMessage(Int32 msg, Int32 wparam, Int32 lparam)
在 System.Windows.Forms.Form.Close()
在 FormThread.Program.aaa() 位置 D:\WinForm\FormThread\FormThread\Program.cs:行号 30
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
waterspace 2008-07-29
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 Cherishny 的回复:]
类似与淡出的效果吧!
[/Quote]
他只是想做欢迎窗口

写点代码你就明白jinjazz的意思了
有点简单,将就着看吧



[STAThread]
static void Main()
{
System.Threading.Thread tempThread = new System.Threading.Thread(new System.Threading.ThreadStart(aaa));
tempThread.Start();
f = new Form2();
f.ShowDialog();
Application.Run(new Form1());
}
static Form2 f;

private static void aaa()
{
System.Threading.Thread.Sleep(6000);
//把你那费时间的代码写在这里来替换System.Threading.Thread.Sleep(6000);
f.Close();
}



damnhe 2008-07-29
  • 打赏
  • 举报
回复
问题真的很难吗,CSDN太让我失望了
Cherishny 2008-07-29
  • 打赏
  • 举报
回复
类似与淡出的效果吧!
beijixing9 2008-07-29
  • 打赏
  • 举报
回复
看不懂,帮楼主顶.
damnhe 2008-07-29
  • 打赏
  • 举报
回复
顶,问题依然没有解决
waterspace 2008-07-29
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 damnhe 的回复:]
引用 1 楼 jinjazz 的回复:
你的思路好像搞反了,应该是show一个窗体,然后再子线程里面other code

直接show的话会卡死,如果用Application.Run的话就直接进去了,后面的代码根本不运行,如何是好?
[/Quote]

人家说的很明白,主线程show窗体,子线程写while (true)的代码
damnhe 2008-07-29
  • 打赏
  • 举报
回复
顶上去,问题mei解决怎么能沉了
damnhe 2008-07-29
  • 打赏
  • 举报
回复
顶上去,问题解决怎么能沉了
xqy_2007 2008-07-29
  • 打赏
  • 举报
回复
你可以试试在Main窗体的Pageload里面new一个你要显示的那个窗体,同时将主窗口隐藏,再在这个窗体里面用计时器来控制窗口在一定时间后关闭,
关闭这个窗口后再将主窗口显示出来就行了.
damnhe 2008-07-29
  • 打赏
  • 举报
回复
求高手测试 给个代码
damnhe 2008-07-29
  • 打赏
  • 举报
回复
那个帖子的方法我试了,主要使用this.Invoke()调用句柄函数,
如果窗体中打开过度窗体 ,没什么问题,此处是Program ,它没有invoke,而且我用过其他方式改写过,还是有问题
冷月孤峰 2008-07-29
  • 打赏
  • 举报
回复
http://blog.csdn.net/knight94/archive/2006/05/27/757351.aspx
加载更多回复(6)

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧