|
good day people
so i have a windows service and i want to call a windows form to show (modally) when i run the service, basically i call the method callform() on the onStart method
when i run the service the form starts to load right? so i get this dialog from windows saying it detects a dialog from interactive services or something and i choose to view the message and the screen turns blue and it shows my form, i want the service to show my form but not on that blue screen, but in windows
what should i do? i have no idea how to fix this
|
|
|
|
|
mirko86 wrote: what should i do
Do something else. Services should not interact with the desktop. If you really want to do something like this, you have to have another application that communicates with the service.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns
Help humanity, join the CodeProject grid computing team here
|
|
|
|
|
What happens if no user is logged on? Where will your UI be displayed then?
Having a service interact with the desktop is VERY bad. Look for an alternative solution.
|
|
|
|
|
I am in a class, and need assistance to convert the HH:MM to decimal when reading from a timecard table.
OneTreeUp
|
|
|
|
|
Use string.Split to get two numbers, then int.TryParse to convert to ints, then cast those to double when doing the division to get a decimal.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
or use TimeSpan.Parse("2:30").TotalHours or TimeSpan.Parse("2:30").TotalMinutes to get a real number representing the amount of time in the unit of your choice.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Cool, I did not know that ( never had a need )
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
how to install and configure lotus notes and domino server s/w..?
how to send mails through lotus notes...?
plss reply....
pls send the snapshots of those if found...
|
|
|
|
|
WTF. Lotus Notes doesn't even contain the letter C or the # symbol, so how did you stumble on this forum or figure it as an appropriate place to post.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys
|
|
|
|
|
If you found this forum b/c someone else mentioned notes before, sorry about that. They were trying to ask a programming question tho. This is not a forum for support for software users. It's a forum for people who write code.
Notes really, really sucks. If you're not installed it, I'd recommend you don't.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Hi!
I am (still) working on the project I asked for something in this message:
http://www.codeproject.com/script/Forums/View.aspx?fid=1649&msg=2956783
I am trying to follow Luc Pattyn's advice on make some synchronous threading, one to scan the serialport in a loop(scanThread), and one to write what the first thread "found" to the textbox(writeThread). The threadcalls in them self seem to function(on debugging), but when the writeThread tries to invoke the textbox something seems to go wrong, and the UI hangs. I have googled the question, and found that it's a frequent problem, but everyone answers the the .Invoke() or .BeginInvoke() is the answer. However, here it isn't.
The lock I have implemented in the attached code was just because I tried to synchronize the threads, it doesn't work any better without(or with ). But I would like a little opinion if I have written it correct, seems strange to me that the "this" will refer to "line".
However, it's the UI "hanging" which is my main problem right now. I really can't see what's wrong..
Thanks!
Jon
Norway
The Code:
public partial class Picaxecomm : Form
{
private Thread scanThread;
private Thread writeThread;
private String line;
private SerialPort serialPort;
private Boolean start;
private AutoResetEvent autoEvent;
public Picaxecomm()
{
InitializeComponent();
serialPort = new SerialPort();
initializePort();
autoEvent = new AutoResetEvent(false);
scanThread = new Thread(new ThreadStart(readPic));
writeThread = new Thread(new ThreadStart(writeDisp));
}
public void initializePort()
{
serialPort.PortName = "COM5";
serialPort.BaudRate = 4800;
serialPort.DataBits = 8;
serialPort.ReadTimeout = 100;
serialPort.Open();
serialPort.DtrEnable = true;
}
private void knapp_Click(object sender, EventArgs e)
{
if (start == false)
{
writeThread.Start();
scanThread.Start();
start = true;
}
}
public void readPic()
{
try
{
int i = serialPort.ReadByte();
lock (this)
{
line = Convert.ToString(i);
}
autoEvent.Set();
}
catch (TimeoutException)
{
}
Thread.Sleep(50);
readPic();
}
public void writeDisp()
{
autoEvent.WaitOne();
if (display.InvokeRequired)
{
display.Invoke(new MethodInvoker(delegate() { setDispText(); }));
}
else
{
setDispText();
}
}
public void setDispText()
{
display.Text = line;
serialPort.DiscardInBuffer();
writeDisp();
}
private void Picaxecomm_Load(object sender, EventArgs e){}
}
}
|
|
|
|
|
jondaeh wrote: if (display.InvokeRequired) { display.Invoke(new MethodInvoker(delegate() { setDispText(); })); } else { setDispText(); }
Try using BeginInvolke and returning
|
|
|
|
|
jondaeh wrote: if (display.InvokeRequired) { display.Invoke(new MethodInvoker(delegate() { setDispText(); })); } else { setDispText(); }
Try using BeginInvoke and returning
|
|
|
|
|
I have already tried BeginInvoke method, that didn't make any difference. Sorry, forgot to write that. What do you mean with "returning"?
Jon.
|
|
|
|
|
Welcome to the world of the dead(lock)
The method writeDisp calls setDispText and setDispText calls writeDisp. That looks very bad.
Do not abuse Invoke and use it for setting data in the control from a different thread without other code (the set display text also discards a serial port buffer).
Natza Mitzi
|
|
|
|
|
The reason why setDisispText calls writeDispText is to make the Thread go in a loop, and set the display whenever scanThread tells it so.
The reason I made a separate method for setDispText is that I found a code example on some other forum, which used exactly a separate method for the job. I thought this made sense, when it's no-go to access variables outside a delegate directly. This would be the case if I had wrote the invoke part like this
display.Invoke(new MethodInvoker(delegate() { display.Text = line; }));
However, I must say I'm on unknown ground here.. Please correct me
I will try to figure out a way to let the invoke only make the call to setDispText..just have to do a little thinking;)
Thanks for the answer!
Jon.
|
|
|
|
|
Hi,
sorry, this is wrong in many ways.
1.
you have unintended "tail recursion" in readPic; your code basically looks like:
public void readPic() {
...
readPic();
}
which means this method will NEVER terminate, since when it reaches the end, it calls itself, which calls itself, which... etc until you get a stack overflow.
2.
you have the same problem in writeDisp(): once the thread launches the first writeDisp, it will invoke setDispText() on the GUI thread, which calls writeDisp() again, which calls setDispText() again, etc, again climbing up the stack until it is exhausted (and halting all normal GUI action while doing it).
3.
the lock does not make any sense. Locks protect against two or more threads that otherwise could access the same code and data at the same time; you having only one lock statement, there is no one to keep out. Throw it away!
4.
Why do you perform serialPort.DiscardInBuffer(); ?? Every time you read one byte (which removes it from the buffer) you clear out the buffer; doing it in a separate statement (and here even on a separate thread) causes an undefined and variable delay so you may be throwing away the very next byte that comes in. Does not make sense to me.
5.
You are using two threads (and an AutoResetEvent to prevent the second one from running ahead of the first), however IMO you don't need two threads at all.
This is what I would try as a first attempt, details may be wrong, I haven't tested it:
public void readPic() {
while(running) {
try {
int i = serialPort.ReadByte();
string line = i.ToString();
display.Invoke(new MethodInvoker(delegate() { display.Text = line; }));
} catch (TimeoutException) {
}
}
}
So the second thread is gone, the tail recursion is gone; a loop is introduced to keep reading the serial port until a bool flag is cleared to cause the thread to exit.
The one thing that is not clear to me is what you want from the time-out: all you do is try again,
so setting the timeout to infinite would do exactly the same.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Hi!
Followed your advice, and it worked great right away!
However, it's still some details that is not completely clear to me. I know that it doesn't matter now that it is working, but I can't stand not knowing what went wrong(psycho.. ).
1.
I know that the loop I had written would never stop. However, what do you mean with stack overflow? As I see it, it will just go around and around, with a pause on 100ms on every cycle. Or does it make a new "process" on each cycle?
3.
I tried to have that lock I had written there on both places where line was accessed. This I meant as a safety to prevent line not to be written from the scanThread and read by writeThread at the same time.
I see why these locks didn't work, but how do I get a lock to understand what object it shall lock? Would I have to write it like this: lock(line){"do something with line"} ? Or must line be accessed from inside a method to make a lock?
4.
I used DiscardBuffer() to make sure that scanThread would get hold on only the newest byte. I was afraid of the buffer to fill up with several bytes while the writeThread did it's job. But I see now how accessing serialPort from another thread will make unnecessary thread "collision". Now I just implemented DiscardBuffer() before the while loop, and it seem to work fine.
Sorry being so curious, but I think these threading things is very interesting. Plus, apparently, we won't be learning it at school
Thanks for good help and good feedback! Appreciate it!
Jon.
|
|
|
|
|
Hi Jon,
1.
jondaeh wrote: I know that the loop I had written
your code did NOT have a loop at all, it had "recursion", i.e. a method calling itself, and an infinite recursion since there was no way to get out of it (compare with the typical example of calculating x factorial with recursion). Every method call takes a number of bytes of stack space; google and read up on "stack".
3.
jondaeh wrote: lock(line){"do something with line"}
one uses a lock to protect some data from being manipulated by two or more threads at the same time, when there is a danger of an inconsistent state (half of the data being written by one, then all of the inconsistent data being read by the others). The object on which the lock applies may or may not be the data to be protected, it all depends.
jondaeh wrote: Sorry being so curious
No problem. Being eager to learn can't be bad. Buy some books and study them, that is the best way to learn languages and technologies. And don't forget to read some CodeProject articles, choose from those with a high score!
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
I am getting this error while sending message though VS08 C# code....
replicator error,
no route is known from this host to the destination address
What possible solution...?
|
|
|
|
|
Replace bloated goatse with a real email client
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
Please........don't make me feel shameful. I use it.
जय हिंद
|
|
|
|
|
as long as you didn't pick it for your company on the basis of glossy's in PHB Times...
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
I did not pick it. That is the only mail client we are allowed to use.
जय हिंद
|
|
|
|
|
My commiserations. I used it for nearly 3 years at my first job.
|
|
|
|